Buat class Time. berikut kode Time.java
public class Time {
private String detik;
private String menit;
private String jam;
public Time(){
}
public Time(String detik, String menit, String jam){
this.detik = detik;
this.menit = menit;
this.jam = jam;
}
public String getDetik() {
return detik;
}
public void setDetik(String detik) {
this.detik = detik;
}
public String getMenit() {
return menit;
}
public void setMenit(String menit) {
this.menit = menit;
}
public String getJam() {
return jam;
}
public void setJam(String jam) {
this.jam = jam;
}
}<><><><><><><><><><><><><><><><>
package Control;
import java.util.Date;
public class StopWatch {
private Date date;
private String detik, menit, jam;
public Time timeFormat(int s, int m, int h){
Time tm;
String nolS="", nolM="", nolH="";
if (s <= 9) nolS = "0";
if (m <= 9) nolM = "0";
if (h <= 9) nolH = "0";
tm = new Time(nolS+Integer.toString(s), nolM+Integer.toString(m), nolH+Integer.toString(h));
return tm;
}
public Time currentTime(){
String nol_jam = "";
String nol_menit = "";
String nol_detik = "";
date = new Date();
Time tm = timeFormat(date.getSeconds(),date.getMinutes(), date.getHours());
return tm;
}
}Tambahkan variabel berikut ini pada ViewForm.java
private int s=0, m=0, h=0;
Timer timer;
private int interval = 100;
Tambahkan method berikut ini
public void setWaktu()
{
if(s==60)
{
s=0;
m++;
}
else s++;
if(m==60)
{
m=0;
h++;
}
StopWatch tm = new StopWatch();
Time t = tm.timeFormat(s, m, h);
lblStopWatch.setText( t.getJam()+":"+ t.getMenit()+":" + t.getDetik());
}
private void initMulai(){
timer = new Timer(interval,new ActionListener() {
public void actionPerformed(ActionEvent ev) {setWaktu();
}
});
timer.start();
}
public void stop() {
timer.stop();
}
Pada Event Button Start tambahkan method initMulai() sehingga menjadi
private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
initMulai();
}
Pada Event Button Stop tambahkan method stop() sehingga menjadi
private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {
stop();
}
Pada Event Button Clear sebagai berikut:
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
s=0; m=0; h=0;
Time t = new Time();
t.setDetik("00");
t.setJam("00");
t.setMenit("00");
lblStopWatch.setText( t.getJam()+":"+ t.getMenit()+":" + t.getDetik());
}Semoga Bermanfaat
No comments:
Write comments