当 运行 时,数字时钟表现得很奇怪。 (Java)
Digital clock acts strange when running it. (Java)
所以我制作了一个程序,所以当 运行 运行程序时会出现一个时钟。所以。当我按下一个按钮 "Start" 时,时钟应该在范围内随机移动并且也像时钟一样移动(我的意思是像数字,15:18:19、15:18:20)。但我现在的问题是每当我按下程序时。时钟保持在同一个位置,当将睡眠时间更改为 500 等时。然后它开始移动,但时间 semms 走得更快(因为它应该睡 1000)。但是我没有在代码中看到问题所在。但是也许你们中的一些人可以帮助我。
public void startClock() {
Thread t2 = new Thread() {
public void run() {
if (clocking) {
Random rand = new Random();
while(clocking){
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
movingClock.setText(hour + ":" + minute + ":" + second);
int x = rand.nextInt(100) + 1;
int y = rand.nextInt(100) + 1;
movingClock.setBounds(x, y, 150, 150);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t2.start();
}
其中时钟 = 真。因为每当我按下按钮。我将时钟设置为 true,然后 运行 startClock()。
但正如我之前所说。如果我将睡眠时间更改为 500 以下,数字时钟只会随机走动。如果它是睡眠时间 (1000),我怎样才能让它工作?
编辑新的:
public void startMoving() {
Thread t1 = new Thread() {
public void run() {
if (moving) {
Random random = new Random();
while (moving) {
int x = random.nextInt(100) + 1;
int y = random.nextInt(100) + 1;
movingDisplay.setBounds(x, y, 150, 150);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t1.start();
}
public void startClock() {
Thread t2 = new Thread() {
public void run() {
if (clocking) {
Random rand = new Random();
while(clocking){
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
movingClock.setText(hour + ":" + minute + ":" + second);
int x = rand.nextInt(100) + 1;
int y = rand.nextInt(100) + 1;
movingClock.setBounds(x, y, 150, 150);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
movingClock.setText(hour + ":" + minute + ":" + second);
movingClock.setBounds(x, y, 150, 150);
movingDisplay.setBounds(x, y, 150, 150);
}
} );
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t2.start();
}
我猜您正试图在某些 Swing 组件(如 JPanel)上移动时钟。您应该对 Event Dispatch Thread
上的组件进行所有修改
您的代码应如下所示:
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
movingClock.setText(hour + ":" + minute + ":" + second);
movingClock.setBounds(x, y, 150, 150);
}
} );
看看:
Java Event-Dispatching Thread explanation
所以我制作了一个程序,所以当 运行 运行程序时会出现一个时钟。所以。当我按下一个按钮 "Start" 时,时钟应该在范围内随机移动并且也像时钟一样移动(我的意思是像数字,15:18:19、15:18:20)。但我现在的问题是每当我按下程序时。时钟保持在同一个位置,当将睡眠时间更改为 500 等时。然后它开始移动,但时间 semms 走得更快(因为它应该睡 1000)。但是我没有在代码中看到问题所在。但是也许你们中的一些人可以帮助我。
public void startClock() {
Thread t2 = new Thread() {
public void run() {
if (clocking) {
Random rand = new Random();
while(clocking){
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
movingClock.setText(hour + ":" + minute + ":" + second);
int x = rand.nextInt(100) + 1;
int y = rand.nextInt(100) + 1;
movingClock.setBounds(x, y, 150, 150);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t2.start();
}
其中时钟 = 真。因为每当我按下按钮。我将时钟设置为 true,然后 运行 startClock()。
但正如我之前所说。如果我将睡眠时间更改为 500 以下,数字时钟只会随机走动。如果它是睡眠时间 (1000),我怎样才能让它工作?
编辑新的:
public void startMoving() {
Thread t1 = new Thread() {
public void run() {
if (moving) {
Random random = new Random();
while (moving) {
int x = random.nextInt(100) + 1;
int y = random.nextInt(100) + 1;
movingDisplay.setBounds(x, y, 150, 150);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t1.start();
}
public void startClock() {
Thread t2 = new Thread() {
public void run() {
if (clocking) {
Random rand = new Random();
while(clocking){
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
movingClock.setText(hour + ":" + minute + ":" + second);
int x = rand.nextInt(100) + 1;
int y = rand.nextInt(100) + 1;
movingClock.setBounds(x, y, 150, 150);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
movingClock.setText(hour + ":" + minute + ":" + second);
movingClock.setBounds(x, y, 150, 150);
movingDisplay.setBounds(x, y, 150, 150);
}
} );
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
t2.start();
}
我猜您正试图在某些 Swing 组件(如 JPanel)上移动时钟。您应该对 Event Dispatch Thread
上的组件进行所有修改您的代码应如下所示:
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
movingClock.setText(hour + ":" + minute + ":" + second);
movingClock.setBounds(x, y, 150, 150);
}
} );
看看: Java Event-Dispatching Thread explanation