gpt4 book ai didi

java - 定时器/秒表 GUI

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:46 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的 Java 应用程序来计算时间,并能够停止和启动计时器。但是,标签不会更新,当我按下开始时,它会卡住。

你能帮我找出问题所在吗?

package random;

import javax.swing.JFrame;

public class Timer {
boolean shouldCount=false;
int int_sec=0;
int int_min=0;
int int_mil=0;
public static void main(String[] args) {
TimeFrame t = new TimeFrame();
JFrame f = new JFrame("Timer");
f.setSize(300,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.getContentPane().add(t);
f.setVisible(true);
}

public void count(){
TimeFrame t = new TimeFrame();
if(shouldCount){
long now = System.currentTimeMillis();
while(true){
if(System.currentTimeMillis()-now>=100){
now=System.currentTimeMillis();
String sec = Integer.toString(int_sec);
String min = Integer.toString(int_min);
String mil = Integer.toString(int_mil);
t.update(sec,int_sec,min,mil,int_mil);
int_mil++;
if(int_mil>9){
int_mil=0;
int_sec++;
if(int_sec>=60){
int_sec=1;
int_min++;
}
}
}
}
}
}
}

这是 TimeFrame.java

    package random;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimeFrame extends JPanel{
JLabel time = new JLabel("Time goes here", JLabel.CENTER);
Timer t = new Timer();
JButton pause = new JButton ("Pause");
JButton start = new JButton ("Start");
public TimeFrame(){

start.addActionListener(new starts());
pause.addActionListener(new starts());
add(time);
add(start);
add(pause);
}
public void update(String sec,int s, String min,String mil,int m){
if (s<=10){
sec="0"+sec;
}
System.out.println(min+":"+sec+","+mil);
time.setText(min+":"+sec+","+mil);

}
public class starts implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource() == start){
t.shouldCount=true;
}else{
t.shouldCount=false;
}
t.count();
}
}
}

最佳答案

问题是您的应用程序中只有一个线程。您应该至少有两个:一个用于将更新文本的 UI,另一个用于计算时间。

如果您只有一个线程,它会卡在 while(true) 循环中,而 Swing 永远不会更新 View 。

我使用两个线程重构了您的代码:

  1. 一个计数直到时间结束并更新字段以将时间保存在内存中

  2. 另一个使用 java.util.Timer#scheduleAtFixedRate() 方法,每 100 毫秒调用一次以更新 View 。

Timer.java(避免像 Java API 中那样命名类)

    public class Timer {

boolean shouldCount=false;
int int_sec=0;
int int_min=0;
int int_mil=0;

public Timer() {
}

public static void main(String[] args) {
TimeFrame t = new TimeFrame();
JFrame f = new JFrame("Timer");
f.setSize(300,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.getContentPane().add(new TimeFrame());
f.setVisible(true);
}

public void count(){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
long now = System.currentTimeMillis();
while(true){
if(shouldCount){
if(System.currentTimeMillis()-now>=100){
now=System.currentTimeMillis();
int_mil++;
if(int_mil>9){
int_mil=0;
int_sec++;
if(int_sec>=60){
int_sec=1;
int_min++;
}
}
}
}
}
}
});
thread.start();
}
}
}

TimeFrame(我宁愿称它为 TimePanel,因为它扩展了 JPanel)

public class TimeFrame extends JPanel{
JLabel time;
Timer t ;
JButton pause ;
JButton start ;
public TimeFrame(){

t= new Timer(this);

time = new JLabel("Time goes here", JLabel.CENTER);
pause = new JButton ("Pause");
start = new JButton ("Start");

start.addActionListener(new starts());
pause.addActionListener(new starts());
add(time);
add(start);
add(pause);

java.util.Timer updateTimer= new java.util.Timer();
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
t.update(int_sec,int_min,int_mil);
}
}, 0, 100);
}

public void update(int s, int minute,int m){
String sec = Integer.toString(s);
String min = Integer.toString(minute);
String mil = Integer.toString(m);
if (s<=10){
sec="0"+sec;
}

System.out.println(min+":"+sec+","+mil);
time.setText(min+":"+sec+","+mil);
}


public class starts implements ActionListener{
boolean firstTime=true;
public void actionPerformed(ActionEvent event){
if (firstTime){
t.count();
firstTime = false;
}
if(event.getSource() == start){
t.shouldCount=true;
}else{
t.shouldCount=false;
}
}
}

}

关于java - 定时器/秒表 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18926839/

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