Java中怎么实现动态数字时钟
创始人
2024-12-24 16:02:13
0次

一、Clock类

四个JPnal 三个放时间 最后一个放日期  放时间的三个JPnal 分别加入 地点 时间 按钮  最后一个按钮添加日期

具体实现如下:

public class Clock extends JFrame { private JPanel jPanelBeijing; private JPanel jPanelNewYork; private JPanel jPanelLondom; private JPanel jPanelDate;  private boolean BeijingThreadFlag_IsStart = true; private boolean NewYorkThreadFlag_IsStart = true; private boolean LondonThreadFlag_IsStart = true;  public Clock() { // TODO Auto-generated constructor stub jPanelBeijing = new JPanel(); jPanelNewYork = new JPanel(); jPanelLondom = new JPanel(); jPanelDate = new JPanel();  iniRelations(); iniLayout(); jFrameClick();  setVisible(true); setSize(480, 225); setLocationRelativeTo(null); }  private void iniLayout() { jPanelBeijing.setLayout(new GridLayout(3, 1)); jPanelNewYork.setLayout(new GridLayout(3, 1)); jPanelLondom.setLayout(new GridLayout(3, 1)); }  // 关系 private void iniRelations() { this.add(BorderLayout.WEST, jPanelBeijing); this.add(BorderLayout.CENTER, jPanelNewYork); this.add(BorderLayout.EAST, jPanelLondom); this.add(BorderLayout.SOUTH, jPanelDate); Font placeFont = new Font("楷体", Font.BOLD, 36); JLabel jLabelBeijing = new JLabel("北京时间"); jLabelBeijing.setFont(placeFont); jPanelBeijing.add(jLabelBeijing); setWestPanel(); JLabel jLabelNewYork = new JLabel("纽约时间"); jLabelNewYork.setFont(placeFont); jPanelNewYork.add(jLabelNewYork); setCenterPanel(); JLabel jLabelLondon = new JLabel("伦敦时间"); jLabelLondon.setFont(placeFont); jPanelLondom.add(jLabelLondon); setEastPanel(); setDatePanel(); }  private void setWestPanel() { // add time for SouthPanel JLabel jLabelTime = new JLabel("加载中."); jLabelTime.setFont(new Font("宋体", Font.BOLD, 30)); Timer timeAction = new Timer(1000, new ActionListener() {   public void actionPerformed(ActionEvent e) {  long timemillis = System.currentTimeMillis();  // 转换日期显示格式  SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");  jLabelTime.setText(time.format(new Date(timemillis)));  } }); timeAction.start(); jPanelBeijing.add(jLabelTime);  Button button = new Button("北京暂停"); button.addActionListener(new ActionListener() {   @Override  public void actionPerformed(ActionEvent e) {  // TODO Auto-generated method stub  if (BeijingThreadFlag_IsStart) {   timeAction.stop();   button.setLabel("北京继续");   BeijingThreadFlag_IsStart = false;  } else {   timeAction.start();   button.setLabel("北京暂停");   BeijingThreadFlag_IsStart = true ;  }  } }); jPanelBeijing.add(button); }  private void setCenterPanel() { // add time for SouthPanel JLabel jLabelTime = new JLabel("加载中."); jLabelTime.setFont(new Font("宋体", Font.BOLD, 30)); Timer timeAction = new Timer(1000, new ActionListener() {   public void actionPerformed(ActionEvent e) {  long timemillis = System.currentTimeMillis();  // 转换日期显示格式  SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");  jLabelTime.setText(time.format(new Date(timemillis - 13 * 60 * 60 * 1000)));  } }); timeAction.start(); jPanelNewYork.add(jLabelTime);  Button button = new Button("纽约暂停"); button.addActionListener(new ActionListener() {   @Override  public void actionPerformed(ActionEvent e) {  // TODO Auto-generated method stub  if (NewYorkThreadFlag_IsStart) {   timeAction.stop();   button.setLabel("纽约继续");   NewYorkThreadFlag_IsStart = false;  } else {   timeAction.start();   button.setLabel("纽约暂停");   NewYorkThreadFlag_IsStart = true ;  }  } }); jPanelNewYork.add(button); }  private void setEastPanel() { // add time for SouthPanel // JLabel jLabelDate = new JLabel("Date"); JLabel jLabelTime = new JLabel("加载中."); jLabelTime.setFont(new Font("宋体", Font.BOLD, 30)); Timer timeAction = new Timer(1000, new ActionListener() {   public void actionPerformed(ActionEvent e) {  long timemillis = System.currentTimeMillis();  // SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd  // 日 ");  // jLabelDate.setText(" 当前日期: " + date.format(new  // Date(timemillis)));  SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");  jLabelTime.setText(time.format(new Time(timemillis - 8 * 60 * 60 * 1000)));  } }); timeAction.start(); jPanelLondom.add(jLabelTime);  Button button = new Button("伦敦暂停"); button.addActionListener(new ActionListener() {   @Override  public void actionPerformed(ActionEvent e) {  // TODO Auto-generated method stub  if (LondonThreadFlag_IsStart) {   timeAction.stop();   button.setLabel("伦敦继续");   LondonThreadFlag_IsStart = false;  } else {   timeAction.start();   button.setLabel("伦敦暂停");   LondonThreadFlag_IsStart = true ;  }  } }); jPanelLondom.add(button); // jPanelLondom.add(jLabelDate); }  private void setDatePanel() { // add time for SouthPanel JLabel jLabelDate = new JLabel("加载中."); Timer timeAction = new Timer(1000, new ActionListener() {   public void actionPerformed(ActionEvent e) {  long timemillis = System.currentTimeMillis();   SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 ");   jLabelDate.setText(" 当前日期: " + date.format(new Date(timemillis)));  } }); timeAction.start(); jPanelDate.add(jLabelDate); }  private void jFrameClick(){ setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//设置不默认关闭 addWindowListener(new WindowListener() {   @Override  public void windowOpened(WindowEvent e) {  // TODO Auto-generated method stub   }   @Override  public void windowIconified(WindowEvent e) {  // TODO Auto-generated method stub   }   @Override  public void windowDeiconified(WindowEvent e) {  // TODO Auto-generated method stub   }   @Override  public void windowDeactivated(WindowEvent e) {  // TODO Auto-generated method stub   }   @Override  public void windowClosing(WindowEvent e) {  // TODO Auto-generated method stub  int x = JOptionPane.showConfirmDialog(null, "确认退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION,   JOptionPane.WARNING_MESSAGE);  if (x == 0) {   System.exit(0);  }  }   @Override  public void windowClosed(WindowEvent e) {  // TODO Auto-generated method stub  }   @Override  public void windowActivated(WindowEvent e) {  // TODO Auto-generated method stub   } }); }}

二、创建ClockText类用于测试

public class ClockText{ public static void main(String[] args) { new Clock(); }}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...