gpt4 book ai didi

java - 如何通过 setText 方法设置定时器?

转载 作者:行者123 更新时间:2023-11-30 10:04:33 25 4
gpt4 key购买 nike

我使用 setText 方法进行更改,但它不起作用。当我调试我的程序时,我注意到 setText() 工作不正常,因为它只显示最后一件事,而它的循环将进行到 60 秒。

例如,我想查看从 1 到 60 的所有数字,但它只显示 59。

从我看到的单击 JButton 的结果来看,我认为 Actionlistener 似乎没有问题。这就是为什么我多次更改逻辑但总是发生相同的原因。

public class Game extends JFrame implements ActionListener {

JLabel jTimer = new JLabel();//Showing time
JLabel jScore = new JLabel("");//Showing score

Game() {
JFrame Gframe = new JFrame("Game play");
Gframe.setBounds(400, 400, 800, 800);
Gframe.setLayout(null);
JPanel pScore = new JPanel();

EtchedBorder border = new EtchedBorder();
JLabel score = new JLabel(" Score ");
Font font1 = new Font("굴림", Font.PLAIN, 20);


jScore.setPreferredSize(new Dimension(5, 5));
score.setFont(font1);
score.setBounds(330, 30, 100, 100);
score.setBorder(border);


Font font2 = new Font("고딕체", Font.PLAIN, 20);
JLabel ttime = new JLabel(" Time ");

JButton start = new JButton("START");
start.setFont(font2);
start.setBounds(150, 40, 100, 100);
start.setBorder(border);
start.addActionListener(this);

jTimer.setLayout(null);
jTimer.setBounds(330, 30, 300, 300);


ttime.setFont(font2);
ttime.setBounds(200, 15, 100, 100);
ttime.setBorder(border);


pScore.setLayout(new GridLayout(2, 2));
pScore.setBounds(330, 30, 200, 100);
pScore.add(score);
pScore.add(ttime);
pScore.add(jScore);
pScore.add(jTimer);
add(start);
Gframe.add(pScore);//Including Score&Time
Gframe.add(start);//Adding Start Butto

Gframe.setVisible(true);
}


public void setTimer() {
int i = 0;
while (true) {
try {
System.out.println(i);
jTimer.setText(i + "Sec");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
if (i == 60) {
break;
}
}
}

// When this method is performed,
// The result indicates that The println(i) shows correctly as a timer
// but jTimer.setText() don't show at all.
// What's more During the playing time, Jbutton(Start) is clicked until it's finished completely.
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("START")) {
System.out.println("Counting");
setTimer();
}
}
}

我希望 JLabel(jTimer) 显示所有数字,但实际输出只是之前的数字。

最佳答案

由于 Swing 的单线程特性,您不能使用循环或 Thread.sleep 方法。

因此,UI 会被阻塞,直到循环完成后才会更新。因为这些更新是在 EDT(事件调度线程)的上下文中执行的,所以在更新 UI 组件时可以安全地使用它。

public class Game extends JFrame implements ActionListener {
private int count = 0;
// ...

public void setTimer() {
count = 0; // initialize variable for timer.
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTimer.setText(count + " Sec");
count++;
if(count == 60) {
((Timer)e.getSource()).stop();
}
}
});
timer.setInitialDelay(0);
timer.start();

// When the timer is over
System.out.println("done!");
}
}

关于java - 如何通过 setText 方法设置定时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55900042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com