java - 使用 JButton 启动/停止程序 (Java)

标签 java jframe jbutton simulation awtrobot

所以我创建了一个程序,它可以实现发送随机击键的预期目的。现在我已经实现了一个带有启动/停止按钮的非常基本的 GUI。该程序意味着当整数“running”等于 1 时运行。因此我决定让按钮将 running 的值从 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“开始”时,它似乎不会将 int running 更改为 1,并且程序不会启动。每当我在代码中手动更改 int 时,程序就会运行。所以问题是 JButton 没有更新变量。为什么?我真的很困惑。

感谢大家的阅读。

最佳答案

尝试一下这段代码

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");

}
}

关于java - 使用 JButton 启动/停止程序 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19334045/

相关文章:

java - 计数器关闭 JFrame,弹出确认对话框

java - 打印整个程序布局

java - DocumentListener 干扰我的 JTextField

java - JButton 数组上的 ActionListener

java - "PKIX path building failed"和 "unable to find valid certification path to requested target"

java - 如何为 MyLocationOverlay 中的 "you are here"点使用自定义位图?

java - 如何在遇到未处理的异常时关闭 java 日志文件

java - 来自其他类的变量在 JFrame 中没有获取值

java - 2D 按钮数组 ActionListener,颜色变化?

java - Maven - 无法激活 webstart