1

这是我在这里的第一篇文章,所以希望一切都好。

我在netbeans。我用按钮等制作了一个窗口。

我有一个名为 SimpleThread 的类,如下所示:

public class SimpleThread extends Thread {

public SimpleThread()
{

}

@Override
public void run()
{

}

而且我有不同种类的子类线程,它们扩展了 simplethread(TimerThread、MouseGrabThread)。

public class MouseGrabThread extends SimpleThread{

private Point pt;
private JTextField x, y;

public MouseGrabThread(JTextField x, JTextField y)
{
    super();
    this.x = x; 
    this.y = y;
}

@Override
public void run()
{
    while(this.isAlive())
    {
        int[] xyVal = new int[2];
        xyVal = getCoords();
        x.setText("" + xyVal[0]);
        y.setText("" + xyVal[1]);
    }
}

public int[] getCoords()
{
    Point pt = MouseInfo.getPointerInfo().getLocation();

    int[] retArr = new int[2];
    retArr[0] = (int)pt.getX();
    retArr[1] = (int)pt.getY();

    return retArr;

}



public class TimerThread extends SimpleThread {

private JTextArea label;
private int time;

public TimerThread(JTextArea label, int time)
{
    super();
    this.label = label;
    this.time = time;
}

@Override
public void run()
{
    int counter = time;
    while(counter != -1)
    {

        label.setText("You have: " + counter + " seconds until the timer ends!\n");
        counter--;
        try {
            this.sleep(1000);
        } catch (InterruptedException ex) {
            System.out.println("Thread Interrupted");
        }

    }
    stop();
}

在我的 UI Window 类中,我有这个:

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
   SimpleThread timer = new TimerThread(jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
   SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
   timer.start();
   if(timer.isAlive())
   {
      mouseGrab.start();
   }
   while(timer.isAlive())//######
   {
       if(!mouseGrab.isAlive())
       {
           mouseGrab.start();
       }
   }
}           

当我按下按钮时,程序会冻结 10 秒钟。

我猜我标记的行 (//#####) 是导致 UI 在计时器持续时间内冻结的行,因为它在主线程中运行。我不知道如何纠正这个。

请原谅我缺乏编程知识,当我在大学里上一门关于数据结构的非常简单的课程时,我现在才自己进入线程。如果可能的话,你能不能尽可能地把答案“哑巴”下来?

另外,我知道我这样做很糟糕,但是我调用了 stop() 函数,即使它不好(不要为此开枪,我不知道该怎么做!)如果有人可以回答这对我来说如何做得很好,太棒了!

4

1 回答 1

1

您可能想要的是mouseGrab在倒计时结束时结束:

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
   SimpleThread timer = new TimerThread(jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
   SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.

   mouseGrab.start();
   timer.start();

   // Wait until countdown finishes
   while(timer.isAlive()) {}

   mouseGrab.stop();
}

在您粘贴的代码中,您mouseGrabtimer运行时一直在启动。您可能更希望在定时器开启时​​让鼠标抓取运行。

编辑:确实,stop()不推荐使用,你真的应该boolean running在你的中使用一个属性TimerThread并将其run()方法的内容包装在一些

while (running) {
    /* stuff */
}

然后用一些二传手在外部“停止”这个线程。正确的答案将是例如:

   mouseGrab.start();
   timer.start();

   // Wait until countdown finishes
   while(timer.isAlive()) {}

   mouseGrab.setRunning(false);
}

Edit2:这最终似乎是你想要的:

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {                                      
   SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
   SimpleThread timer = new TimerThread(mouseGrab, jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second

   mouseGrab.start();
   timer.start();
}

和:

public class MouseGrabThread extends SimpleThread {

    private Point pt;
    private JTextField x, y;
    private boolean running;

    public MouseGrabThread(JTextField x, JTextField y) {
        super();
        this.x = x; 
        this.y = y;
    }

    @Override
    public void run() {
        running = true;
        while(running) {
            int[] xyVal = new int[2];
            xyVal = getCoords();
            x.setText("" + xyVal[0]);
            y.setText("" + xyVal[1]);
        }
    }

    public int[] getCoords() {
        Point pt = MouseInfo.getPointerInfo().getLocation();

        int[] retArr = new int[2];
        retArr[0] = (int)pt.getX();
        retArr[1] = (int)pt.getY();

        return retArr;

    }

    public void break() {
        this.running = false;
    }
}


// ------------- //


public class TimerThread extends SimpleThread {

    private MouseGrabThread mouseGrab;
    private JTextArea label;
    private int time;

    public TimerThread(MouseGrabThread mouseGrab, JTextArea label, int time)
    {
        super();
        this.label = label;
        this.time = time;
        this.mouseGrab = mouseGrab;
    }

    @Override
    public void run()
    {
        int counter = time;
        while(counter != -1)
        {
            label.setText("You have: " + counter + " seconds until the timer ends!\n");
            counter--;
            try {
                this.sleep(1000);
            } catch (InterruptedException ex) {
                System.out.println("Thread Interrupted");
            }

        }

        mouseGrab.break();
    }
}
于 2013-04-05T17:36:50.250 回答