내 목표를 달성하기 위해 내가 무엇을 권하고 싶습니까? 정규 intervall에서 Robot
물건 (키 누름)을 반복하고 싶습니다. 설명도 내 코드에 있습니다. eclipse neon with windowbuilder
(java/swing/jframe)을 사용 중입니다.Runnable에서 반복되는 작업을위한 Java 최고의 솔루션
package patrick;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.sql.Time;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import javax.management.timer.Timer;
public class Background implements Runnable {
public boolean isRunning = true;
int intervall = 0;
Robot r = null;
public Background(int i)
{
this.intervall = i;
}
@Override
public void run()
{
System.out.println("Thread: " + Thread.currentThread().getName() + " gestartet!");
System.out.println("Intervall i: " + intervall);
try {
r = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
while(isRunning)
{
//int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
//now I want to do Robot stuff here that repeats in the given intervall time
}
}
public void stop()
{
isRunning = false;
System.out.println("Thread: " + Thread.currentThread().getName() + " gestoppt!");
}
}
편집 :
bg = new Background(itmp);
th1 = new Thread(bg);
th1.start();
EDIT2 :이 같은 MainWindow를 클래스에서
배경 시작이 같이
첫 번째 대답
에?timer.scheduleAtFixedRate(
while(isRunning)
{
//int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
//now I want to do Robot stuff here that repeats in the given intervall time
}
, delay, period);
EDIT3는 :
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
while(isRunning)
{
//int intervall is given me from my MainWindow Class and represents the minutes (for example I get a 5 which means 5 minutes)
//now I want to do Robot stuff here that repeats in the given intervall time
}
}
}, 0, intervall*6000);
내가 EDIT2에서처럼 타이머 https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html – Chains
을 찾고 당신의 동안 당신은 의미합니까 가치가있을 수도? – Patrick260284
TimerTask 객체를 만들어야합니다. https://docs.oracle.com/javase/8/docs/api/java/util/TimerTask.html – Chains