[英]How update a JFrame in Java?
I have that code我有那个代码
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ChatClient extends JFrame {
public static void main(String[] args) throws InterruptedException {
JFrame frame=new JFrame("chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
frame.setLayout(null);
JLabel label=new JLabel();
label.setText("HOR");
label.setBounds(100,100,100,100);
frame.add(label);
Thread.sleep(1000);
JLabel label1=new JLabel();
label1.setText("SE");
label1.setBounds(100,200,100,100);
frame.add(label1);
}
}
I want write multiple strings in the frame but at different times.我想在框架中写入多个字符串,但在不同的时间。 The problem is that "hor" is printed correctly, but "se" is printed only if I change the size of the window or if I minimize it.问题是“hor”打印正确,但只有当我更改 window 的大小或最小化它时才会打印“se”。 I think "se" is not printed until I update the frame.我认为在更新框架之前不会打印“se”。 I know that i can resolve with frame.repaint()
after Thread.sleep()
, but is the more appropriate method?我知道我可以在Thread.sleep()
frame.repaint()
解决,但更合适的方法是什么?
Setup the Frame completely before sleep and then change the text after sleep在睡眠前完全设置框架,然后在睡眠后更改文本
private static void drawFrame() {
JFrame frame=new JFrame("chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
frame.setLayout(null);
JLabel label=new JLabel();
label.setBounds(100,100,100,100);
frame.add(label);
JLabel label1=new JLabel();
label1.setBounds(100,200,100,100);
frame.add(label1);
label.setText("HOR" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
// frame elemets setup
sleep();
label1.setText("HOR" + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.