Java,thread.sleep()阻止按钮的使用(不识别按钮点击)

标签 java multithreading swing netbeans

我正在编写一个程序,绘制一列在屏幕上移动的火车。我将图形类与 fillRect、drawOval 和绘制线方法一起使用。我在屏幕右侧有一个启动火车的按钮。从屏幕上消失后,它应该以随机的 y 位置重新出现并再次运行。这应该循环直到单击停止按钮。问题是我正在使用 thread.sleep() 方法在程序更新火车位置之前暂停程序。由于某种原因,程序运行时我无法单击任何按钮。关于如何让我的停止按钮起作用有什么想法吗?谢谢。

这是我正在使用的代码。它还具有 JFrame 表单在 netbeans 中具有的正常代码。

private void btnStartTrainActionPerformed(java.awt.event.ActionEvent evt) {                                              
    run = true;
    while (run) {
    Graphics g = jPanel1.getGraphics(); 
    int x =0;
    int y = (int)(Math.random() *500) + 20;
    int smoke =1;

    for( x = 900; x > -600; x--)
    {
        drawTrain(g, x, y, smoke);        
    try {
          Thread.sleep(17);
        } 
catch (InterruptedException e) 
    {
             e.printStackTrace();
    }
    clearJFrame(g);
    smoke++;
    x = x-4; 
    }
    if (x == -599)
    {
        x = 900;
        y = (int)(Math.random() *500) + 20;
    }
    }
    }                                             

    private void btnStopTrainActionPerformed(java.awt.event.ActionEvent evt) {                                             
    run = false;
    } 
public void drawTrain(Graphics g, int x, int y, int smoke)
{
         // draw locomotive
    g.setColor(Color.RED);
    g.fillRect(x, y, 100, 30); 
    g.fillRect(x +100, y-30, 40, 60); 
    g.setColor(Color.BLACK);
    g.fillRect(x +110, y-20, 20, 10);
    g.drawLine(x +10, y, x, y-20); 
    g.drawLine(x +11, y, x, y-20);
    g.drawLine(x, y-20, x +30, y-20);
    g.drawLine(x +30, y-20, x +20, y);
    g.drawLine(x +31, y-20, x +20, y);
    g.drawLine(x, y+30, x-20, y+25);
    g.drawLine(x-20, y+25, x-20, y+20); 
    g.drawLine(x-20, y+20, x, y+15); 
    g.drawOval(x +10, y+20, 25, 25); //draw wheels
    g.drawOval(x +35, y+20, 25, 25);
    g.drawOval(x +60, y+20, 25, 25);
    g.drawOval(x +85, y+20, 25, 25);
    g.drawOval(x +110, y+20, 25, 25);
    if (smoke >20) 
        g.drawOval(x +8, y-33, 12, 12); // draw smoke
    if (smoke >40) 
        g.drawOval(x +12, y-53, 12, 12);
    if (smoke >60)
        g.drawOval(x +18, y-73, 13, 13);
    if (smoke >80)
        g.drawOval(x +25, y-100, 14, 14);
    if(smoke >100)
        g.drawOval(x+31, y-120, 15, 15);
    if (smoke > 120)
        g.drawOval(x+37, y - 140, 16, 16);
    if (smoke > 140)
        g.drawOval(x+44, y-160, 17, 17);
    g.setColor(Color.RED);
    g.fillRect(x +160, y, 80, 30);      // draw additional cars
    g.fillRect(x +260, y, 80, 30);
    g.fillRect(x +360, y, 80, 30);
    g.setColor(Color.BLACK);
    g.drawOval(x +160, y+20, 25, 25); 
    g.drawOval(x +215, y+20, 25, 25);
    g.drawOval(x +260, y+20, 25, 25);
    g.drawOval(x +315, y+20, 25, 25);
    g.drawOval(x +360, y+20, 25, 25);
    g.drawOval(x +415, y+20, 25, 25);


 }
 public void clearJFrame(Graphics g)
 {
     g.setColor(jPanel1.getBackground());
     g.fillRect(0, 0, jPanel1.getWidth(), jPanel1.getHeight());
 }

最佳答案

您正在事件调度线程上执行Thread.sleep(),使 GUI 在该调用的整个持续时间内卡住。

延迟在 GUI 上执行操作的正确方法是使用 Swing Timer 来安排它。

关于Java,thread.sleep()阻止按钮的使用(不识别按钮点击),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159915/

相关文章:

java - 在 JPanel 中显示 Word 文档

java - JDialog 消失得太早,用户无法查看它包含的信息

本地主机 Java 客户端错误 : MasterNotDiscoveredException[waited for [30s]] 上的 Elasticsearch 2.1.1

java - 如何在 Spring 和 Hibernate 中使用两个数据库/数据源

java - 从哪里开始异步任务 - Android

java - 异步执行多个java方法并获取完成工作的结果

java - 无法找到 tools.jar

java - 多模块 Spring Boot 配置

multithreading - 共享线程变量而不使其成为全局变量(Perl)

java - 如何在 JLayer 覆盖的 JComponent 上激活工具提示?