Java如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘

小編給大家分享一下Java如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到陸良網(wǎng)站設(shè)計(jì)與陸良網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋陸良地區(qū)。

構(gòu)建:

Clock繼承 JFrame 為運(yùn)行頁(yè)面
ClockText 測(cè)試類(lèi) 創(chuàng)建 Clock 對(duì)象 

運(yùn)行效果:

Java如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘

具體實(shí)現(xiàn):

一、Clock類(lèi)

  • 四個(gè)JPnal 三個(gè)放時(shí)間 最后一個(gè)放日期

  • 放時(shí)間的三個(gè)JPnal 分別加入 地點(diǎn) 時(shí)間 按鈕

  • 最后一個(gè)按鈕添加日期

具體實(shí)現(xiàn)如下:

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));
 }
 
 // 關(guān)系
 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("北京時(shí)間");
 jLabelBeijing.setFont(placeFont);
 jPanelBeijing.add(jLabelBeijing);
 setWestPanel();
 JLabel jLabelNewYork = new JLabel("紐約時(shí)間");
 jLabelNewYork.setFont(placeFont);
 jPanelNewYork.add(jLabelNewYork);
 setCenterPanel();
 JLabel jLabelLondon = new JLabel("倫敦時(shí)間");
 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();
  // 轉(zhuǎn)換日期顯示格式
  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("北京繼續(xù)");
   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();
  // 轉(zhuǎn)換日期顯示格式
  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("紐約繼續(xù)");
   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(" 當(dāng)前日期: " + 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("倫敦繼續(xù)");
   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(" 當(dāng)前日期: " + date.format(new Date(timemillis)));
  }
 });
 timeAction.start();
 jPanelDate.add(jLabelDate);
 }
 
 private void jFrameClick(){
 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//設(shè)置不默認(rèn)關(guān)閉
 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, "確認(rèn)退出么?", "友情提示", 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
 
  }
 });
 }
}

二、創(chuàng)建ClockText類(lèi)用于測(cè)試

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

以上是“Java如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

標(biāo)題名稱:Java如何實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
標(biāo)題URL:http://muchs.cn/article12/jpehdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)電子商務(wù)、虛擬主機(jī)移動(dòng)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司