#WW

#WW
Home » » Program GUI StopWatch Java

Program GUI StopWatch Java

Written By Unknown on Friday, 21 March 2014 | 01:56

Program GUI StopWatch Java

public class Driver {
    public static void main(String[] args) {
        Gui g = new Gui();
        CurrentTime c = new CurrentTime(g);
        g.lunch();
        c.start();
    }
}





import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Gui {
    private JFrame frame;
    private JButton btna,btnb;
    private JPanel panel1,panel2,panel3,panel4, panel5;
    private JLabel lcur, ljudul,lstopwatch, lhour,lminute,lsecond,ltime,lmili,ltitikdua1,ltitikdua2,ltitikdua3;
    JLabel lcurrent;
    private JTextField tjam,tmenit,tdetik,tmili;
    Stopwatch s ;
   
    public Gui(){
        frame = new JFrame("Stopwatch");
        panel1 = new JPanel();
        panel2 = new JPanel();
        panel3 = new JPanel();
        panel4 = new JPanel();
        panel5 = new JPanel();
        btna = new JButton("Start");
        btnb = new JButton("Reset");
        panel1 = new JPanel();
        ljudul = new JLabel("STOPWATCH");
        lcur = new JLabel("Current Time : ");
        lcurrent = new JLabel();
        lstopwatch = new JLabel("Stopwatch   ");
        lhour = new JLabel("                      Hour");
        lminute = new JLabel("  Minute");
        lsecond = new JLabel("  Second");
        lmili = new JLabel("  Mili");
        tjam = new JTextField(2);
        tmili = new JTextField(2);
        tmenit = new JTextField(2);
        tdetik = new JTextField(2);
        ltitikdua1 = new JLabel(" : ");
        ltitikdua2 = new JLabel(" : ");
        ltitikdua3 = new JLabel(" : ");
        s = new Stopwatch(this);
    }
    public void lunch(){
        FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
        layout.setHgap(110);
        frame.getContentPane().setLayout(layout);
        btna.addActionListener(new aHandler());
        btnb.addActionListener(new bHandler());
        panel1.add(ljudul);
        panel2.add(lcur);
        panel2.add(lcurrent);
        panel3.add(lstopwatch);
        panel3.add(tjam);
        panel3.add(ltitikdua1);
        panel3.add(tmenit);
        panel3.add(ltitikdua2);
        panel3.add(tdetik);
        panel3.add(ltitikdua3);
        panel3.add(tmili);
       
        panel4.add(lhour);
        panel4.add(lminute);
        panel4.add(lsecond);
        panel4.add(lmili);
        panel5.add(btna);
        panel5.add(btnb);
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);
        frame.getContentPane().add(panel4);
        frame.getContentPane().add(panel5);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.show();
    }
   
    public void setCurrentTimeToLabel(String currentTime){
        lcurrent.setText(currentTime);
    }
    public void setFieldHour(String Hour){
        tjam.setText(Hour);
    }
    public void setFieldMinute(String Minute){
        tmenit.setText(Minute);
    }
    public void setFieldSecond(String Second){
        tdetik.setText(Second);
    }
    public void setFieldMili(String Mili){
        tmili.setText(Mili);
    }
   
    private class aHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if(btna.getText().equals("Start")){
               
                btna.setText("Stop");
                btnb.setText("Pause");
                s.start();
            }
            else if(btna.getText().equals("Stop")){
                btna.setText("Start");
                btnb.setText("Reset");
                s.stop();
            }
        }
   
    }
    private class bHandler implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if(btnb.getText().equals("Pause")){
                s.stop();
                btnb.setText("Resume");
            }
            else if(btnb.getText().equals("Reset")){
                tmenit.setText("");
                tjam.setText("");
                tdetik.setText("");
                tmili.setText("");
            }
            else{
                s.resume();
                btnb.setText("Pause");
            }
        }
   
    }
}






import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CurrentTime extends Thread{
    private Gui frame;

    public CurrentTime(Gui f) {
        frame = f;
    }
   
    @Override
    public void run(){
        while(true){
            Calendar cal = Calendar.getInstance();
            cal.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            frame.lcurrent.setText(sdf.format(cal.getTime()));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(CurrentTime.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
    }
   
   
}






import java.util.logging.Level;
import java.util.logging.Logger;

public class Stopwatch extends Thread{
    private Gui frame;
    boolean cek = false;

    public Stopwatch(Gui f) {
        frame = f;
    }
   
    @Override
    public void run(){
        for(int j=0; j<24; j++){
            for(int m=0; m<60; m++){
                for(int d=0; d<60; d++){
                    for(int n=0; n<100; n++){
                        frame.setFieldHour(j+"");
                        frame.setFieldMinute(m+"");
                        frame.setFieldSecond(d+"");
                        frame.setFieldMili(n+"");
                        try {
                            sleep(10);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Stopwatch.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
       
    }
    public void Stop(){
        cek = true;
    }
   
}

0 komentar:

Post a Comment