2013年12月1日星期日

Help JAVA multiple listeners simultaneously

Younger beginner JAVASE the listener want to implement a function , but always have a place to have a problem , you can ask the god of doubts about it , thank you.
function as follows:
realize the mouse automatically moves to a coordinate on the screen and then click the left N times (N is the value set ) , the process by clicking the ESC key to terminate continue to click the mouse .

problem encountered is now : Left mouse automatically point has been achieved, but in the process I clicked the mouse keyboard events triggered by pressing the ESC key to terminate the program and did not play the expected results.

The current situation is only after an event such as a mouse automatically end point , keyboard triggering event listener will begin to run.
In other words , I am in the process of automatic spot in the mouse , the ESC key point is to wait for the automatic mouse clicks will terminate the program entirely point after

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();
}

}

------ Solution ------------------------------------- -------

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();
}

}

------ For reference only ------------------------- --------------
terry21 Thank you. Realized that the original is to serve as a multi- threaded
------ For reference only ---------------------------- -----------
but questioning Oh , I have used the thread tried before
@ Override
public void run () {
listener ();

}
public static void main (String [] args) throws Exception {
T t = new T ();
Thread t1 = new Thread (t);
t1.start ();


}
probably code like this. . But the effect did not reach you . . . Can tell what principles you ~ Thank you.
------ For reference only -------------------------------------- -
single-thread rule : Swing threads at the same time can only be accessed by one thread . Generally, this thread is the event dispatch thread (event-dispatching thread).
exceptions to the rule : some operations guaranteed to be thread safe.
event distribution : If you need to deal with (event-handling) or draw from events outside the local code to access UI, then you can use SwingUtilities class invokeLater () or invokeAndWait () method .
create threads : If you need to create a thread to handle such a large number of computing power or by consuming I / O capacity limitations of the work you can use tools such as a thread SwingWorker or Timer????.
============================================== ==========================
you write this thread simply does not make sense , you create a thread to run TT, then the end of the main thread immediately , with no create a thread , the main thread to run directly TT is the same effect . Recommended that each swing event operations are time-consuming to start a thread to run , it is best to use ExecutorService to manage all the threads , it will reuse the thread resources.

没有评论:

发表评论