Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ActionListener-Java更新时钟_Java_Timer_Jframe_Actionlistener - Fatal编程技术网

使用ActionListener-Java更新时钟

使用ActionListener-Java更新时钟,java,timer,jframe,actionlistener,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; i

我想要一个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
方法中。这就是你每秒钟打开:的方法。

我想你需要把这张支票

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

actionPerformed
方法中。这就是每秒钟切换:的方法。

如果在调试模式下运行项目,您将了解switch case语句只运行一次(启动时)。根据上述实现,这是正常的,因为逻辑被放置在构造函数中,该构造函数仅称为树时间


简而言之,您需要从根本上更改此实现。

如果您在调试模式下运行项目,您将了解switch case语句只运行一次(在启动时)。根据上述实现,这是正常的,因为逻辑被放置在构造函数中,该构造函数仅称为树时间


简而言之,您需要彻底更改此实现。

在ClockLabel的构造函数中,启动时只需设置一次SimpleDataFormat。您应该在actionPerformed()中切换格式顺便说一下,这些日期时间类现在是遗留的。签出java.time类、ThreeTen Backport项目和ThreeTenABP项目。在ClockLabel的构造函数中,启动时只需设置一次SimpleDataFormat。您应该在actionPerformed()中切换格式顺便说一下,这些日期时间类现在是遗留的。签出java.time类、ThreeTen Backport项目和ThreeTenABP项目。