我在使用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.");
}
}
}
答案 0 :(得分:1)
BorderLayout
最多可包含5个组件,每个布局约束一个(例如BorderLayout.PAGE_START
)。
该代码将两个组件添加到BorderLayout.CENTER
区域。
其他提示:
JFrame
和JPanel
。