1 15 16 package xenon.free.swing; 30 31 import java.awt.*; 32 import java.awt.event.*; 33 import javax.swing.*; import javax.swing.event.*; 35 36 public class DigitalClockDemo extends JFrame 37 implements ActionListener 38 { 39 private DigitalClock clock; 40 private JButton bt_start, bt_stop; 41 42 public DigitalClockDemo( String title) 43 { 44 super(title); 45 46 getContentPane().setLayout( new BorderLayout(5,5) ); 47 48 Font buttonFont = new Font("Dialog", Font.BOLD, 12 ); 49 Font clockFont = new Font("Dialog", Font.BOLD, 18 ); 50 51 52 bt_start = new JButton("Start"); 53 bt_start.addActionListener(this); 54 getContentPane().add( bt_start, BorderLayout.WEST ); 55 56 bt_stop = new JButton("Stop"); 57 bt_stop.setEnabled(false); 58 bt_stop.addActionListener(this); 59 getContentPane().add( bt_stop, BorderLayout.EAST ); 60 61 clock = new DigitalClock(); 62 clock.setMode24Hours(false); 63 clock.setFont( clockFont ); 64 getContentPane().add( clock, BorderLayout.CENTER ); 65 } 66 67 public void actionPerformed( ActionEvent e ) 68 { 69 Object src = e.getSource(); 70 if ( bt_start == src ) { 71 bt_start.setEnabled(false); 72 bt_stop.setEnabled(true); 73 clock.start(); 74 } 75 else if ( bt_stop == src ) { 76 bt_start.setEnabled(true); 77 bt_stop.setEnabled(false); 78 clock.stop(); 79 } 80 } 81 82 public static void main( String [] args ) 83 { 84 DigitalClockDemo dc = new DigitalClockDemo("Digital Clock Demo!"); 85 dc.pack(); 86 dc.setVisible(true); 87 dc.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); 88 } 89 90 } 91 | Popular Tags |