JLabel 中的更新时间?
Update time in JLabel?
所以我想更新我程序中的时间。为此,我使用了 JLabel。所以当我把 text.setText("Example"); JLabel 不更新?任何想法为什么? (我只是包含了代码的每个重要部分,我没有遇到任何语法错误)
public JLabel timeNow;
public static Date dt = new Date(); // current time
public static int month = dt.getMonth(); // gets the current month
public static int hours = dt.getHours(); // gets hour of day
public static int minute = dt.getMinutes();
public static int second = dt.getSeconds();
public static void main(String[] args) {
GUI g = new GUI(); //GUI is the name of the class
g.Update();
}
public void Update() {
while(1 == 1) {
hours = dt.getHours();
minute = dt.getMinutes();
second = dt.getSeconds();
String timeText = "Aktuelle Zeit: " + hours + ":" + minute + ":" + second;
timeNow.setText(timeText);
}
}
对 UI 的更新应该发生在 EventDispatch 线程上,为此将 timeNow.setText(timeText)
替换为
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
timeNow.setText(timeText);
}
});
正如@BeUndead 在评论中所说,您没有更新 dt
对象,因此这样做仍然只会写入原始日期时间,您还需要获取新的日期时间。
更好的方法是使用 Timer
对象创建一个事件,您可以在该事件中更新 UI。
要在 Swing 中更新 GUI,您应该在 EventDispatch
线程中进行:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GUI extends JFrame implements ActionListener {
private final JLabel timeLabel = new JLabel();
private final DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
public GUI() {
setLayout(new BorderLayout());
add(timeLabel, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300, 100);
new Timer(500, this).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GUI().setVisible(true));
}
@Override
public void actionPerformed(ActionEvent event) {
timeLabel.setText(df.format(new Date()));
}
}
所以我想更新我程序中的时间。为此,我使用了 JLabel。所以当我把 text.setText("Example"); JLabel 不更新?任何想法为什么? (我只是包含了代码的每个重要部分,我没有遇到任何语法错误)
public JLabel timeNow;
public static Date dt = new Date(); // current time
public static int month = dt.getMonth(); // gets the current month
public static int hours = dt.getHours(); // gets hour of day
public static int minute = dt.getMinutes();
public static int second = dt.getSeconds();
public static void main(String[] args) {
GUI g = new GUI(); //GUI is the name of the class
g.Update();
}
public void Update() {
while(1 == 1) {
hours = dt.getHours();
minute = dt.getMinutes();
second = dt.getSeconds();
String timeText = "Aktuelle Zeit: " + hours + ":" + minute + ":" + second;
timeNow.setText(timeText);
}
}
对 UI 的更新应该发生在 EventDispatch 线程上,为此将 timeNow.setText(timeText)
替换为
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
timeNow.setText(timeText);
}
});
正如@BeUndead 在评论中所说,您没有更新 dt
对象,因此这样做仍然只会写入原始日期时间,您还需要获取新的日期时间。
更好的方法是使用 Timer
对象创建一个事件,您可以在该事件中更新 UI。
要在 Swing 中更新 GUI,您应该在 EventDispatch
线程中进行:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class GUI extends JFrame implements ActionListener {
private final JLabel timeLabel = new JLabel();
private final DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
public GUI() {
setLayout(new BorderLayout());
add(timeLabel, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(300, 100);
new Timer(500, this).start();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GUI().setVisible(true));
}
@Override
public void actionPerformed(ActionEvent event) {
timeLabel.setText(df.format(new Date()));
}
}