로봇을 사용하여 몇 초마다 특정 키를 누르는 프로그램을 Java에서 만들려고합니다. GUI에는 시작 및 중지 버튼이 있고 어떤 상태인지 알 수있는 레이블이 있습니다. "시작"을 클릭하면 로봇 기능 (무한대)을위한 루프가 실행됩니다. 생각했던 것처럼 멈춤 버튼을 사용할 수 없습니다. 나는 무한 루프가 놓여있는 곳에서 무언가 어리 석다는 것을 안다.하지만 올바르게 작동하게 만드는 방법을 모르겠다.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.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static boolean running = false;;
private JButton start_button;
private JButton stop_button;
private JLabel tl;
private static int i = 0;
Robot robot;
void start() {
JFrame frame = new JFrame("Helper");
tl = new JLabel("Running: " + running);
start_button = new JButton("Start");
stop_button = new JButton("Stop");
stop_button.setEnabled(false);
frame.add(tl);
frame.add(start_button);
frame.add(stop_button);
frame.setSize(300, 100);
frame.setVisible(true);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocation(400, 400);
try {
robot = new Robot();
} catch (AWTException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
robot.setAutoDelay(200);
start_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start_button.setEnabled(false);
stop_button.setEnabled(true);
running = true;
tl.setText("Running: " + running);
while (running) {
robot_loop(robot);
}
}
});
stop_button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
start_button.setEnabled(true);
stop_button.setEnabled(false);
running = false;
tl.setText("Running: " + running);
}
});
}
public static void main(String[] args) {
new Main().start();
}
private static void robot_loop(Robot robot) {
robot.keyPress(KeyEvent.VK_NUMPAD0);
robot.keyRelease(KeyEvent.VK_NUMPAD0);
System.out.println("numpad 0 pressed! - " + i);
i++;
}
}
그 이벤트 리스너의 actionPerformed 메소드는 스윙의 이벤트 파견 스레드에서 호출, 그리고 당신이 무한 루프에 진입하고 있기 때문에, 그것은 GUI를 일으킬 수 있습니다 얼어 버려. actionPerformed 메소드 안에 스레드를 만들고 새 스레드 내부에서 작업을 수행 할 수 있습니다. 다음 번 문제는 사용자가 중지 버튼을 누를 때마다 스레드를 중지하는 좋은 방법을 찾는 것입니다. – sgbj
swingtimer insteas를 사용하십시오 – nachokk