Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JPanel中使用sleep_Java_Jpanel_Sleep - Fatal编程技术网

Java 在JPanel中使用sleep

Java 在JPanel中使用sleep,java,jpanel,sleep,Java,Jpanel,Sleep,我编写了一个简单的java程序,在屏幕上放置一些矩形(在彼此之间经过一些延迟之后) 运行上述代码的问题是,没有显示正方形。10*2S=20秒后,显示包含所有正方形的最终面板。我想要的是: 1-使用g.drawRect绘制正方形 2-等待2秒钟 3-删除之前的方块并绘制新方块。也许你可以使用下面的代码,我用注释对你的代码做了一些修改 import java.awt.Color; import java.awt.Graphics; import javax.swing.JPa

我编写了一个简单的java程序,在屏幕上放置一些矩形(在彼此之间经过一些延迟之后)

运行上述代码的问题是,没有显示正方形。10*2S=20秒后,显示包含所有正方形的最终面板。我想要的是:

1-使用
g.drawRect
绘制正方形

2-等待2秒钟


3-删除之前的方块并绘制新方块。

也许你可以使用下面的代码,我用注释对你的代码做了一些修改

    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JFrame;

    public class RectTest {
     DrawPanel panel;
     int x;
     int y;
     public static void main(String[] args) {
      new RectTest().startApp();
     } 

     public void startApp() {
      panel = new DrawPanel();
      JFrame app = new JFrame();
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
      //modify this line
      app.getContentPane().add(panel);
      app.setSize(450, 250);
      app.setVisible(true);
      //added for loop here
      for (int i = 0; i < 10; i++ ) {
       // x,y here
       x = 10+5*i;
       y = 10+5*i;
       // repaint the panel
       panel.repaint();
       // wait 2sec
       try{
          Thread.sleep( 2000 );
       }
       catch (InterruptedException ex) { }
    }
   }

   class DrawPanel extends JPanel {
    public void paintComponent( Graphics g) {
       //super.paintComponent(g);
       // repaint the backround to see the single reactangles
       g.setColor(Color.white);
       g.fillRect(0, 0, this.getWidth(), this.getHeight());

       g.setColor(Color.green);
       g.drawRect(x, y, 20, 20);
    }
   }
  }
导入java.awt.Color;
导入java.awt.Graphics;
导入javax.swing.JPanel;
导入javax.swing.JFrame;
公共类测试{
拉丝板;
int x;
int-y;
公共静态void main(字符串[]args){
新的测试().startApp();
} 
公开作废startApp(){
panel=新的DrawPanel();
JFrame app=新的JFrame();
应用程序setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//修改此行
app.getContentPane().add(面板);
附录设置尺寸(450250);
app.setVisible(真);
//这里添加了for循环
对于(int i=0;i<10;i++){
//这里是x,y
x=10+5*i;
y=10+5*i;
//重新粉刷面板
panel.repaint();
//等2秒
试一试{
《睡眠》(2000年);
}
catch(InterruptedException ex){}
}
}
类DrawPanel扩展了JPanel{
公共组件(图形g){
//超级组件(g);
//重新粉刷背面,以查看单个表面
g、 setColor(Color.white);
g、 fillRect(0,0,this.getWidth(),this.getHeight());
g、 setColor(Color.green);
g、 drawRect(x,y,20,20);
}
}
}
您正在上睡眠,因此阻止了所有与GUI相关的事件。GUI在EDT这一线程上运行,因此您不能像那样阻止它(即在循环中睡眠)

EDT链接中的相关引用:

将事件分派线程上运行的代码看作一系列短任务是很有用的。[……]

事件调度线程上的任务必须快速完成
;否则,未处理的事件将备份,用户界面将变得无响应

在您的例子中,您正在绘制10个矩形,其中一些矩形处于中间。然后处理事件队列的其余部分。这将导致GUI被卡住10 x 2000毫秒,然后绘图完成

相反,请使用计时器来计时GUI相关事件:

Swing计时器(的实例)在指定延迟后触发一个或多个操作事件。[……]

[…]我们建议对GUI相关任务使用Swing计时器[…],因为Swing计时器都共享相同的、预先存在的计时器线程,并且GUI相关任务会在事件调度线程上自动执行


它应该按照你的要求工作,我换了颜色。
package guitest2;
import javax.swing.JFrame;
public class Guitest2 {
    public static void main(String[] args) {
        DrawPanel panel = new DrawPanel();
        JFrame app = new JFrame();
        app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
        app.add(panel);
        app.setSize(450, 250);
        app.setVisible(true);
    }    
}
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JFrame;

    public class RectTest {
     DrawPanel panel;
     int x;
     int y;
     public static void main(String[] args) {
      new RectTest().startApp();
     } 

     public void startApp() {
      panel = new DrawPanel();
      JFrame app = new JFrame();
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
      //modify this line
      app.getContentPane().add(panel);
      app.setSize(450, 250);
      app.setVisible(true);
      //added for loop here
      for (int i = 0; i < 10; i++ ) {
       // x,y here
       x = 10+5*i;
       y = 10+5*i;
       // repaint the panel
       panel.repaint();
       // wait 2sec
       try{
          Thread.sleep( 2000 );
       }
       catch (InterruptedException ex) { }
    }
   }

   class DrawPanel extends JPanel {
    public void paintComponent( Graphics g) {
       //super.paintComponent(g);
       // repaint the backround to see the single reactangles
       g.setColor(Color.white);
       g.fillRect(0, 0, this.getWidth(), this.getHeight());

       g.setColor(Color.green);
       g.drawRect(x, y, 20, 20);
    }
   }
  }