java實現時鐘方法匯總

來源:互聯網
上載者:User

標籤:listener   eth   npe   vax   version   panel   logs   exception   upd   

import java.awt.Dimension;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;//第一種比較推薦:public class TimeFrame extends JFrame{ /* * Variables  */ private JPanel timePanel; private JLabel timeLabel; private JLabel displayArea; private String DEFAULT_TIME_FORMAT = "HH:mm:ss"; private String time; private int ONE_SECOND = 1000;   public TimeFrame() { timePanel = new JPanel(); timeLabel = new JLabel("CurrentTime: "); displayArea = new JLabel();   configTimeArea();   timePanel.add(timeLabel); timePanel.add(displayArea); this.add(timePanel); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(new Dimension(200,70)); this.setLocationRelativeTo(null); }   /** * This method creates a timer task to update the time per second */ private void configTimeArea() { Timer tmr = new Timer(); tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND); }   /** * Timer task to update the time display area * */ protected class JLabelTimerTask extends TimerTask{ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT); @Override public void run() {  time = dateFormatter.format(Calendar.getInstance().getTime());  displayArea.setText(time); } }   public static void main(String arg[]) { TimeFrame timeFrame=new TimeFrame(); timeFrame.setVisible(true);  } }

  

import java.awt.Dimension;import java.text.SimpleDateFormat;import java.util.Calendar;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;//第二種 public class DTimeFrame2 extends JFrame implements Runnable{ private JFrame frame; private JPanel timePanel; private JLabel timeLabel; private JLabel displayArea; private String DEFAULT_TIME_FORMAT = "HH:mm:ss"; private int ONE_SECOND = 1000;   public DTimeFrame2() { timePanel = new JPanel(); timeLabel = new JLabel("CurrentTime: "); displayArea = new JLabel();   timePanel.add(timeLabel); timePanel.add(displayArea); this.add(timePanel); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(new Dimension(200,70)); this.setLocationRelativeTo(null); } public void run() { while(true) {  SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT);  displayArea.setText(dateFormatter.format(     Calendar.getInstance().getTime()));  try  {  Thread.sleep(ONE_SECOND);   }  catch(Exception e)  {  displayArea.setText("Error!!!");  } }  }    public static void main(String arg[]) { DTimeFrame2 df2=new DTimeFrame2(); df2.setVisible(true);   Thread thread1=new Thread(df2); thread1.start();  } }

  

import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.TimeZone;import java.util.Timer;import java.util.TimerTask;import javax.swing.DefaultComboBoxModel;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;//第三種:多國時鐘實現public class WorldTimeFrame extends JFrame{ /** *  */ private static final long serialVersionUID = 4782486524987801209L;   private String time; private JPanel timePanel; private TimeZone timeZone;//時區 private JComboBox zoneBox; private JLabel displayArea;   private int ONE_SECOND = 1000; private String DEFAULT_FORMAT = "EEE d MMM, HH:mm:ss";   public WorldTimeFrame() { zoneBox = new JComboBox(); timePanel = new JPanel(); displayArea = new JLabel(); timeZone = TimeZone.getDefault();   zoneBox.setModel(new DefaultComboBoxModel(TimeZone.getAvailableIDs()));   zoneBox.addActionListener(new ActionListener(){  @Override  public void actionPerformed(ActionEvent e) {  updateTimeZone(TimeZone.getTimeZone((String) zoneBox.getSelectedItem()));  }    });   configTimeArea();   timePanel.add(displayArea); this.setLayout(new BorderLayout()); this.add(zoneBox, BorderLayout.NORTH); this.add(timePanel, BorderLayout.CENTER); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); pack(); }   /** * This method creates a timer task to update the time per second */ private void configTimeArea() { Timer tmr = new Timer(); tmr.scheduleAtFixedRate(new JLabelTimerTask(),new Date(), ONE_SECOND); }   /** * Timer task to update the time display area * */ public class JLabelTimerTask extends TimerTask{ SimpleDateFormat dateFormatter = new SimpleDateFormat(DEFAULT_FORMAT, Locale.ENGLISH); @Override public void run() {  dateFormatter.setTimeZone(timeZone);  time = dateFormatter.format(Calendar.getInstance().getTime());  displayArea.setText(time); } }   /** * Update the timeZone * @param newZone */ public void updateTimeZone(TimeZone newZone) { this.timeZone = newZone; }   public static void main(String arg[]) { new WorldTimeFrame(); } }

  

java實現時鐘方法匯總

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.