1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 5 public class Test_16_24 extends JFrame{ 6 JPanel butJP= new JPanel(); 7 StillClock clock = new StillClock(); 8 JButton jb1 = new JButton("start"); 9 JButton jb2 = new JButton("stop"); 10 Timer timer = new Timer(1000,new ActionListener(){ 11 @Override 12 public void actionPerformed(ActionEvent e) { 13 // TODO Auto-generated method stub 14 clock.setCurrentTime(); 15 clock.repaint(); 16 } 17 }); 18 19 public Test_16_24(){ 20 butJP.add(jb1); 21 butJP.add(jb2); 22 add(butJP,BorderLayout.SOUTH); 23 add(clock,BorderLayout.CENTER); 24 //start event listener 25 jb1.addActionListener(new ActionListener(){ 26 27 @Override 28 public void actionPerformed(ActionEvent arg0) { 29 // TODO Auto-generated method stub 30 timer.start(); 31 } 32 33 }); 34 //stop event listener 35 jb2.addActionListener(new ActionListener(){ 36 37 @Override 38 public void actionPerformed(ActionEvent arg0) { 39 // TODO Auto-generated method stub 40 timer.stop(); 41 } 42 43 }); 44 45 } 46 47 public static void main(String[] args) { 48 // TODO Auto-generated method stub 49 JFrame frame = new Test_16_24(); 50 frame.setTitle("Test_16_24"); 51 frame.setSize(300, 300); 52 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 53 frame.setLocationRelativeTo(null); 54 frame.setVisible(true); 55 } 56 57 }