- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个Java秒表,其中我使用线程来连续监视所耗时。但我的代码遇到了问题。我的 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 行。
我的秒表停止工作,即即使按“开始”、“停止”或“重置”,时钟也始终显示“0:0:0”。此外,将第 123 行替换为“System.out.println()”以外的任何其他语句也会出现同样的问题。
最佳答案
我看到的两个主要问题:
setText(...)
,这是您不应该做的事情,而且紧密循环可能会完全卡住您的 Swing GUI 。 volatile
,以便其状态在所有线程中都相同。如果您需要在后台线程中更改 Swing 状态,请使用 SwingWorker 并使用发布/进程对进行更改。
此外,不要使用 ==
或 !=
比较字符串。请改用 equals(...)
或 equalsIgnoreCase(...)
方法。了解 ==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,方法检查两个字符串是否具有相同的字符以相同的顺序排列,这就是这里最重要的。所以而不是
else if (evt.getActionCommand() == "Stop") {
做
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();
}
}
}
}
}
关于java - Java 线程的未知行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42041166/
我有一个交叉表函数,我过去曾多次成功使用它,但现在它在最后转储所有数据,而不是将其旋转到输出表中。它似乎无法找到交叉表。我通过以下方式对其进行了研究; 如果 tablefunc 不存在则创建扩展; -
表1(客户表) Id, CustomerId, IsKnownCustomer,phonemacaddress 1, 空 0 00:9a:34:cf:a4 2, 004024 1 00:6f:64:c
知道为什么我总是收到这个烦人且无用的错误代码/描述吗? Failed to pull image myapidemodocker.azurecr.io/apidemo:v4.0: rpc error:
我正在进行 PHP 登录,并且之前可以正常工作,但我尝试使用户名功能不区分大小写,但此后代码一直无法正常工作。我删除了我添加的所有内容,以尝试使其不区分大小写,即 strtolower()。页面上显示
有人会帮助我提供有关此错误的任何可能信息吗?原因?登录?在哪里寻找/开始? Cannot use output buffering in output buffering display handl
我已经添加了这样的脚本 我在我的 test.js 中做了这个 var app = angular.module('MyApp', ['ngRoute']).config
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我有这个sql语句: selectAllUsersByCriteria = 连接.prepareStatement( “从用户那里选择*?=?” ); 下面的方法运行该语句: public Array
我有一个白色的 EditText,在 Android 3.1 及更高版本中,光标不显示(因为它也是白色的)。有关信息,我使用 android:background="@android:drawable
我正在尝试使用 Keras 实现深度学习模型。但是我有一个未知形状实现的问题。我一直在寻找类似的错误,但没有找到。 这是我的代码。 Xhome = dataset[:,32:62] Xaway = d
关注此introduction可以通过导入命名空间 System.Xml 来使用 XMLReader 类。在我的 Visual Studio 项目中,我使用 .NET 4.0,但 System.Xml
我有一个动态链接库的程序。该程序将函数指针传递给该库以执行。 但是 ubsan(Undefined Behavior Sanitizer)指定指针位于错误的函数类型上。那只会发生 如果回调函数有一个类
我正在尝试在我的 Swift SpriteKit 应用程序中使用 AVAudioSession。我遇到了奇怪的“未声明类型”问题。例如…… import AVFoundation var audioS
如果在编译期间(在实际编译和运行程序之前)其参数之一的值已知/未知,如何专门化模板函数? 我还不知道怎么做。 想法 1: #include #include int main(void){
我看到一些人的代码是这样的: while (!(baseType == typeof(Object))) { .... baseType = baseType.BaseType;
我正在尝试使用 GoColly 框架获取所有 HREF 链接,但是只允许任何域的 url 为根 URL 或子域(否路径)。我已经注释掉了我的 REGEXP。文件扩展名没有事情。我只是在“/”之后不想要
我有一个包含多个实体的数据库,特别是 Book 和 User。它们之间存在这样的 ManyToMany 关系: 书: @Entity @Table(name = "Books") public cla
如果我将范围的初始部分设置为 Range("A:A"),如何确保将整行传递给排序? 数据 id、fname、mname、lname、后缀、状态、位置、时区 通过在 id 中搜索起点和终点来选择范围。
我对kubernetes很陌生,而对于docker来说就不那么多了。 我一直在研究示例,但是我对自动缩放器(似乎无法缩放)感到困惑。 我在这里通过示例https://kubernetes.io/doc
我在 ChildWindow 中使用 SL Toolkit 5 中的 BusyIndicator 控件。 在某些解决方案中,它可以工作,但在其他解决方案中,使用完全相同的代码(至少看起来),我在运
我是一名优秀的程序员,十分优秀!