Java – running functions on JButton

I'm trying to create a program in Java that uses a robot to press a specific key every few seconds It has a GUI with start and stop buttons and a tab that tells it which state to enter So far I've done all the work, except that when I click "start", it runs the cycle of my robot function (which is infinite) and it doesn't enable the stop button as I think I know it's stupid where infinite loops are placed, but I'm not sure how to make it work

I don't do a lot of Java work. It's just an interesting thing. I want to try, but I'm stuck for a while Any help is appreciated

import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

    /**
    * 
    */
    private static final long serialVersionUID = 1L;
    private static boolean running = false;;
    private JButton start_button;
    private JButton stop_button;
    private JLabel tl;
    private static int i = 0;
    Robot robot;

    void start() {

        JFrame frame = new JFrame("Helper");
        tl = new JLabel("Running: " + running);
        start_button = new JButton("Start");
        stop_button = new JButton("Stop");
        stop_button.setEnabled(false);
        frame.add(tl);
        frame.add(start_button);
        frame.add(stop_button);
        frame.setSize(300,100);
        frame.setVisible(true);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloSEOperation(EXIT_ON_CLOSE);
        frame.setLocation(400,400);

        try {
            robot = new Robot();
        } catch (AWTException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        robot.setAutoDelay(200);

        start_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                start_button.setEnabled(false);
                stop_button.setEnabled(true);
                running = true;
                tl.setText("Running: " + running);
                while (running) {
                    robot_loop(robot);
                }

            }
        });
        stop_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                start_button.setEnabled(true);
                stop_button.setEnabled(false);
                running = false;
                tl.setText("Running: " + running);

            }
        });

    }

    public static void main(String[] args) {
        new Main().start();

    }

    private static void robot_loop(Robot robot) {

        robot.keyPress(KeyEvent.VK_NUMPAD0);
        robot.keyRelease(KeyEvent.VK_NUMPAD0);

        System.out.println("numpad 0 pressed! - " + i);
        i++;

    }

}

Solution

I have adapted my comments into answers

Call the actionperformed method of these event listeners on the event scheduling thread of swing, and it will cause the GUI to freeze because you enter an infinite loop You can create a thread in the actionperformed method and complete the work inside the new thread Although the next problem you encounter is to find a good way to stop the thread when the user presses the stop button

It's cool that you've done all the logic in the code Making it work is as simple as changing:

start_button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            start_button.setEnabled(false);
            stop_button.setEnabled(true);
            running = true;
            tl.setText("Running: " + running);
            while (running) {
                robot_loop(robot);
            }

        }
    });

To complete work on your own thread:

start_button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            start_button.setEnabled(false);
            stop_button.setEnabled(true);
            running = true;
            tl.setText("Running: " + running);
            Executors.newSingleThreadExecutor().submit(new Runnable() {
                @Override public void run() {
                    while (running) {
                        robot_loop(robot);
                    }
                }
            });
        }
    });

The above code uses the executor framework (Java. Util. Concurrent. *), rather than directly creating a thread Another approach suggested by nachokk is to use the timer Java util. Timer or javax swing. Timer (in this case, it should be OK)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>