Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dalydesigns.swing;
- import java.awt.event.ActionEvent;
- import java.awt.event.ItemEvent;
- import java.util.Date;
- import java.util.TimeZone;
- import java.text.SimpleDateFormat;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Toolkit;
- import java.awt.event.ActionListener;
- import java.awt.event.ItemListener;
- import javax.swing.Timer;
- import javax.swing.BorderFactory;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JComboBox;
- public class Clock extends JFrame {
- private Toolkit toolkit;
- private TimeZone tz;
- private SimpleDateFormat format;
- private JLabel time;
- private JComboBox timezones;
- public Clock() {
- setTitle( "Digital Clock" );
- tz = TimeZone.getDefault();
- format = new SimpleDateFormat( "h:m:s a" );
- format.setTimeZone( tz );
- JPanel main = new JPanel();
- main.setLayout( new BorderLayout( 10, 10 ) );
- time = new JLabel( format.format( new Date() ) );
- time.setFont( new Font( "Courier New", Font.BOLD, 14 ) );
- time.setHorizontalAlignment( JLabel.CENTER );
- time.setForeground( new Color( 50, 50, 25 ) );
- timezones = new JComboBox( TimeZone.getAvailableIDs() );
- timezones.setSelectedIndex( -1 );
- for( int i = 0; i < timezones.getItemCount(); i++ ) {
- if( timezones.getItemAt( i ).toString().equals( tz.getID() ) ) {
- timezones.setSelectedIndex( i );
- break;
- }
- }
- timezones.addItemListener( new ItemListener() {
- public void itemStateChanged( ItemEvent e ) {
- String zone = (String)timezones.getSelectedItem();
- tz = TimeZone.getTimeZone( zone );
- format.setTimeZone( tz );
- }
- } );
- main.add( time, BorderLayout.CENTER );
- main.add( timezones, BorderLayout.SOUTH );
- main.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
- add( main );
- pack();
- toolkit = getToolkit();
- Dimension screenSize = toolkit.getScreenSize();
- setLocation( ( screenSize.width - getWidth() ) / 2, ( screenSize.height - getHeight() ) / 2 );
- setDefaultCloseOperation( EXIT_ON_CLOSE );
- Timer t = new Timer( 1000, new ActionListener() {
- public void actionPerformed( ActionEvent e ) {
- time.setText( format.format( new Date() ) );
- }
- } );
- t.start();
- }
- public static void main( String[] args ) {
- Clock clock = new Clock();
- clock.setVisible( true );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement