这是我自己写的程序,刚学java不久,这个是自动按键的程序,按F1启动,每隔5秒自动按下F5刷新网页,但是我想退出程序,就是不能退出,按F2不管用,求大神帮忙让程序按F2可以退出程序!...
这是我自己写的程序,刚学java不久,这个是自动按键的程序,按F1启动,每隔5秒自动按下F5刷新网页,但是我想退出程序,就是不能退出,按F2不管用,求大神帮忙让程序按F2可以退出程序!
程序如下:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class AutoPress {
JFrame jfFrame = new JFrame();
public AutoPress() {
JLabel jLabel = new JLabel("按F1开启自动按键,F2退出!");
jfFrame.setTitle("自动按键");
jfFrame.setSize(200, 200);
jfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfFrame.add(jLabel);
jfFrame.addKeyListener(new AutoRobot());
//jfFrame.addMouseListener(new AutoRobot());
jfFrame.setVisible(true);
// jfFrame.dispose();
}
public static void main(String[] args) throws AWTException {
new AutoPress();
}
//}
class AutoRobot implements KeyListener,MouseListener {
public void flash() throws AWTException {
Robot robot = new Robot();
robot.delay(5000);
robot.keyPress(116);
}
public void flash2() throws AWTException {
Robot robot = new Robot();
robot.delay(3000);
robot.keyPress(116);
//robot.mousePress(1);
}
public void keyPressed(KeyEvent e) {
//System.out.println(e.getKeyCode());
while (true) {
// System.out.println(1);
if (e.getKeyCode() == KeyEvent.VK_F2) {
jfFrame.dispose();
}
if (e.getKeyCode() == KeyEvent.VK_F1)
try {
flash();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
//System.out.println(e.getButton());
while(true){
try {
flash2();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//System.out.println(1);
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}//
展开