如何使用定时器创建时钟? [英] How do I create a clock using Timer?
问题描述
我正在尝试使用 ActionListener 和 actionPerformed 创建一个使用计时器自动移动的七段显示.
I am trying to create a seven segment display that automatically moves using the timer with the use of the ActionListener and actionPerformed.
我认为如果我使用 for, if 语句,它会自动从 0 循环到 2 并为每个数字段设置背景颜色.但是,当我显示它时,它一直停留在只显示零并且不会从那里开始计数.
I thought that if I use the for, if statement it would automatically loop from 0 to 2 and set the Background color for each segment of the numbers. However, when I display it, it is stuck on just displaying zero and will not count up from there.
任何人都可以帮助我解决我做错了什么,这使得它停留在零上?
Can anyone help me on what I am doing wrong, that makes this stuck on zero?
这是我现在使用 JFrame 的编程.
Here is the programming I have now using JFrame.
import javax.swing.Timer;
public class SevenSegment extends JFrame {
Timer timer = new Timer(100, null);
public SevenSegment() {
timer.start();
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 3; i++) {
if(i == 0) {
lblNumberOne.setBackground(Color.red);
lblNumberTwo.setBackground(Color.red);
lblNumberThree.setBackground(Color.red);
lblNumberFour.setBackground(Color.red);
lblNumberFive.setBackground(Color.red);
lblNumberSix.setBackground(Color.red);
}
else if(i == 1) {
lblNumberTwo.setBackground(Color.red);
lblNumberThree.setBackground(Color.red);
}
else if(i == 2) {
lblNumberOne.setBackground(Color.red);
lblNumberTwo.setBackground(Color.red);
}
}
}
});
}
}
推荐答案
这是您可以制作的最简单的 Swing GUI 时钟.
Here's about as simple a Swing GUI clock as you can make.
我这样做是为了向您展示启动所有 Swing GUI 的一般方法.Oracle 有一个有用的教程,Creating a GUI with JFC/Swing,这将向您展示如何创建各种 Swing GUI.
I did this so I could show you a general way to start all of your Swing GUIs. Oracle has a helpful tutorial, Creating a GUI With JFC/Swing, that will show you how to create all kinds of Swing GUIs.
我们通过调用 SwingUtilities
invokeLater
方法启动所有 Swing GUI.此方法确保在 事件调度线程上创建和执行所有 Swing 组件.
We start all Swing GUIs with a call to the SwingUtilities
invokeLater
method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.
我们总是创建一个 JPanel
来放置我们的 Swing 组件.我们添加到 JFrame
的唯一 Swing 组件是 JPanel
或 JScrollPane
.这允许我们将 JFrame
的创建与视图的其余部分分开.JFrame
代码对于所有 Swing 应用程序几乎相同.唯一的区别是您添加到 JFrame
的 JPanels
.
We always create a JPanel
to put our Swing components on. The only Swing component that we add to a JFrame
is a JPanel
or a JScrollPane
. This allows us to separate the creation of a JFrame
from the rest of the view. The JFrame
code is nearly identical for all Swing applications. The only difference is the JPanels
you add to the JFrame
.
JFrame
代码必须按特定顺序调用.这是我用于 Swing 应用程序的顺序.
The JFrame
code must be called in a specific order. This is the order I use for my Swing applications.
JPanel
代码在一个单独的段落中,以保持整体整洁.我喜欢把事情分开,这样我就可以一次专注于 GUI 的一小部分.
The JPanel
code is in a separate paragraph for general tidiness. I like to keep things separate so I can focus on one small part of the GUI at a time.
updateClockLabel
方法的存在是因为我需要在创建 JPanel
时执行一次代码,之后每秒执行五次,以实际更新 <代码>JLabel.
The updateClockLabel
method exists because I need to execute the code one time when I'm creating the JPanel
, and five times a second thereafter, to actually update the JLabel
.
顺便说一句,我没有一次性写完所有这些代码(全部 64 行).我写了一点,测试了很多.我第一次写的代码不正确,我的意思不仅仅是打错字.
By the way, I didn't write all this code (all 64 lines) in one shot. I wrote a little, tested a lot. My code was not correct the first time I wrote it, and I don't just mean making typos.
这是完整的可运行代码.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SimpleClock implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleClock());
}
private JLabel clockLabel;
@Override
public void run() {
JFrame frame = new JFrame("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
Timer timer = new Timer(200, new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
updateClockLabel();
}
});
timer.start();
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 45, 5, 45));
clockLabel = new JLabel(" ");
clockLabel.setFont(panel.getFont().deriveFont(Font.BOLD, 72f));
updateClockLabel();
panel.add(clockLabel);
return panel;
}
public void updateClockLabel() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm:ss a");
String timeDisplay = LocalTime.now().format(formatter);
clockLabel.setText(timeDisplay);
}
}
这篇关于如何使用定时器创建时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!