English 中文(简体)
停泊在 but带上
原标题:Cant stop a while loop with a button

试图撰写节目,每3分钟播放一次节目(以阻止播放的屏幕),但我希望能够停下来并开始。 如下文所示,我建立了纽州和方法,但当你点击时,当你把步骤推到 lo中,而且实际上,它赢得了无限的 lo,看看你是否点了终点。

我已尝试过系统.exit(0)点击尾纽顿,带端纽顿的通行证错过了这种方法,而且你可以从该法典中看到,如果在坐期间有人试图看到我是否会注意到我!

任何帮助都将受到高度赞赏。

法典:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Test 
{
boolean loop;
static boolean exit;

public static void main(String[] args) throws AWTException
{
    System.out.println("before");
    makeButtons();
    System.out.println("after");
}

public static void makeButtons()
{
    JFrame jfrMain = new JFrame ("Mouse Robot");
    JPanel jplMain = new JPanel();
    final JButton run = new JButton("Run");
    final JButton end = new JButton("End");

    run.setEnabled(true);
    end.setEnabled(true);

    run.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //run.setEnabled(false);
            //end.setEnabled(true);
            try {
                run(true);
            } catch (AWTException e1) 
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

    end.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            exit = true;
        }
    });

    jplMain.setLayout(new FlowLayout());
    jplMain.add(run);
    jplMain.add(end);

    jfrMain.getContentPane().add(jplMain, BorderLayout.CENTER);
    jfrMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrMain.pack();
    jfrMain.setVisible(true);

    }   

public static void run(boolean loop) throws AWTException
{
    Robot r2d2 = new Robot();

    while(loop)
    {

        System.out.println("1");
        Point mousePoint = MouseInfo.getPointerInfo().getLocation();
        mousePoint.translate(0, 1);
        r2d2.mouseMove(mousePoint.x, mousePoint.y);
        r2d2.delay(60000);
        //r2d2.delay(60000);
        //r2d2.delay(60000);
        System.out.println("2");
        mousePoint = MouseInfo.getPointerInfo().getLocation();
        mousePoint.translate(0, -1);
        r2d2.mouseMove(mousePoint.x, mousePoint.y);
        r2d2.delay(60000);
        //r2d2.delay(60000);
        //r2d2.delay(60000);
        System.out.println("looping");
        if (exit = true)
        {
            break;
        }
    }
}

}
最佳回答

首先正确说明出国的条件,即,如第一份答复所述,将其改为exit = 真实

第二,我甚至不认为这将会解决你的问题,因为你正在对东帝汶国防军所呼吁的行动进行无限的准备,这将完全停止事件处理。 因此,在移动改变的操作方法中开辟了新的通道。 经常提到这种read线,以便你能够阻止或阻断胎面,或者你也可以为停止read线设定出路条件。

让我知道,你们是否需要这方面的一个典型例子。

问题回答

提 出

if (exit == true)
{
   break;
}

if (exit == true) { break; }

我对这项工作有一些怀疑,尽管我尚未尝试。 t是否在没有听说过会 end,最终会变成无限的 lo?

我认为,如果 but子被点击,就不得不将 mo印功能打成某种等同体。

首先是:<代码>exit=真实;。

而不是使用<代码>exit = 真实; 页: 1

Still it will not stop while loop as soon as you click on end button. To stop it immediately after button clicked you must use two different threads. 1. Handling your events. 2. another one running the while loop.

如果处理线索,你必须保留一对 lo子的物体,通过该物体,你可以确定<代码>loop或exit变量的适当价值,以阻止 lo。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签