所以我创建了一个程序,它可以实现发送随机击键的目的。现在我已经实现了一个带有开始/停止按钮的非常基本的GUI。程序意味着当整数“running”等于1时运行。所以我决定让按钮将运行的值从0改为1,开始循环。
以下是代码:
public class AutoKeyboard extends JFrame {
public static int running = 0; // program will not run until this is 1
Random r = new Random();
public static int randInt(int min, int max) { // returns random number
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
private JLabel label;
private JButton button;
public AutoKeyboard() {
setLayout(new FlowLayout());
label = new JLabel("Not Running");
add(label);
button = new JButton("Start");
add(button);
event f = new event();
button.addActionListener(f);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent f) {
label.setText("Running");
System.out.println("Running");
running = 1; // changes running to 1? but doesn't start the program?
}
}
public static void main(String[] args) throws InterruptedException {
AutoKeyboard gui = new AutoKeyboard();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(180, 80);
gui.setVisible(true);
gui.setResizable(false);
gui.setTitle("Anti AFK");
while (running == 1) { // if running is 1, do this
try {
int delay = randInt(4864,7834); // 336415, 783410 15 97
Robot robot = new Robot();
int keypress = randInt(65, 86);
Thread.sleep(delay);
robot.keyPress(keypress);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
我的问题是,每当我按下我的JButton“Start”时,它似乎没有改变int运行到1,并且程序无法启动。每当我在代码中手动更改int时,程序就会起作用。所以问题是JButton没有更新变量。为什么?我真的很困惑。
感谢大家阅读。
答案 0 :(得分:1)
尝试使用此代码
import java.awt.AWTException;
import java.awt.FlowLayout;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class NewClass1 extends JFrame {
public int running; // program will not run until this is 1
Random r = new Random();
public void performtast(){
while (running == 1) { // if running is 1, do this
try {
int delay = randInt(4864,7834); // 336415, 783410 15 97
Robot robot = new Robot();
int keypress = randInt(65, 86);
Thread.sleep(delay);
robot.keyPress(keypress);
} catch (InterruptedException ex) {
Logger.getLogger(NewClass1.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
public static int randInt(int min, int max) {
// returns random number
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
private JLabel label;
private JButton button;
public NewClass1() {
setLayout(new FlowLayout());
label = new JLabel("Not Running");
add(label);
button = new JButton("Start");
add(button);
event f = new event();
button.addActionListener(f);
}
public class event implements ActionListener {
@Override
public void actionPerformed(ActionEvent f) {
label.setText("Running");
System.out.println("Running");
running = 1; // changes running to 1? but doesn't start the program?
performtast();
}
}
public static void main(String[] args) throws InterruptedException, AWTException {
NewClass1 gui = new NewClass1();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(180, 80);
gui.setVisible(true);
gui.setResizable(false);
gui.setTitle("Anti AFK");
}
}
答案 1 :(得分:0)
您必须通知您的代码运行为1
running = 1; // changes running to 1? but doesn't start the program?
您正在制作1
但之后没有运行代码。
你需要做的是
public void actionPerformed(ActionEvent f) {
label.setText("Running");
System.out.println("Running");
running = 1; // changes running to 1? but doesn't start the program?
//ok everything set, now do my task.
// now call the method here to do your task, which uses *running *
}
答案 2 :(得分:0)
没有办法告诉程序它应该运行,比如checkForPaused()。
public static void checkForPaused() {
synchronized (GUI_INITIALIZATION_MONITOR) {
while (isPaused()) {
try {
GUI_INITIALIZATION_MONITOR.wait();
} catch (Exception e) {
}
}
}
}