问题描述
我即将完成开始的工作,但是我只是无法使我的键绑定正常工作。当我按 w 时,它可以正常工作。但是,当循环(自动点击器)正在运行时(通过按下JButton),按键绑定不会注册,并且我无法执行任何操作。我希望 w 键的操作停止循环,但是按键绑定失去焦点,并且循环一直保持运行,直到通过按命令 q 终止循环。 / p>
package autoclicker;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class click
implements ActionListener{
JFrame frame;
JLabel label;
JButton button;
Action ON;
Action OFF;
private static Robot bot;
public static boolean status = false;
click(){
ON = new statusON();
//setting up JFrame
frame = new JFrame("Bullet Chicken Clicker");
label = new JLabel();
button = new JButton("turn on?");
frame.setSize(400,400);
frame.setLocation(600,150);
frame.setVisible(true);
frame.setAlwaysOnTop(true);
frame.add(label);
frame.add(button);
button.setPreferredSize(new Dimension(40,40));
button.setopaque(true);
button.setForeground(Color.BLACK);
button.setBounds(125,150,30);
button.setVisible(true);
button.addActionListener(this);
button.setFocusable(false);
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(Keystroke.getKeystroke('w'),"forward");
label.getActionMap().put("forward",ON);
}
//main action
public void run(){
try {
bot = new Robot();
} catch (AWTException e2) {
e2.printstacktrace();
}
if(status != false);
while (status == true) {
try {
bot.mousepress(InputEvent.BUTTON1_DOWN_MASK);
bot.delay(1);
bot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
bot.delay(1);
}catch (Exception e) {
e.printstacktrace();
}
if (status == false) {
status = false;
break;
}
}
}
public class statusON extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
status = false;
System.out.print(status);
}
}
@Override
public void actionPerformed(ActionEvent e) {
status = true;
System.out.print(status);
try {
Thread.sleep(1000);
run();
} catch (InterruptedException e1) {
// Todo Auto-generated catch block
e1.printstacktrace();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)