import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class JTest extends JFrame{
private JPanel p=new JPanel();
public JTest(){
JButton b=new JButton("Show me");
add(b,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(600,400);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Thread(new Runner(p)).start();
}
});
}
class Runner implements Runnable{
JPanel panel;
public Runner(JPanel panel){
this.panel=panel;
}
public void run(){
JLabel label=new JLabel(new ImageIcon("d:/123.jpg"));
panel.add(label);
}
}
public static void main(String[] args){
new JTest();
}}上边的代码中,点击按钮,面板中显示图片,可为什么点了之后没有反映,必须切换下应用程序或者改变窗口大小后才能显示??求助
import java.awt.event.*;
import javax.swing.*;
public class JTest extends JFrame{
private JPanel p=new JPanel();
public JTest(){
JButton b=new JButton("Show me");
add(b,BorderLayout.NORTH);
add(p,BorderLayout.CENTER);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(600,400);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Thread(new Runner(p)).start();
}
});
}
class Runner implements Runnable{
JPanel panel;
public Runner(JPanel panel){
this.panel=panel;
}
public void run(){
JLabel label=new JLabel(new ImageIcon("d:/123.jpg"));
panel.add(label);
}
}
public static void main(String[] args){
new JTest();
}}上边的代码中,点击按钮,面板中显示图片,可为什么点了之后没有反映,必须切换下应用程序或者改变窗口大小后才能显示??求助
panel.add(label);
JTest.this.setVisible(true);
JPanel panel;
public Runner(JPanel panel){
this.panel=panel;
}
public void run(){
JLabel label=new JLabel(new ImageIcon("D:/123.jpg"));
panel.add(label);
panel.revalidate();
}
}
reviladite()
和
doLayout()方法都管用