public class TestFrame extends JFrame
{
public TestFrame()
{
setBounds(10, 10, 500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(3);
}
public static void main(String[] args) throws InterruptedException
{
TestFrame tf = new TestFrame();
tf.add(new JButton("test1"));
tf.setVisible(true);
Thread.sleep(2000);
tf.getContentPane().removeAll();
tf.add(new JButton("test2"));
System.out.print("Show test");
}
}
Run Code Online (Sandbox Code Playgroud)
我希望节目JButton("test2")在2秒后显示出来.
然后,添加thread.sleep(2000)后test1.
但我不知道为什么程序停止显示test1 JButton,而不显示test2 JButton和"显示测试"消息可以成功打印出来