package a;
public class Threads implements Runnable {
public volatile boolean exit = true;
@Override
public void run() {
// TODO 自动生成的方法存根
int i = 0;
while(exit) {
i++;
System.out.println(i);
System.out.println(exit);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
package a;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class qmq extends JFrame implements KeyListener{
JLabel text = new JLabel("F11");
qmq(){
setTitle("qmq");
setLayout(new GridLayout(1, 1));
setSize(500, 200);
add(text);
setVisible(true);
this.addKeyListener(this);
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO 自动生成的方法存根
Threads rb = new Threads();
Thread t = new Thread(rb,"1");
if(arg0.getKeyCode() == KeyEvent.VK_F11) {
t.start();
System.out.print("按下了F11");
}
else if(arg0.getKeyCode() == KeyEvent.VK_F12) {
try {
System.out.print("按下了F12");
Thread.sleep(10000);
rb.exit=false;
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO 自动生成的方法存根
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
new qmq();
}
}