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

で Event Dispatch ThreadをブロックautoClick()while(true)ていますが、代わりにSwing Timerを使用できます。2つのキーのみをリッスンするためにkeyListenerを使用しないことをお勧めします。その目的にはキーバインディングを使用できます。また、画面に配置するために適切なLayoutManagerに直接setBounds依存して呼び出すべきではありません。

于 2013-08-28T17:39:53.950 に答える