Jam
Nama : Timothyus Tanner
NRP : 05111740000103
Kelas : PBO - A
Pada minggu ke 5 ini, saya berlatih untuk membuat sebuah jam digital dengan menggunakan java, berikut merupakan source code dan screenshoot dari program saya.
Source Code
1. Executor
/*
* Nama : Timothyus Tanner
* NRP : 05111740000103
* Kelas : PBO - A
*/
public class Executor {
public static void main(String[]args) {
new Executor();
}
public Executor() {
new clock();
}
}
2. Clock
/*
* Nama : Timothyus Tanner
* NRP : 05111740000103
* Kelas : PBO - A
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
public class clock extends JFrame{
private static final long serialVersionUID = 1L;
JTextField timeF;
JPanel panel;
public clock() {
super("Java Clock by Timothyus Tanner");
setSize(225,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
setLocationRelativeTo(null);
panel = new JPanel();
panel.setLayout(new FlowLayout());
timeF = new JTextField(0);
timeF.setBackground(Color.BLACK);
timeF.setForeground(Color.GREEN);
timeF.setEditable(false);
timeF.setFont(new Font("Arial", Font.PLAIN, 72));
panel.add(timeF);
add(panel);
Timer t = new Timer(1000, new Listener());
t.start();
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int min = rightNow.get(Calendar.MINUTE);
int sec = rightNow.get(Calendar.SECOND);
String h1 = String.format("%02d", hour);
String m1 = String.format("%02d", min);
String s1 = String.format("%02d", sec);
timeF.setText(h1 + ":" + m1 +":"+ s1);
}
}
}
Screenshoot
Tidak ada komentar:
Posting Komentar