Cant stop a while loop with a button
Im trying to write a program which moves the mouse every 3 minutes (to stop a screen saver coming on) but I want to be able 开发者_开发百科to stop and start it at will. As you can see below I have create the buttons and method but when you click run it steps into the while loop and because its in effect an infinite loop it won't look and see if you have clicked the end button.
I have tried system.exit(0) on click for the end button, having the end button pass in false to the method run() and as you can see from the code ive tried an if statement in the while loop to see if it will take notice of me!
Any help would be greatly appreciated!
code:
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;
}
}
}
}
Well first correct the condition for exit i.e., make it exit == true
as mentioned in the first answer.
Second I don't think even this is going to fix your problem because you are making an infinite loop in the actionPerformed which gets called by EDT (Even Dispatch Thread) and this will halt the event processing altogether. So instead start a new Thread inside the actionPerformed method that moves the mouse. Keep a reference to that thread so that you can stop/interrupt the thread or you can also set the exit condition to stop the thread.
Let me know if you need a code example for this.
Try
if (exit == true)
{
break;
}
if (exit == true) { break; }
I have some doubt about that working out, though I have yet to try it. Wouldn't Java run while without ever listening to mouse, which will end up as infinite loop?
I think it'll have to be some equivalent of javascript function setInterval()
to move the mouse and clearInterval()
once the button has been clicked.
first of all make it exit == true;
instead of using exit = true;
you can use loop = false
.
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.
In event handling thread you have to maintain a object of while loop thread, by which you can set appropriate value of loop
or exit
variable to stop the loop.
精彩评论