Search

Rabu, 06 Juni 2012

StopWatch Program menggunakan java swing dan awt

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class stopwatch extends JFrame implements ActionListener {
  Timer time;
  int milidetik, detik, menit, jam;
  String nol_jam = "";
  String nol_men = "";
  String nol_det = "";
  String nol_mili = "";
  int no = 0;
  JTextField tampilan = new JTextField("klik starts");
 
  Button mulai = new Button("Start");
  Button reset = new Button("Reset");
  Button pause = new Button("Pause");

  public stopwatch() {

    setResizable(false);
    getContentPane().setLayout(null);
  
   tampilan.setFont(new java.awt.Font("Times New Roman", Font.BOLD, 20));
    tampilan.setEditable(false);
    tampilan.setForeground(Color.white);
    tampilan.setBackground(Color.black);
    tampilan.setHorizontalAlignment(JTextField.CENTER);
    tampilan.setBounds(new Rectangle(75, 16, 146, 35));

    mulai.setBounds(new Rectangle(50, 68, 40, 30));
    mulai.setSize(60, 30);
    mulai.setForeground(Color.black);
    mulai.setBackground(Color.red);

    reset.setEnabled(false);
    reset.setBounds(new Rectangle(120, 68, 40, 30));
    reset.setSize(60, 30);
    reset.setForeground(Color.black);
    reset.setBackground(Color.red);

    pause.setEnabled(false);
    pause.setBounds(new Rectangle(190, 68, 40, 30));
    pause.setSize(60, 30);
    pause.setForeground(Color.black);
    pause.setBackground(Color.red);

    mulai.addActionListener(this);
    pause.addActionListener(this);
    reset.addActionListener(this);
  
    this.getContentPane().add(tampilan);
    this.getContentPane().add(mulai);
    this.getContentPane().add(reset);
    this.getContentPane().add(pause);
  }
  public void aktifkan() {
    ActionListener tr = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {

milidetik++;
if(milidetik==100)
{
milidetik = 0;

detik++;
if(detik==60)
{
detik = 0;

menit++;
if(menit==60)
{
menit=0;

jam++;
if (jam==24);
{
jam=0;
}}}}

if (jam <= 9) {
nol_jam = "0";
}else nol_jam="";
if (menit <= 9) {
nol_men = "0";
}else nol_men="";
if (detik <= 9) {
nol_det = "0";
}else nol_det="";
if (milidetik <= 9) {
nol_mili = "0";
}else nol_mili="";

tampilan.setText(nol_jam + Integer.toString(jam)+ " : " + nol_men + Integer.toString(menit)+ " : " +
nol_det +Integer.toString(detik)+ " : " + nol_mili + Integer.toString(milidetik));
      }
    };
    time=new Timer(10,tr);
    time.start();
  }

  public void actionPerformed(ActionEvent e) {
  
    Object tombol = e.getSource();
    if (tombol== mulai) {
      mulai.setEnabled(false);
      reset.setEnabled(false);
      pause.setEnabled(true);
      aktifkan();
    }
    if (tombol == pause) {
      mulai.setEnabled(true);
      reset.setEnabled(true);
      pause.setEnabled(false);
      time.stop();
    }

    if (tombol == reset) {
     no=0;menit=0;detik=0;milidetik=0;jam=0;nol_mili="0";nol_men="0";nol_det="0";nol_jam="0";
      tampilan.setText(nol_jam + Integer.toString(jam)+ " : " + nol_men + Integer.toString(menit)+ " : " +
     nol_det +Integer.toString(detik)+ " : " + nol_mili + Integer.toString(milidetik));
    }
  }
}

Buat kelas exsekusinya pakai yang di bawah :

import javax.swing.JFrame;

public class stopwatch1{
    public static void main(String[] args) {
        // TODO code application logic
           stopwatch sw = new stopwatch();
    sw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sw.setTitle("Tugas Stopwatch");
    sw.setSize(300,150);
    sw.show();
    }
}

Tidak ada komentar:

Posting Komentar