求助JAVA多个监听器同时使用
小弟初学JAVASE的监听器,想实现一个功能但是始终有个地方有问题,能请教各位大神解惑一下吗,谢谢。
功能如下:
实现鼠标自动移动到屏幕上的某个坐标然后点击左键N次(N为设置的值),在点击的过程中按ESC键可终止鼠标的继续点击。
现在遇到的问题是:鼠标自动点左键已经实现,但是在鼠标点击的过程中我按ESC键触发的键盘事件并没有起到预期终止程序的效果。
目前的情况是,只有等鼠标自动点的事件结束后,键盘监听器触发的事件才会开始运行。
换句话说,我在鼠标自动点的过程中,点了ESC键,是要等鼠标自动点击的次数完全点完后才会终止程序
package test;监听器
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.AWTException;
import java.awt.Button;
import java.awt.Label;
import java.awt.Robot;
import java.awt.TextField;
public class TT extends JFrame {
private int title = 0;
private int times;
final JFrame jf;
Button jb;
JPanel panel;
Label tx_lab;
final TextField tx;
static int command = 0;
public TT() {
jf = new JFrame(String.valueOf(title));
panel = new JPanel();
tx_lab = new Label("次数");
tx = new TextField();
jb = new Button("点我");
Label resolution_lab = new Label("坐标");
final TextField resolutionX = new TextField();
final TextField resolutionY = new TextField();
resolutionX.setText("1000");
resolutionY.setText("1000");
tx.setSize(50, 50);
tx.setText("5");
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Robot robot;
try {
robot = new Robot();
times = Integer.valueOf(tx.getText());
for (int i = 0; i < times; i++) {
robot.mouseMove(Integer.valueOf(resolutionY.getText()),
Integer.valueOf(resolutionY.getText()));
// robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(500);
// robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(500);
title++;
jf.setTitle(String.valueOf(title));
listener();
}
} catch (AWTException e1) {
e1.printStackTrace();
}
}
});
jf.add(panel);
panel.add(resolution_lab);
panel.add(resolutionX);
panel.add(resolutionY);
panel.add(tx_lab);
panel.add(tx);
panel.add(jb);
jf.pack();
jf.setSize(300, 300);
jf.setVisible(true);
}
public void listener() {
jb.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 27) {
System.exit(-1);
}
command = e.getKeyCode();
System.out.println(command);
}
});
}
public static void main(String[] args) throws Exception {
TT tt = new TT();
}
}
public class TT extends JFrame {
private int title = 0;
private int times;
final JFrame jf;
Button jb;
JPanel panel;
Label tx_lab;
final TextField tx;
static int command = 0;
public TT() {
jf = new JFrame(String.valueOf(title));
panel = new JPanel();
tx_lab = new Label("次数");
tx = new TextField();
jb = new Button("点我");
Label resolution_lab = new Label("坐标");
final TextField resolutionX = new TextField();
final TextField resolutionY = new TextField();
resolutionX.setText("1000");
resolutionY.setText("1000");
tx.setSize(50, 50);
tx.setText("5");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
Robot robot;
try {
robot = new Robot();
times = Integer.valueOf(tx.getText());
for (int i = 0; i < times; i++) {
robot.mouseMove(Integer.valueOf(resolutionY
.getText()), Integer
.valueOf(resolutionY.getText()));
// robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(500);
// robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(500);
title++;
jf.setTitle(String.valueOf(title));
listener();
}
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}).start();
}
});
jf.add(panel);
panel.add(resolution_lab);
panel.add(resolutionX);
panel.add(resolutionY);
panel.add(tx_lab);
panel.add(tx);
panel.add(jb);
jf.pack();
jf.setSize(300, 300);
jf.setVisible(true);
}
public void listener() {
jb.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 27) {
System.exit(-1);
}
command = e.getKeyCode();
System.out.println(command);
}
});
}
public static void main(String[] args) throws Exception {
TT tt = new TT();
}
}