paintImmediately()函数没有效果? 400 报错
@Kooosy 你好,想跟你请教个问题:
public class Test extends JFrame {
JPanel panel = new JPanel();
int i = 0;
JButton button = new JButton("da");
JTextField field1 = new JTextField("aaaa");
JTextField field2 = new JTextField("bbbb");
JTextField field3 = new JTextField("cccc");
public Test(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 200, 400);
button.addActionListener(new ac());
panel.setLayout(new GridLayout(3, 1));
panel.add(field1);
panel.add(field2);
panel.add(field3);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(button,BorderLayout.EAST);
getContentPane().add(panel,BorderLayout.WEST);
}
public class ac implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
updata();
}
}
public static void main(String[] args){
Test test = new Test();
test.setVisible(true);
}
public void updata(){
try {
field1.setText(i+1+"");
field1.paintImmediately(field1.getBounds());
Thread.sleep(2000);
field2.setText(i+2+"");
field2.paintImmediately(field2.getBounds());
Thread.sleep(2000);
field3.setText(i+3+"");
field3.paintImmediately(field3.getBounds());
Thread.sleep(2000);
i = i+1;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("hello");
}
}
例如这个程序,只有field1会立刻更新,field2、field3都是等主程序完成才更新,为什么呢
1. 对UI的操作必须在UI线程中 main方法 里面改为
SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Test test = new Test(); test.setVisible(true); } });2. update 方法里面的
field1.paintImmediately(field1.getBounds()); Thread.sleep(2000);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。