Digital Clock Java Implementation

Setelah mempelajari tentang Abstraction dan Modularization, sekarang saya mencoba implementasi Digital Clock dalam bahasa Java.

Class Clock

1:  /*  
2:   * Digital Clock Implementation  
3:   *   
4:   * @author : Annas Nuril Iman  
5:   * @source : ilikechess1  
6:   */  
7:  import javax.swing.*;  
8:  import java.awt.*;  
9:  import java.awt.event.*;  
10:  import java.util.Calendar;  
11:  public class clock extends JFrame{  
12:       private static final long serialVersionUID = 1L;  
13:       // components  
14:       JTextField timeF;  
15:       JPanel panel;  
16:       public clock() {  
17:            // housekeeping  
18:            super("Java Clock by [insert your name here]");  
19:            setSize(225,200);  
20:            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
21:            setVisible(true);  
22:            setResizable(true);// for now  
23:            setLocationRelativeTo(null);  
24:            //init panel  
25:            panel = new JPanel();  
26:            panel.setLayout(new FlowLayout());  
27:            //init text field  
28:            timeF = new JTextField(10);  
29:            timeF.setEditable(false);  
30:            timeF.setFont(new Font("Arial",Font.BOLD,48));  
31:            panel.add(timeF);  
32:            add(panel);  
33:            Timer t = new Timer(1000, new Listener());  
34:            t.start();  
35:       }  
36:       class Listener implements ActionListener{  
37:           public void actionPerformed(ActionEvent e){  
38:             Calendar rightNow = Calendar.getInstance();  
39:             int hour = rightNow.get(Calendar.HOUR_OF_DAY);  
40:             int min = rightNow.get(Calendar.MINUTE);  
41:             int sec = rightNow.get(Calendar.SECOND);  
42:             timeF.setText(hour+":"+min+":"+sec);  
43:           }  
44:         }  
45:  }  

Class Executor

1:  // by ilikechess1  
2:  public class Executor {  
3:       public static void main(String[]args) {  
4:            new Executor();  
5:       }  
6:       public Executor() {  
7:            new clock();  
8:       }  
9:  }  

Maka saat di run hasilnya sebagai berikut:

Komentar

Postingan populer dari blog ini

Pong Java Game

EAS PBO-B

Image Viewer versi 3