我有一个JFrame的程序,但当我使用我的“开始”动作时,它无法切换,而是它只是坚持运行而我需要强制关闭它:(你能说我吗?为什么,因为我是新手编码我不会发现我的错误是我的代码:
public class ClickBotSetUp extends JFrame {
static ClickBotSetUp frame;
static Robot robot;
public static void ClickBot() throws AWTException{
final Robot robot = new Robot();
robot.delay(2000);
while(true)
{
{
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(least);
}
}
}
public static void main(String[] args) throws IOException, AWTException {
frame = new ClickBotSetUp("setup speed");
frame.setVisible(true);
frame.setBackground(Color.WHITE);
robot = new Robot();
}
//settings
static int least = 100;
JTextField count;
JButton start;
static int bot = 0;
public ClickBotSetUp(String title) throws HeadlessException
{
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(180, 145);
Container cont = getContentPane();
cont.setLayout(new BorderLayout());
((JComponent) cont).setBorder(BorderFactory.createEmptyBorder(15, 15,
15, 15));
//desing
JLabel instructions = new JLabel("low nubers can crash");
cont.add(instructions, BorderLayout.SOUTH);
//buttons
start = new JButton("start");
start.setAction(starting);
cont.add(start, BorderLayout.WEST);
//score
JPanel scores = new JPanel();
scores.setLayout(new BorderLayout());
cont.add(scores, BorderLayout.CENTER);
JPanel times = new JPanel();
times.setLayout(new BorderLayout());
scores.add(times, BorderLayout.WEST);
//times
count = new JTextField("100");
count.setEditable(false);
times.add(count, BorderLayout.CENTER);
JButton add10 = new JButton("+10");
add10.setAction(add_10);
times.add(add10, BorderLayout.NORTH);
JButton remove10 = new JButton("-10");
remove10.setAction(remove_10);
times.add(remove10, BorderLayout.SOUTH);
}
private AbstractAction starting = new AbstractAction("start") {
@Override
public void actionPerformed(ActionEvent arg0) {
frame.setVisible(false);
try {
ClickBot();
} catch (AWTException e) {
e.printStackTrace();
}
}
};
private AbstractAction add_10 = new AbstractAction("+10") {
@Override
public void actionPerformed(ActionEvent arg0) {
least = least + 10;
count.setText("" +least+ "");
}
};
private AbstractAction remove_10 = new AbstractAction("-10") {
@Override
public void actionPerformed(ActionEvent arg0) {
if(least < 20){
}else{
least = least - 10;
count.setText("" +least+ "");
}
}
};
}
答案 0 :(得分:5)
while(true
)是一个无限循环。它永远不会退出。
在while(someCondition Here)中有一些条件会破坏while循环。
同样@ArnaudDenoyelle指出
你需要一行
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
这是因为X按钮的JFrame的默认行为等同于
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
因此,大多数情况下,您需要在创建JFrame时手动添加行
答案 1 :(得分:2)
while(true)
将创建一个永无止境的循环。你应该使用一个标志,当条件满足时,应该改变标志,以便循环。