1

我做了一个简单的自动点击器,但是当我运行它时屏幕变黑,所以我无法停止它等等。我不知道我做错了什么。我想也许我必须集中注意力,但我不确定。代码:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.JRadioButton;

public class AutoClicker1 extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    public static int rate;
    public static boolean go = false;
    public static int time;
    public static int multiplyer;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AutoClicker1 frame = new AutoClicker1();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AutoClicker1() {
        setTitle("Auto Clicker");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 361, 154);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNoOfClicks = new JLabel("Interval between clicks");
        lblNoOfClicks.setBounds(10, 25, 149, 14);
        contentPane.add(lblNoOfClicks);

        textField = new JTextField();
        textField.setBounds(10, 55, 139, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnStopf = new JButton("Stop(F11)");
        btnStopf.setBounds(203, 45, 89, 23);
        contentPane.add(btnStopf);

        JButton button = new JButton("Stop(F11)");
        button.setBounds(203, 81, 89, 23);
        contentPane.add(button);

        final JRadioButton rdbtnSeconds = new JRadioButton("Seconds");
        rdbtnSeconds.setBounds(6, 81, 65, 23);
        contentPane.add(rdbtnSeconds);

        final JRadioButton rdbtnMilliseconds = new JRadioButton("Milliseconds");
        rdbtnMilliseconds.setBounds(71, 81, 109, 23);
        contentPane.add(rdbtnMilliseconds);
        btnStopf.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                go = false;
            }

        });
        JButton btnStart = new JButton("Start(F10)");
        btnStart.setBounds(203, 11, 89, 23);
        contentPane.add(btnStart);
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                go = true;
                if (rdbtnMilliseconds.isSelected()) {
                    autoClick();
                } else {
                    if (rdbtnSeconds.isSelected()) {
                        multiplyer = 1000;
                        autoClick();
                    }
                }
            }
        });

    }

    private void autoClick() {
        requestFocus();
        rate = Integer.parseInt(textField.getText());
        time = (rate * multiplyer);
        System.out.print(time);
        try {
             Robot robot = new Robot();
             while (true) {
                try {
                   Thread.sleep(rate);
                   robot.mousePress(InputEvent.BUTTON1_MASK);
                   robot.mouseRelease(InputEvent.BUTTON1_MASK);
                } catch (InterruptedException ex) {}
             }
          } catch (AWTException e) {}
    }

    private void keyListner(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_F10) {
            System.out.print("pressed F10");
            go = true;
        }
        if (key == KeyEvent.VK_F11) {
            go = false;
        }
    }

    private void setTheme() {
        try {
            UIManager
                    .setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void checkRate() {
        if (rate < 500) {
            rate = 0;
        }
    }
}
4

2 回答 2

5

您正在使用 阻塞事件调度线程autoClick()while(true)不是使用Swing Timer。我建议不要使用 keyListener 只听 2 个键,您可以为此目的使用键绑定。此外,您不应该直接调用setBounds依赖于适当的LayoutManager来定位在屏幕中。

于 2013-08-28T17:39:53.950 回答
0

当您在 AutoClick 方法中进入 wile 循环时,您会在 Main 方法中跳出程序循环。

您可以改为在“游戏循环”中运行您的程序。

在主要方法中,我要做的是创建一个名为 run() 的方法

public void run()
{
    int FPS = 60;

    float startTime =  System.currentTimeInMillis();

    while(started)
    {  
        float currentTime = System.currentTimeInMillis();
        float passedTime = currentTime - startTime;

        startTime = System.currentTimeInMillis();

        if(passedTime > (float) 1000/FPS)
          { 
           update();
          }
    }  
}

然后在update方法中放入程序的逻辑。我知道这与您当前的操作方式完全不同,但我认为它允许更大的灵活性。

例如,在您的动作侦听器中,您可以让它调用“startClick()”方法,在该方法中将布尔值设置为 true 并初始化您当前在 autoclick 方法中初始化的内容。然后您的 update() 方法将如下所示:

public void update()
{
  if(boolean) //boolean value that startClick sets to true
    {
      robot.mousePress(InputEvent.BUTTON1_MASK);
      robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }
}

这样程序就不会陷入无限的 while 循环,您可以使用 FPS 变量控制它的更新频率。

于 2013-08-28T19:07:31.147 回答