Tugas PBO B Digital Clock
Nama: Chrisnady Anggaiswara
NRP: 5111640000153
Kelas: PBO B
Disini ada 2 class yang digunakan, yaitu class DigitalClock untuk menjalankan programnya, dan class ClockLabel untuk menampilkan waktunya. Berikut source codenya:
1. Digital Clock
/**
* Executioner program
*
* @author (Chrisnady Anggaiswara)
* @version (1)
*/
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.util.*;
import java.text.*;
public class DigitalClock {
public static void main(String[] arguments) {
ClockLabel dateLable = new ClockLabel("date");
ClockLabel timeLable = new ClockLabel("time");
ClockLabel dayLable = new ClockLabel("day");
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame("Digital Clock");
f.setSize(300,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(3, 1));
f.add(timeLable);
f.getContentPane().setBackground(Color.black);
f.setVisible(true);
}
}
2. ClockLabel
/**
* LabelClock/CLock program
*
* @author (Chrisnady Anggaiswara)
* @version (1)
*/
import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.util.*;
import java.text.*;
class ClockLabel extends JLabel implements ActionListener {
String type;
SimpleDateFormat sdf;
public ClockLabel(String type) {
this.type = type;
setForeground(Color.green);
switch (type) {
case "time" : sdf = new SimpleDateFormat("hh:mm:ss a");
setFont(new Font("sans-serif", Font.PLAIN, 40));
setHorizontalAlignment(SwingConstants.CENTER);
break;
default : sdf = new SimpleDateFormat();
break;
}
Timer t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent ae) {
Date d = new Date();
setText(sdf.format(d));
}
}
Tidak ada komentar:
Posting Komentar