/* Timer GUI by Chuck Liang, Hofstra University Computer Science. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; /* gui *********************** */ class mywindow extends JFrame implements ActionListener { private final int BSIZE = 100; private final int WGAP = 20; private Container pane; private JTextField m0; // pair of minute and second windows private JTextField m1; private JTextField s0; private JTextField s1; private JButton resetB; private JButton startB; private JButton stopB; private JButton reverseB; private JLabel colonL; // label for colon public timer thetimer; // instance of student-defined class public void actionPerformed(ActionEvent e) // event handler { if (e.getSource() == startB) starthandler(); if (e.getSource() == resetB) resethandler(); } void starthandler() { int tm1, tm0, ts1, ts0; tm1 = Integer.parseInt(m1.getText()); tm0 = Integer.parseInt(m0.getText()); ts1 = Integer.parseInt(s1.getText()); ts0 = Integer.parseInt(s0.getText()); thetimer = new timer(tm1,tm0,ts1,ts0,this); thetimer.start(); } public void resethandler() { ((timer)thetimer).reset(); } public mywindow() { setBounds(100,100,BSIZE*4+WGAP*6,2*BSIZE+BSIZE); m1 = new JTextField("0",4); m1.setBounds(WGAP,WGAP,BSIZE,BSIZE); m0 = new JTextField("0",4); m0.setBounds((WGAP*2+BSIZE),WGAP,BSIZE,BSIZE); colonL = new JLabel(":"); colonL.setBounds((WGAP*3)+BSIZE+BSIZE,WGAP,WGAP,BSIZE); colonL.setFont(new Font("Serif",Font.BOLD,24)); colonL.setHorizontalAlignment(JTextField.CENTER); s1 = new JTextField("0",4); s1.setBounds((WGAP*4)+BSIZE+BSIZE,WGAP,BSIZE,BSIZE); s0 = new JTextField("0",4); s0.setBounds((WGAP*5)+BSIZE+BSIZE+BSIZE,WGAP,BSIZE,BSIZE); m1.setFont(new Font("Serif",Font.BOLD,BSIZE-2)); m0.setFont(new Font("Serif",Font.BOLD,BSIZE-2)); s1.setFont(new Font("Serif",Font.BOLD,BSIZE-2)); s0.setFont(new Font("Serif",Font.BOLD,BSIZE-2)); s0.setHorizontalAlignment(JTextField.CENTER); m0.setHorizontalAlignment(JTextField.CENTER); s1.setHorizontalAlignment(JTextField.CENTER); m1.setHorizontalAlignment(JTextField.CENTER); startB = new JButton("Start"); startB.setBounds(2*WGAP,WGAP*2+BSIZE,6*BSIZE/3,BSIZE/2); resetB = new JButton("Reset"); resetB.setBounds(WGAP*4+(6*BSIZE/3),WGAP*2+BSIZE,6*BSIZE/3,BSIZE/2); // stopB = new JButton("Stop"); // stopB->setGeometry(WGAP*3+(6*BSIZE/3),WGAP*2+BSIZE,4*BSIZE/3,BSIZE/2); pane = getContentPane(); pane.setLayout(null); pane.add(m0); pane.add(s0); pane.add(colonL); pane.add(m1); pane.add(s1); pane.add(startB); pane.add(resetB); startB.addActionListener(this); resetB.addActionListener(this); } // end of constructor public void displayUpdate(int mm1, int mm0 ,int ss1 ,int ss0) { m1.setText(""+mm1); m0.setText(""+mm0); s1.setText(""+ss1); s0.setText(""+ss0); repaint(); // paint(getGraphics()); } public void delay(int ms) { try { Thread.sleep(ms); } catch (Exception DE) {} } } // end of class mywindow public class timergui { public static void main(String[] args) { mywindow s = new mywindow(); s.setVisible(true); s.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); } // end main }// timergui