java – 在JButton上运行函数
发布时间:2020-12-15 04:13:18 所属栏目:Java 来源:网络整理
导读:我正在尝试在 java中创建一个程序,它使用机器人每隔几秒钟按一个特定的键.它有一个带有开始和停止按钮的GUI和一个告诉它进入哪个状态的标签.到目前为止我已经完成了所有工作,除了当我点击“开始”它运行我的机器人功能的循环(这是无限的)它不像我想的那样启
我正在尝试在
java中创建一个程序,它使用机器人每隔几秒钟按一个特定的键.它有一个带有开始和停止按钮的GUI和一个告诉它进入哪个状态的标签.到目前为止我已经完成了所有工作,除了当我点击“开始”它运行我的机器人功能的循环(这是无限的)它不像我想的那样启用停止按钮.我知道无限循环放置的地方有些愚蠢,但我不确定如何让它正常工作.
我不做很多java工作,这只是一个有趣的事情,我想尝试但是被困住了一段时间.任何帮助表示赞赏. 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++; } } 解决方法
我已将我的评论改编成答案.
在Swing的事件调度线程上调用这些事件侦听器的actionPerformed方法,并且由于您进入无限循环,它将导致GUI冻结.您可以在actionPerformed方法中创建一个线程,并在新线程内部完成工作.虽然您遇到的下一个问题是找到一种很好的方法来在用户按下停止按钮时停止该线程. 很酷的是,你已经在代码中完成了所有逻辑.让它工作就像改变一样简单: 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); } } }); 要在自己的线程上完成工作: 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); } } }); } }); 上面的代码使用了执行程序框架(java.util.concurrent.*),而不是直接创建一个线程. nachokk建议的另一种方法是使用计时器java.util.Timer或javax.swing.Timer(在这种情况下应该没问题). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |