当我运行我的自动点击器时,我无法停止它 [英] When I run my autoclicker I can't stop it

查看:46
本文介绍了当我运行我的自动点击器时,我无法停止它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我点击开始按钮,我无法点击返回或停止.我需要停止应用程序.停止应用程序的一种方法是您必须关闭计算机电源:D.当应用程序运行时,开始按钮被锁定,我无法点击它.单击退出按钮不起作用.这个应用程序是简单的 AutoClicker.在java中存在比机器人类更好的自动点击器吗?我该如何解决?哪里有错误?或者我可以做些什么来停止应用程序?

If I click on start button I can't click back or stop. I need to stop the app. One way to stop the app is that you must power off computer :D. When the app is running, start button is locked and I can't click on it. Click on the exit button doesn't work. This app is simple AutoClicker. Exist in java something better to do autoclicker than robot class? How can I fix it? Is there error anywhere? Or is there something I can do to stop the app?

 import java.awt.AWTException;
 import java.awt.BorderLayout;
 import java.awt.EventQueue;
 import java.awt.Robot;

 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.border.EmptyBorder;
 import javax.swing.JCheckBox;
 import javax.swing.JLabel;

 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.awt.Color;


public class AutoClicker extends JFrame {

private JPanel contentPane;
private final JLabel m = new JLabel("AutoClicker");

/**
 * Launch the application.
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                AutoClicker frame = new AutoClicker();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public AutoClicker() {

    setAlwaysOnTop(false);
    setTitle("AutoClicker");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 147, 162);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JCheckBox chckbxOnTop = new JCheckBox("On Top");
     boolean onTop = false;
    chckbxOnTop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            if(chckbxOnTop.isSelected()){
                setAlwaysOnTop(true);
            }
            else{
                setAlwaysOnTop(false);
            }   

        }
    });
    chckbxOnTop.setBounds(6, 7, 97, 23);
    contentPane.add(chckbxOnTop);




    JCheckBox chckbxAutoclicker = new JCheckBox("AutoClicker");
    chckbxAutoclicker.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            while(chckbxAutoclicker.isSelected()){

                        try {
                            Robot r = new Robot();
                            r.mousePress(MouseEvent.BUTTON1_MASK);
                            r.setAutoDelay(1080);
                            r.mouseRelease(MouseEvent.BUTTON1_MASK);
                        } catch (AWTException e1) {

                            e1.printStackTrace();
                        }

                    }
                }



    });
    chckbxAutoclicker.setBounds(6, 80, 97, 23);
    contentPane.add(chckbxAutoclicker);
    m.setForeground(new Color(153, 102, 0));
    m.setBounds(16, 92, 120, 31);
    contentPane.add(m);
}
}

推荐答案

Swing 是单线程的 - 在该线程上调用任何长时间运行的任务将锁定该线程(EDT)并阻止任何绘制、事件等...从发生.ActionListener 实现之一创建了一个无限循环:

Swing is single threaded - calling any long running task on that thread will lock that thread up (the EDT) and prevent any painting, events, etc... from occurring. One of the ActionListener implementations creates an infinite loop:

while(chckbxAutoclicker.isSelected()){

以上永远不会评估为假,因为它是在 EDT 上评估的,并且在 EDT 空闲之前不会发生事件(例如禁用 JCheckBox 以允许此方法返回 false).如果您希望在允许 EDT 执行其必要任务的同时连续运行任务,您有三个选择:

The above will never evaluate to false, because it is evaluating on the EDT, and events (such as disabling the JCheckBox to allow this method to return false) cannot occur until the EDT is free. If you wish to continually run a task while allowing the EDT to performs its necessary tasks, you have three options:

  1. 创建一个新的线程.请注意,应使用 SwingUtilities.invoke*
  2. 将从此线程对 Swing 的任何调用分派到 EDT
  3. 使用 SwingWorker
  4. 如果您希望稍后在 EDT 上做某事,或在 EDT 上按计划运行某事,请使用 javax.swing.Timer
  1. Create a new Thread. Note any calls to Swing from this thread should be dispatched to the EDT using SwingUtilities.invoke*
  2. Use a SwingWorker
  3. If you wish to do something at a later time on the EDT, or run something on a schedule on the EDT, use a javax.swing.Timer

这篇关于当我运行我的自动点击器时,我无法停止它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆