Advertisement
bojjenclon

Clock.java

Mar 11th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package dalydesigns.swing;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ItemEvent;
  5.  
  6. import java.util.Date;
  7. import java.util.TimeZone;
  8. import java.text.SimpleDateFormat;
  9.  
  10. import java.awt.BorderLayout;
  11. import java.awt.Color;
  12. import java.awt.Dimension;
  13. import java.awt.Font;
  14. import java.awt.Toolkit;
  15.  
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.ItemListener;
  18.  
  19. import javax.swing.Timer;
  20. import javax.swing.BorderFactory;
  21. import javax.swing.JFrame;
  22. import javax.swing.JLabel;
  23. import javax.swing.JPanel;
  24. import javax.swing.JComboBox;
  25.  
  26. public class Clock extends JFrame {
  27.     private Toolkit toolkit;
  28.     private TimeZone tz;
  29.     private SimpleDateFormat format;
  30.     private JLabel time;
  31.     private JComboBox timezones;
  32.    
  33.     public Clock() {
  34.         setTitle( "Digital Clock" );
  35.        
  36.         tz = TimeZone.getDefault();
  37.        
  38.         format = new SimpleDateFormat( "h:m:s a" );
  39.         format.setTimeZone( tz );
  40.        
  41.         JPanel main = new JPanel();
  42.         main.setLayout( new BorderLayout( 10, 10 ) );
  43.        
  44.         time = new JLabel( format.format( new Date() ) );
  45.         time.setFont( new Font( "Courier New", Font.BOLD, 14 ) );
  46.         time.setHorizontalAlignment( JLabel.CENTER );
  47.         time.setForeground( new Color( 50, 50, 25 ) );
  48.  
  49.         timezones = new JComboBox( TimeZone.getAvailableIDs() );
  50.         timezones.setSelectedIndex( -1 );
  51.         for( int i = 0; i < timezones.getItemCount(); i++ ) {
  52.             if( timezones.getItemAt( i ).toString().equals( tz.getID() ) ) {
  53.                 timezones.setSelectedIndex( i );
  54.                
  55.                 break;
  56.             }
  57.         }
  58.         timezones.addItemListener( new ItemListener() {
  59.             public void itemStateChanged( ItemEvent e ) {
  60.                 String zone = (String)timezones.getSelectedItem();
  61.                 tz = TimeZone.getTimeZone( zone );
  62.                 format.setTimeZone( tz );
  63.             }
  64.         } );
  65.        
  66.         main.add( time, BorderLayout.CENTER );
  67.         main.add( timezones, BorderLayout.SOUTH );
  68.         main.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
  69.         add( main );
  70.         pack();
  71.        
  72.         toolkit = getToolkit();
  73.         Dimension screenSize = toolkit.getScreenSize();
  74.         setLocation( ( screenSize.width - getWidth() ) / 2, ( screenSize.height - getHeight() ) / 2 );
  75.         setDefaultCloseOperation( EXIT_ON_CLOSE );
  76.        
  77.         Timer t = new Timer( 1000, new ActionListener() {
  78.             public void actionPerformed( ActionEvent e ) {
  79.                 time.setText( format.format( new Date() ) );
  80.             }
  81.         } );
  82.         t.start();
  83.     }
  84.    
  85.     public static void main( String[] args ) {
  86.         Clock clock = new Clock();
  87.         clock.setVisible( true );
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement