Java:如何从头开始重新启动程序?

时间:2019-03-11 21:49:30

标签: java

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class bot {
    private static Robot robot = null;

    public static void main(String [] args) throws AWTException, 
     InterruptedException {
        try {
            robot =new Robot ();
        } catch (AWTException e) {
            e.printStackTrace ();
        }
        click ( 400,1000);

    }

    public static void click(int x, int y) throws AWTException, 
        InterruptedException{
        robot.mouseMove(x,y);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        robot.keyPress(KeyEvent.VK_O);
        robot.keyRelease(KeyEvent.VK_O);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyPress(KeyEvent.VK_E);
        robot.keyRelease(KeyEvent.VK_E);
        robot.keyPress(KeyEvent.VK_R);
        robot.keyRelease(KeyEvent.VK_R);
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
        robot.keyPress(KeyEvent.VK_T);
        robot.keyRelease(KeyEvent.VK_T);
        robot.keyPress(KeyEvent.VK_H);
        robot.keyRelease(KeyEvent.VK_H);
        robot.keyPress(KeyEvent.VK_R);
        robot.keyRelease(KeyEvent.VK_R);
        robot.keyPress(KeyEvent.VK_O);
        robot.keyRelease(KeyEvent.VK_O);
        robot.keyPress(KeyEvent.VK_W);
        robot.keyRelease(KeyEvent.VK_W);
        robot.keyPress(KeyEvent.VK_SPACE);
        robot.keyRelease(KeyEvent.VK_SPACE);
        robot.keyPress(KeyEvent.VK_A);
        robot.keyRelease(KeyEvent.VK_A);
        robot.keyPress(KeyEvent.VK_K);
        robot.keyRelease(KeyEvent.VK_K);
        robot.keyPress(KeyEvent.VK_I);
        robot.keyRelease(KeyEvent.VK_I);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        Thread.sleep(4000);
    }
}

这是为我所在的不和谐服务器开个玩笑而写的,但是我不知道如何使它重新启动或重复该过程。

我希望它从头开始,然后再次输入我的句子。我对Java真的很陌生,因此不胜感激!谢谢,祝你有美好的一天!

1 个答案:

答案 0 :(得分:1)

我认为您只需要一个循环即可。尝试以下代码:

public static void main(String [] args) throws AWTException,
        InterruptedException {
    try {
        robot =new Robot ();
    } catch (AWTException e) {
        e.printStackTrace ();
    }

    while(true){
        click ( 400,1000);
    }
}