小编YWX*_*YWX的帖子

在秋千中使用sleep()

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和"显示测试"消息可以成功打印出来

java swing multithreading

0
推荐指数
1
解决办法
951
查看次数

标签 统计

java ×1

multithreading ×1

swing ×1