Domanda

I have a JFrame that has a button on it that starts a loop. The loop makes the robot click on my screen etc... On The JFrame, how come when the 'x' button is pressed, the program does not terminate? The button does absulotely nothing. I've removed all of the un-relevant code. This controls the Robot:

package nova;

public class Run {
    public static void main(String[] args) throws Exception {
        while (true) {
        // move
        Thread.sleep(Number.random(350, 650));
        // click
        Control.getPos();
        Thread.sleep(Number.random(120, 340));
        // move
        // click
        Control.getPos();
        Thread.sleep(Number.random(800, 1000));
        // space bar
        Thread.sleep(Number.random(11500, 12900));
        // move
        // click
        Control.getPos();
        Thread.sleep(Number.random(120, 340));
        // move
        // click
        Control.getPos();
        Thread.sleep(Number.random(800, 1000));
        // space bar
        Thread.sleep(Number.random(11500, 12900));
        }
   }

}

And this is the JFrame:

package nova;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

public class Main extends JFrame implements ActionListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;

public Main() {
    /*
     * JFrame.
     */
    setSize(600, 600);// Size of JFrame
    setVisible(true);// Sets if its visible.
    /*
     * JButton.
     */
    JButton startButton = new JButton("Start");// The JButton name.
    add(startButton);// Add the button to the JFrame.
    startButton.addActionListener(this);// Reads the action.
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            onExit();
        }
    });

}

private void onExit() {
    System.exit(0);
}

/*
 * The main method.
 */
public static void main(String[] args) {
    new Main();// Reads method main()
}

/*
 * What the button does.
 */
public void actionPerformed(ActionEvent e) {
    // does this when the button is pressed
    try {
        Run.main(null);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
}

I am completely confused on why the program is not terminating and closing. How this be resolved?

È stato utile?

Soluzione

A Robot is not part of the GUI. It does not execute on the Event Dispatch Thread, so closing the GUI has no effect on it.

How this be resolved?

Add logic in your windowClosing(...) method to stop the Robot. So you will need to somehow restructure your code to get rid the while (true) loop.

Maybe you can create an ArrayList to keep track of all the events you want to generate. Then you can use a Timer to invoke each event. Then in the windowClosing() code you stop the Timer.

Altri suggerimenti

Why not just have:

 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Become:

 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top