Java 在运行时单击JButton时,JTextArea在一段时间内没有清晰显示
JTextArea在特定时间段内未清晰显示 我创建了一个JFrame,里面添加了一个JButton,当单击JButton时,JFrame将被展开,它将显示一个JTextArea,它将以2秒的间隔添加两行文本。问题是,当单击JButton时,atonce JFrame被展开,但JTextArea显示不清楚(JTextArea以黑色显示),如下所示: 2秒钟后,将显示两行文本,如下所示: 预期场景:当点击JButton时,atonce JTextArea应该清楚地显示为空,然后用户应该能够逐个查看输入的2行 有人能帮我吗 2秒后,将显示2行文本 不要使用Thread.sleep(…)。这样可以防止框架自身重新绘制 相反,您需要:Java 在运行时单击JButton时,JTextArea在一段时间内没有清晰显示,java,swing,jframe,jtextarea,Java,Swing,Jframe,Jtextarea,JTextArea在特定时间段内未清晰显示 我创建了一个JFrame,里面添加了一个JButton,当单击JButton时,JFrame将被展开,它将显示一个JTextArea,它将以2秒的间隔添加两行文本。问题是,当单击JButton时,atonce JFrame被展开,但JTextArea显示不清楚(JTextArea以黑色显示),如下所示: 2秒钟后,将显示两行文本,如下所示: 预期场景:当点击JButton时,atonce JTextArea应该清楚地显示为空,然后用户应该能够逐个查
并发性中所示
请务必阅读已尝试使用SwingUtilities.invokeLater的可能副本,但遇到相同的问题。这可能会有帮助:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class ExpandJFrame {
public static JFrame frame;
public static JButton btn;
public static JTextArea textArea;
public static void main(String[] args) {
frame = new JFrame("JFrame Expand");
frame.setSize(400, 200);
btn = new JButton("Expand");
btn.setBounds(10, 10, 80, 25);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
frame.setSize(400, 400);
textArea = new JTextArea();
textArea.setBounds(5, 210, 370, 150);
textArea.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
frame.add(textArea);
Thread.sleep(1000);
textArea.append("FirstLine\n");
Thread.sleep(1000);
textArea.append("SecondLine\n");
} catch (Exception e) {
}
}
});
frame.add(btn);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}