我正在写一个石头布剪刀图形用户界面应用程序,在我的一个 JPanel
s、 我有一个 JLabel
. 这个 JLabel
应该说石头一秒钟,然后是布一秒钟,然后是剪刀一秒钟。到目前为止只显示了 JLabel
最后的拍摄文本。我似乎不明白为什么。有人能帮忙吗?
代码:
import javax.swing.*;
import java.awt.*;
class RockPaperScissorsShoot {
public JFrame shootFrame;
JPanel shootPanel;
JLabel shootLabel;
int flagThing = 0;
public RockPaperScissorsShoot(Window gameWindow) {
shootFrame = new JFrame();
shootFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shootFrame.setSize(250, 150);
shootPanel = new JPanel(new GridLayout(1, 1));
shootFrame.add(shootPanel);
shootLabel = new JLabel();
shootPanel.add(shootLabel);
shootFrame.setVisible(true);
shootFrame.setTitle("Rock!"); // the title changes
shootLabel.setText("Rock!"); // but the JLabel text does not
waitForMilSecs(1000);
shootFrame.setTitle("Paper!");
shootLabel.setText("Paper!");
waitForMilSecs(1000);
shootFrame.setTitle("Scissors!");
shootLabel.setText("Scissors!");
waitForMilSecs(1000);
shootFrame.setTitle("Shoot!");
shootLabel.setText("Shoot!"); //Except here, right here, the text shows up
}
public static void waitForMilSecs(long ms) {
long checkMS = System.currentTimeMillis() + ms;
while (System.currentTimeMillis() <= checkMS) {
System.out.println("Not yet!");
}
System.out.println("NOW!");
}
}
编辑:我尝试添加一个 SwingWorker
在单独的线程中处理等待。我还有一个冰冻的gui。代码:
import java.awt.*;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
//import javax.swing.event.ChangeEvent;
import javax.swing.SwingWorker.StateValue;
class RockPaperScissorsShoot {
public JFrame shootFrame;
JPanel shootPanel;
JLabel shootLabel;
int flagThing = 0;
public RockPaperScissorsShoot(Window gameWindow) {
shootFrame = new JFrame();
shootFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
shootFrame.setSize(250, 150);
shootPanel = new JPanel(new GridLayout(1, 1));
shootFrame.add(shootPanel);
shootLabel = new JLabel();
shootPanel.add(shootLabel);
shootFrame.setVisible(true);
changeText(false, "Rock!", shootLabel, shootFrame, 1);
changeText(true, "Paper!", shootLabel, shootFrame, 1);
changeText(true, "Scissors", shootLabel, shootFrame, 1);
changeText(true, "Shoot!", shootLabel, shootFrame, 1);
}
public static void changeText(boolean wait, String text, JLabel shootLabel, JFrame shootFrame, long secondsToWait) {
while (wait) {
wait = waiter(secondsToWait * 1000);
}
shootFrame.setTitle(text);
shootLabel.setText(text);
}
private static boolean waiter(long ms) {
boolean keepGoing = false;
SwingWorker sw1 = new SwingWorker() {
@Override
protected Boolean doInBackground() throws Exception {
Thread.sleep(ms);
// String res = "Finished Execution";
return true;
}
@Override
protected void done() {
try {
System.out.println(get().toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
};
// executes the swingworker on worker thread
sw1.execute();
while (sw1.getState() != StateValue.DONE) {
System.out.println("Not done (debug)");
}
System.out.println("Done!");
return false;
}
public static void waitForMilSecs(long ms) {
long checkMS = System.currentTimeMillis() + ms;
while (System.currentTimeMillis() <= checkMS) {
System.out.println("Not yet!");
}
System.out.println("NOW!");
}
public static void wait(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
System.out.println("Error waiting, cannot wait lol");
}
}
}
有人能解释一下为什么我的gui还冻着吗?
1条答案
按热度按时间tag5nh1u1#
使用[摆动]计时器。
请尝试以下操作。