Home > Java > Aplikasi Time Remaining dengan Java

Aplikasi Time Remaining dengan Java

Pada kesempatan kali ini, kita akan membuat Aplikasi Time Remaining dengan Java.
Berikut ini langkah langkahnya utamanya.

  • Buat class Time berikut. Berikut ini code-nya


package Control;

/**
*
* @author Taeyeon
*/
public class Time {
private String seconds;
private String minutes;
private String hours;

public Time(){

}
public Time(String seconds, String minutes, String hours){
this.seconds = seconds;
this.minutes = minutes;
this.hours = hours;

}
public String getseconds() {
return seconds;
}

public void setseconds(String seconds) {
this.seconds = seconds;
}

public String getminutes() {
return minutes;
}

public void setminutes(String minutes) {
this.minutes = minutes;
}

public String gethours() {
return hours;
}
public void sethours(String hours) {
this.hours = hours;
}
}

  • Buat class TimeRemaining. berikut ini code-nya


package Control;

import java.util.Date;

/**
*
* @author Taeyeon
*/
public class TimeRemaining {
private Date date;
private String seconds, minutes, hours;

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;
}
}

  • Buat tampilan aplikasi sebagai berikut

Time Remaining Application

Sesuaikan nama variabel dan text  untuk setiap component pada tampilan aplikasi diatas.

Tambahkan variabel berikut.

private Timer timer;
private int seconds=0;
private int minutes=0;
private int hours=0;
private int delay=1000;

buat method berikut pada code tampilan aplikasi diatas.

private void cbIndex(){
if(cbTime.getSelectedIndex()==0){
seconds = hours = 0;
minutes=5;
}
else if(cbTime.getSelectedIndex()==1){
seconds = hours = 0;
minutes = 15;
}
else if(cbTime.getSelectedIndex()==2){
seconds = hours = 0;
minutes = 30;
}
else if(cbTime.getSelectedIndex()==3){
seconds = hours = 0;
minutes = 45;
}
else if(cbTime.getSelectedIndex()==4){
seconds = hours = 0;
minutes = 60;
}
else if(cbTime.getSelectedIndex()==5){
seconds = 0;
minutes = 30;
hours = 1;
}
}

private void mulaiTimer()
{
timer = new Timer(delay,new ActionListener(){public void actionPerformed(ActionEvent e){
if(seconds==0&&(minutes>0 || hours>0)){
seconds=59;
minutes--;
}
else
{
seconds--;
}
if(minutes==0&& hours>0){
minutes=59;
hours--;
}
if(minutes==0&& seconds==0&& hours==0)
{
stop();
seconds = minutes = hours = 0;
txtRemaining.setText("Finished");
}
TimeRemaining tm = new TimeRemaining();
Time t= tm.timeFormat(seconds, minutes, hours);
if (minutes >= 0 && hours > 0 && seconds >= 0) {
txtRemaining.setText(t.gethours() + ":" + t.getminutes() + ":" + t.getseconds());
} else if (minutes > 0 && seconds >= 0 && hours <= 0) {
txtRemaining.setText(t.getminutes() + ":" + t.getseconds());
} else if (minutes == 0 && hours == 0 && seconds > 0) {
if (seconds > 10) {
txtRemaining.setText(t.getseconds());
} else {
txtRemaining.setText(String.valueOf(seconds));

}
}

}});
}

public ViewForm() {
initComponents();
mulaiTimer();
}
public void start() {
timer.start();
btnStart.setText("Stop");
}
public void stop() {
timer.stop();
btnStart.setText("Start");
}

untuk event pada button methodnya sebagai berikut

private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {
if (btnStart.getText().equals("Start")) {
cbIndex();
start();
} else if (btnStart.getText().equals("Stop")) {
stop();
}
}

Berikut ini link download program diatas

Semoga bermanfaat😀

Referensi : dari berbagai sumber

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: