我正在尝试实现一个Java Stop Watch,其中我使用了一个Thread来连续监视时间。但我在我的代码中遇到了问题。 我的Java代码是 -
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.*;
public class Prac2_StopWatch implements ActionListener{
JFrame Time;
JLabel TimeLabel;
JButton StartStop;
JButton Reset;
StopWatch stThread;
String prefix="<html><h1><body>";
String suffix="</h1></body></html>";
StopWatch myControl;
Prac2_StopWatch()
{
Time = new JFrame();
Time.setSize(275,275);
Time.setTitle("Stop Watch");
Time.setLayout(new GridBagLayout());
GridBagConstraints gridConst = new GridBagConstraints();
TimeLabel = new JLabel();
TimeLabel.setText(prefix+"0:0:0"+suffix);
myControl = new StopWatch(TimeLabel);
myControl.start();
StartStop = new JButton();
Reset = new JButton();
StartStop.setActionCommand("Start");
StartStop.setText("Start");
Reset.setText("Reset");
StartStop.addActionListener(this);
Reset.addActionListener(this);
gridConst.gridx=0;
gridConst.gridy=0;
Time.add(StartStop,gridConst);
gridConst.gridx=1;
gridConst.gridy=0;
Time.add(Reset,gridConst);
gridConst.gridx=0;
gridConst.gridy=1;
Time.add(TimeLabel,gridConst);
Time.setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand()=="Start")
{
StartStop.setActionCommand("Stop");
StartStop.setText("Stop");
if(myControl.curMil==-1)
myControl.curMil=Calendar.getInstance().getTimeInMillis();
else
myControl.curMil=Calendar.getInstance().getTimeInMillis()- myControl.diff;
myControl.count=true;
}
else if(evt.getActionCommand()=="Stop")
{
StartStop.setActionCommand("Start");
StartStop.setText("Start");
myControl.count=false;
}
else if(evt.getActionCommand()=="Reset")
{
StartStop.setActionCommand("Start");
StartStop.setText("Start");
myControl.count=false;
myControl.curMil=-1;
TimeLabel.setText(prefix+"0:0:0"+suffix);
}
}
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
new Prac2_StopWatch();
}
});
}
}
class StopWatch extends Thread
{
long curMil;
long diff;
JLabel TimeLabel;
boolean count;
String prefix="<html><body><h1>";
String suffix="</h1></body></html>";
StopWatch(JLabel TimeLabel)
{
this.TimeLabel = TimeLabel;
this.count=false;
this.curMil=-1;
}
public void run()
{
while(true){
System.out.print("Commenting this line will make stop watch useless");
if(count){
diff= Calendar.getInstance().getTimeInMillis() - curMil;
TimeLabel.setText(prefix+((diff/60000)%60)+" : "+((diff/1000)%60)+" : "+(diff%1000)+suffix);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
现在的问题是,当我对语句进行注释时,在StopWatch类的run方法中 -
System.out.println("Commenting this line will make stop watch useless");
第123行我的秒表停止工作,即时钟总是显示&#34; 0:0:0&#34;即使按下“开始”,“停止&#39;或者&#39;重置&#39;。此外,将行123替换为除了System.out.println()&#39;以外的任何其他语句。有同样的问题。
答案 0 :(得分:3)
我看到两个主要问题:
setText(...)
来自您的后台线程,这是您不应该做的事情,并且通过紧密循环可以完全冻结您的Swing GUI。 volatile
,以便其状态在所有线程中都相同。如果需要在后台线程中进行Swing状态更改,请使用SwingWorker并使用发布/进程对进行更改。
另外,请勿使用==
或!=
比较字符串。请改用equals(...)
或equalsIgnoreCase(...)
方法。了解==
检查两个对象引用是否相同而不是您感兴趣的内容。另一方面,这些方法检查两个字符串是否具有相同的字符以相同的顺序,这是重要的。而不是
else if (evt.getActionCommand() == "Stop") {
DO
else if (evt.getActionCommand().equalsIgnoreCase("Stop")) {
如,
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.*;
public class Prac2_StopWatch implements ActionListener {
JFrame time;
JLabel timeLabel;
JButton startStop;
JButton reset;
StopWatch stThread;
String prefix = "<html><h1><body>";
String suffix = "</h1></body></html>";
StopWatch myControl;
Prac2_StopWatch() {
time = new JFrame();
time.setSize(275, 275);
time.setTitle("Stop Watch");
time.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
time.setLayout(new GridBagLayout());
GridBagConstraints gridConst = new GridBagConstraints();
timeLabel = new JLabel();
timeLabel.setText(prefix + "00:00:000" + suffix);
myControl = new StopWatch(timeLabel);
new Thread(myControl).start(); // StopWatch should implement Runnable
// myControl.start();
startStop = new JButton();
reset = new JButton();
startStop.setActionCommand("Start");
startStop.setText("Start");
reset.setText("Reset");
startStop.addActionListener(this);
reset.addActionListener(this);
gridConst.gridx = 0;
gridConst.gridy = 0;
time.add(startStop, gridConst);
gridConst.gridx = 1;
gridConst.gridy = 0;
time.add(reset, gridConst);
gridConst.gridx = 0;
gridConst.gridy = 1;
time.add(timeLabel, gridConst);
time.setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equalsIgnoreCase("Start")) {
startStop.setActionCommand("Stop");
startStop.setText("Stop");
if (myControl.getCurMil() == -1)
myControl.setCurMil(Calendar.getInstance().getTimeInMillis());
else
myControl.setCurMil(Calendar.getInstance().getTimeInMillis() - myControl.getDiff());
myControl.count = true;
} else if (evt.getActionCommand().equalsIgnoreCase("Stop")) {
startStop.setActionCommand("Start");
startStop.setText("Start");
myControl.count = false;
} else if (evt.getActionCommand().equalsIgnoreCase("Reset")) {
startStop.setActionCommand("Start");
startStop.setText("Start");
myControl.count = false;
myControl.setCurMil(-1);
timeLabel.setText(prefix + "00:00:000" + suffix);
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Prac2_StopWatch();
}
});
}
}
class StopWatch implements Runnable {
private long curMil;
private long diff;
private JLabel timeLabel;
// *************** this is key ****************
volatile boolean count; // !! *********************
String prefix = "<html><body><h1>";
String suffix = "</h1></body></html>";
StopWatch(JLabel TimeLabel) {
this.timeLabel = TimeLabel;
this.count = false;
this.curMil = -1;
}
public long getCurMil() {
return curMil;
}
public void setCurMil(long curMil) {
this.curMil = curMil;
}
public long getDiff() {
return diff;
}
public void run() {
while (true) {
// System.out.println("Commenting this line will make stop watch useless");
if (count) {
diff = Calendar.getInstance().getTimeInMillis() - curMil;
// make Swing changes **on** the event thread only
SwingUtilities.invokeLater(() -> {
int mSec = (int) (diff % 1000);
int sec = (int) ((diff / 1000) % 60);
int min = (int) ((diff / (60 * 1000)) % 60);
String text = String.format("%s%02d:%02d:%03d%s", prefix, min, sec, mSec, suffix);
timeLabel.setText(text);
});
try {
Thread.sleep(1); // ** actually 1 is OK **
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
答案 1 :(得分:3)
移动
if(count){
diff= Calendar.getInstance().getTimeInMillis() - curMil;
TimeLabel.setText(prefix+((diff/60000)%60)+":"+((diff/1000)%60)+" : "+(diff%1000)+suffix);
}
在try-catch
区块内。
附注:
JFrame
窗口关闭时,您的程序不会停止。使用
Time.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
在java中比较字符串的正确方法是
if("string1".equals("string2")) {
//do stuff
}