java - Java GUI秒表不显示时间
问题描述
我已经在这个秒表应用程序上工作了很长时间,但我遇到了一些问题。
问题
- 秒表未正确启动可能是由于时钟逻辑
- 秒表没有在我的 Java 程序上正确显示数字
如果有人能帮助解释我的程序中的缺陷并告诉我为什么它没有按照我希望的方式工作,那就太好了。需要和赞赏很多帮助,所以这是我的代码。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Clock {
private static void createWindow() {
// Important
final int windowWidth = 800; // Window width
final int windowHeight = 300; // Window height
// Clock variables
boolean clockRunning = true; // When clock is running, this will be true
int milliseconds = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;
// Create JFrame
JFrame frame = new JFrame(); // JFrame object
// Create timer text
JLabel timer = new JLabel("00:00:00");
timer.setText("00:00:00");
timer.setBounds(355, 100, 100, 40); // Button position
// JButtons
JButton startTimer = new JButton("Start the timer"); // Start timer button
JButton stopTimer = new JButton("Stop the timer"); // Stop timer button
// Event listeners
startTimer.addActionListener(new ActionListener() // Start timer
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Timer has started");
}
});
stopTimer.addActionListener(new ActionListener() // Stop timer
{
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("Timer has stoped");
}
});
// Clock logic
if (clockRunning = true) {
milliseconds = 0;
seconds++;
}
if(milliseconds > 1000) {
milliseconds=0;
seconds++;
}
if(seconds > 60) {
milliseconds=0;
seconds=0;
minutes++;
}
if(minutes > 60) {
milliseconds=0;
minutes=0;
hours++;
}
timer.setText(" : " + seconds); // Milliseconds
timer.setText(" : " + milliseconds); // Seconds
timer.setText(" : " + minutes); // Minutes
timer.setText("" + hours); // Hours
// JButton Settings
startTimer.setBounds(10, 100, 200, 30); // Button position
stopTimer.setBounds(570, 100, 200, 30); // Button position
// Frame Settings
frame.setSize(windowWidth, windowHeight); // Window size
frame.setLayout(null); // Frame position
// Add
frame.add(startTimer);
frame.add(stopTimer);
frame.add(timer);
// Frame settings
frame.setVisible(true); // Make frame visible
frame.setResizable(false); // Disables maximize
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allows the window to be closed
}
public static void main(String[] args) {
createWindow();
}
}
解决方案
秒表没有在我的 Java 程序上正确显示数字
timer.setText(" : " + seconds); // Milliseconds
timer.setText(" : " + milliseconds); // Seconds
timer.setText(" : " + minutes); // Minutes
timer.setText("" + hours); // Hours
该setText(…)
方法替换现有文本。
所以上面的代码是一样的:
timer.setText("" + hours); // Hours
秒表未正确启动可能是由于时钟逻辑
对于秒表,您将需要使用摆动计时器,以便您可以以指定的时间间隔更新文本。
请参阅:程序在 Thread.sleep() 和 Timer 期间冻结,了解使用 Timer 的基础知识。
推荐阅读
- java - Spring没有在测试中调用@Bean方法
- angularjs - 保存动态创建的输入值
- c++ - Xcode c/c++ 未定义符号
- pdo - PDO MySQL 区分大小写,即使使用 utf8_general_ci
- python-3.x - 我如何向特定频道发送消息 [Discord.py]
- java - 遍历包含数组的 json 值以选择图像字符串
- javascript - 对象未被读取 node.js
- python-3.x - python3和libre office calc - 设置列宽
- sql-server - SQL Server - 非唯一聚集索引优化
- vb.net - 无法在 DLL 库中打开表单