更新JLabel中的时间?

huangapple go评论114阅读模式
英文:

Update time in JLabel?

问题

以下是翻译好的内容:

  1. public JLabel timeNow;
  2. public static Date dt = new Date(); // 当前时间
  3. public static int month = dt.getMonth(); // 获取当前月份
  4. public static int hours = dt.getHours(); // 获取小时
  5. public static int minute = dt.getMinutes();
  6. public static int second = dt.getSeconds();
  7. public static void main(String[] args) {
  8. GUI g = new GUI(); // GUI是类的名称
  9. g.Update();
  10. }
  11. public void Update() {
  12. while (1 == 1) {
  13. hours = dt.getHours();
  14. minute = dt.getMinutes();
  15. second = dt.getSeconds();
  16. String timeText = "当前时间:" + hours + ":" + minute + ":" + second;
  17. timeNow.setText(timeText);
  18. }
  19. }

注意:这段代码存在一个问题,即while (1 == 1)会导致无限循环,可能会导致程序无响应。建议使用合适的循环条件来避免这个问题。

英文:

So I would like to update the Time in my program. For that I used a JLabel. So when I put the text.setText("Example"); The JLabel doesnt update? Any Ideas why? (I just included every important part of the code, I dont get any Syntax error)

  1. public JLabel timeNow;
  2. public static Date dt = new Date(); // current time
  3. public static int month = dt.getMonth(); // gets the current month
  4. public static int hours = dt.getHours(); // gets hour of day
  5. public static int minute = dt.getMinutes();
  6. public static int second = dt.getSeconds();
  7. public static void main(String[] args) {
  8. GUI g = new GUI(); //GUI is the name of the class
  9. g.Update();
  10. }
  11. public void Update() {
  12. while(1 == 1) {
  13. hours = dt.getHours();
  14. minute = dt.getMinutes();
  15. second = dt.getSeconds();
  16. String timeText = "Aktuelle Zeit: " + hours + ":" + minute + ":" + second;
  17. timeNow.setText(timeText);
  18. }
  19. }

答案1

得分: 1

UI的更新应该在EventDispatch线程上进行,要实现这一点,请使用以下代码替换timeNow.setText(timeText)

  1. SwingUtilities.invokeLater(
  2. new Runnable()
  3. {
  4. public void run()
  5. {
  6. timeNow.setText(timeText);
  7. }
  8. });

正如@BeUndead在评论中所说,您没有更新dt对象,因此这样做仍然只会写入原始日期时间,您还需要获取新的日期时间。

一个更好的方法是使用Timer对象,在其中创建一个事件,您可以在其中更新UI。

英文:

Updates to the UI should happen on the EventDispatch thread, to achieve this replace timeNow.setText(timeText) with

  1. SwingUtilities.invokeLater(
  2. new Runnable()
  3. {
  4. public void run()
  5. {
  6. timeNow.setText(timeText);
  7. }
  8. });

As @BeUndead said in the comments, you're not updating the dt object, so doing this will still only write the original date time, you need to get a new date time as well.

A better way would be to use a Timer object which creates an event within which you update the UI.

答案2

得分: 1

更新Swing中的GUI应该在“EventDispatch”线程中进行:

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.SwingUtilities;
  4. import javax.swing.Timer;
  5. import javax.swing.WindowConstants;
  6. import java.awt.BorderLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. public class GUI extends JFrame implements ActionListener {
  13. private final JLabel timeLabel = new JLabel();
  14. private final DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  15. public GUI() {
  16. setLayout(new BorderLayout());
  17. add(timeLabel, BorderLayout.CENTER);
  18. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  19. setSize(300, 100);
  20. new Timer(500, this).start();
  21. }
  22. public static void main(String[] args) {
  23. SwingUtilities.invokeLater(() -> new GUI().setVisible(true));
  24. }
  25. @Override
  26. public void actionPerformed(ActionEvent event) {
  27. timeLabel.setText(df.format(new Date()));
  28. }
  29. }
英文:

To update GUI in Swing you should do it within EventDispatch thread:

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.SwingUtilities;
  4. import javax.swing.Timer;
  5. import javax.swing.WindowConstants;
  6. import java.awt.BorderLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.text.DateFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. public class GUI extends JFrame implements ActionListener {
  13. private final JLabel timeLabel = new JLabel();
  14. private final DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
  15. public GUI() {
  16. setLayout(new BorderLayout());
  17. add(timeLabel, BorderLayout.CENTER);
  18. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  19. setSize(300, 100);
  20. new Timer(500, this).start();
  21. }
  22. public static void main(String[] args) {
  23. SwingUtilities.invokeLater(() -> new GUI().setVisible(true));
  24. }
  25. @Override
  26. public void actionPerformed(ActionEvent event) {
  27. timeLabel.setText(df.format(new Date()));
  28. }
  29. }

huangapple
  • 本文由 发表于 2020年9月21日 23:03:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定