自动点击器问题

时间:2014-06-29 02:13:36

标签: java swing user-interface

我在使用GUI时出现了一些问题,用于制作GUI和按钮,但现在只使用GUI。另外,我无法弄清楚如何使用GUI本身运行自动点击器。

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Clicker {

public JTextField ClickSpd;

    public static void main(String[] args) {
        ButtonFrame frame = new ButtonFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class ButtonFrame extends JFrame {

    @SuppressWarnings("LeakingThisInConstructor")
    public ButtonFrame() {
        super("AutoClicker Version 1.0.8");
        setLayout(new FlowLayout());
        setSize(300, 100);
        ButtonPanel panel = new ButtonPanel();
        panel.add(new JLabel("Enter Clicking Speed (Milliseconds)"));
        ClickSpd = new JTextField(20);
        panel.add(ClickSpd);
        add(panel, BorderLayout.CENTER);
    }

    public JTextField ClickSpd;

    class ButtonPanel extends JPanel implements ActionListener {

        private Component frame;

        public ButtonPanel() {
            final JButton b2 = new JButton("Start");
            add(b2, BorderLayout.SOUTH);
            b2.setActionCommand("Start");

            b2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ButtonFrame bf = new ButtonFrame();
                    if ("Start".equals(e.getActionCommand())) {
                        int rate = 0;
                        while (rate == 0) {
                            try {
                                System.out.println("Speed of the autoclicker (in milliseconds): ");
                                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                                try {
                                    rate = Integer.parseInt(in.readLine());
                                    if (rate < 1) {
                                        rate = 0;
                                        System.out.println("Must be at least 1.");
                                    }
                                } catch (NumberFormatException ex) {
                                    System.out.println("Error - please try again.");
                                }
                            } catch (IOException e1) {}
                        }

                        try {
                            Robot robot = new Robot();
                            while (true) {
                                try {
                                    Thread.sleep(rate);
                                    robot.mousePress(InputEvent.BUTTON1_MASK);
                                    robot.keyRelease(InputEvent.BUTTON1_MASK);
                                } catch (InterruptedException ex) {}
                            }
                        } catch (AWTException e1) {}
                    }
                }
            });
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            throw new UnsupportedOperationException("Not Supported yet.");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

BorderLayout最多可包含5个组件,每个布局约束一个(例如BorderLayout.PAGE_START)。

该代码将两个组件添加到BorderLayout.CENTER区域。

其他提示:

  1. 鉴于这是一个Q&amp; A网站,最好先询问一个特定问题。
  2. 不要不必要地扩展GUI组件。在这种情况下,您应该只使用JFrameJPanel
  3. 的实例
  4. 请了解课程,方法和方法的常见Java naming conventions(特别是用于名称的情况)属性名称&amp;一贯地使用它们。