用java在label上图片显示不出来 15
求大神指点啊,代码哪里有问题啊packagexuni;importjava.awt.EventQueue;importjavax.swing.ImageIcon;impo...
求大神指点啊,代码哪里有问题啊
package xuni;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
public class shiyishi {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
String path="";
shiyishi window = new shiyishi();
while(true){
for(int i=2;i<4;i++){
path="/images/11"+i+".png";
System.out.println(path);
window.initialize(path);
window.frame.setVisible(true);
Thread.sleep(1000);
window.frame.setVisible(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public shiyishi() {
frame = new JFrame();
frame.setBounds(100, 100, 576, 440);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//initialize();
}
private void initialize(String path) {
JLabel lblNewLabel = new JLabel();
lblNewLabel.setIcon(new ImageIcon(shiyishi.class.getResource(path)));
frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
//ImageIcon image=new ImageIcon("/images/112.png");
}
} 展开
package xuni;
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
public class shiyishi {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
String path="";
shiyishi window = new shiyishi();
while(true){
for(int i=2;i<4;i++){
path="/images/11"+i+".png";
System.out.println(path);
window.initialize(path);
window.frame.setVisible(true);
Thread.sleep(1000);
window.frame.setVisible(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public shiyishi() {
frame = new JFrame();
frame.setBounds(100, 100, 576, 440);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//initialize();
}
private void initialize(String path) {
JLabel lblNewLabel = new JLabel();
lblNewLabel.setIcon(new ImageIcon(shiyishi.class.getResource(path)));
frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
//ImageIcon image=new ImageIcon("/images/112.png");
}
} 展开
2个回答
2013-10-26
展开全部
import java.awt.GridLayout;//新增加
public class JLabelImageDemo extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new JLabelImageDemo();
}
});
}
public JLabelImageDemo() {
super("JLabelImageDemo");
this.setBounds(100, 100, 576, 440);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(1, 2));
try{
String path;
for(int i=2; i<4; i++){
path="/images/11"+i+".jpg";
System.out.println(path);
initialize(path);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
this.setVisible(true);
}
private void initialize(String path) {
JLabel lblNewLabel = new JLabel();
lblNewLabel.setIcon(new ImageIcon(getClass().getResource(path)));
this.getContentPane().add(lblNewLabel);
}
}
经测试,正确显示两张图片
public class JLabelImageDemo extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new JLabelImageDemo();
}
});
}
public JLabelImageDemo() {
super("JLabelImageDemo");
this.setBounds(100, 100, 576, 440);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(1, 2));
try{
String path;
for(int i=2; i<4; i++){
path="/images/11"+i+".jpg";
System.out.println(path);
initialize(path);
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
this.setVisible(true);
}
private void initialize(String path) {
JLabel lblNewLabel = new JLabel();
lblNewLabel.setIcon(new ImageIcon(getClass().getResource(path)));
this.getContentPane().add(lblNewLabel);
}
}
经测试,正确显示两张图片
追问
我的意思是交替出现两张图片,不是同时出现两张啊,求指点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询