java - 使用 ActionListener 更新时钟 - Java

标签 java timer jframe actionlistener

我想要一个用 Java 编写的数字时钟,显示时间和日期,并且冒号应该闪烁。但是,我无法让冒号眨眼。这是我的代码:

import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.util.*;
import java.text.*;

public class DigitalClock {

  public static void main(String[] arguments) {

    ClockLabel dateLable = new ClockLabel("date");
    ClockLabel timeLable = new ClockLabel("time");
    ClockLabel dayLable = new ClockLabel("day");

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame("Digital Clock");
    f.setSize(300,150);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(new GridLayout(3, 1));

    f.add(dateLable);
    f.add(timeLable);
    f.add(dayLable);

    f.getContentPane().setBackground(Color.black);

    f.setVisible(true);
  }
}

class ClockLabel extends JLabel implements ActionListener {
  String type;
  SimpleDateFormat sdf;
  public ClockLabel(String type) {
    this.type = type;
    setForeground(Color.green);
    Calendar calendar = Calendar.getInstance();
    int seconds = calendar.get(Calendar.SECOND);
    switch (type) {
      case "date" : sdf = new SimpleDateFormat("  MMMM dd yyyy");
                    setFont(new Font("sans-serif", Font.PLAIN, 12));
                    setHorizontalAlignment(SwingConstants.LEFT);
                    break;
      case "time" : if(seconds % 2 != 0) sdf = new SimpleDateFormat("hh:mm:ss a");
                    else sdf = new SimpleDateFormat("hh mm ss a");
                    setFont(new Font("sans-serif", Font.PLAIN, 40));
                    setHorizontalAlignment(SwingConstants.CENTER);
                    break;
      case "day"  : sdf = new SimpleDateFormat("EEEE  ");
                    setFont(new Font("sans-serif", Font.PLAIN, 16));
                    setHorizontalAlignment(SwingConstants.RIGHT);
                    break;
      default     : sdf = new SimpleDateFormat();
                    break;
    }
    Timer t = new Timer(1000, this);
    t.start();
  }

  public void actionPerformed(ActionEvent ae) {
      Date d = new Date();
    setText(sdf.format(d));
  }
}

如您所见,我有以下几行:

case "time" : if(seconds % 2 != 0) sdf = new SimpleDateFormat("hh:mm:ss a");
              else sdf = new SimpleDateFormat("hh mm ss a");

这样,当秒为奇数时,冒号可见,否则冒号不可见。

问题是,如果我启动程序并且当时第二个是奇数,那么冒号始终可见。我不明白为什么,因为第二个更改(时间更新),但冒号没有。

最佳答案

我认为你需要进行这张支票

if(seconds % 2 != 0) 
  sdf = new SimpleDateFormat("hh:mm:ss a");
else 
  sdf = new SimpleDateFormat("hh mm ss a");

actionPerformed方法内。这就是你可以每秒切换 : 的方式。

关于java - 使用 ActionListener 更新时钟 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45346748/

相关文章:

java - JButton 占据整个框架

java - 从 JTextArea 清除附加文本

java - 转换为 String 的首选方式

java - 我的计时器正在执行的方法一直失败

c++ - 如何在 C++ 中将回调函数指针传递给 epoll_event 结构

java - 在 Netbeans 7.0 中向 JFrame 添加背景图像时出现问题

java - 写 DAO : how to override or implements the interface methods in abstract class?

java - 从输入流读取并从数据创建特定实例

c - 在线程内实现计时器的最佳方法是什么?

java - 如何在 JFrame 上显示 JTable