import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.text.*;
public class Clock extends JFrame implements ActionListener{
private String time;
private String dual;
private String date;
private Date datum;
private Font schrift;
private JLabel timelabel;
private JLabel dualtime;
private JLabel datelabel;
private Timer timer;
private Container ct;
public Clock(String titel){
super(titel);
datum = new Date();
time = (DateFormat.getTimeInstance()).format(datum);
date = getDayName() + " , "
+ (DateFormat.getDateInstance()).format(datum);
dual = (Integer.toBinaryString(datum.getHours()) + ": "
+ Integer.toBinaryString(datum.getMinutes()) + ": "
+ Integer.toBinaryString(datum.getSeconds()) + " ");
schrift = new Font("Serif", Font.BOLD, 40);
timelabel = new JLabel(time);
timelabel.setFont(schrift);
timelabel.setHorizontalAlignment(JLabel.CENTER);
timelabel.setVerticalAlignment(JLabel.CENTER);
timelabel.setForeground(Color.cyan);
dualtime = new JLabel(dual);
dualtime.setFont(schrift);
dualtime.setHorizontalAlignment(JLabel.CENTER);
dualtime.setVerticalAlignment(JLabel.CENTER);
dualtime.setForeground(Color.pink);
datelabel = new JLabel(date);
datelabel.setFont(schrift);
datelabel.setForeground(Color.green);
datelabel.setHorizontalAlignment(JLabel.CENTER);
datelabel.setVerticalAlignment(JLabel.CENTER);
ct = getContentPane();
ct.setBackground(Color.blue);
ct.setLayout(new BorderLayout());
ct.add("North", timelabel);
ct.add("Center", dualtime);
ct.add("South", datelabel);
timer = new Timer(1000, this);
timer.start();
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
timelabel.setText((DateFormat.getTimeInstance()).format(new Date()));
Date d = new Date();
dualtime.setText("" + Integer.toBinaryString(d.getHours()) + ": "
+ Integer.toBinaryString(d.getMinutes())+ ": "
+ Integer.toBinaryString(d.getSeconds()));
datelabel.setText(getDayName() + " , "
+ (DateFormat.getDateInstance()).format(new Date()));
}
private String getDayName(){
int day = datum.getDay();
if(day == 0){
return "Sonntag";
} else if(day == 1){
return "Montag";
} else if(day == 2){
return "Dienstag";
} else if(day == 3){
return "Mittwoch";
} else if(day == 4){
return "Donnerstag";
} else if(day == 5){
return "Freitag";
} else if(day == 6){
return "Samstag";
} else {
return "";
}
}
public static void main(String[] args) {
Clock clock = new Clock("Uhr mit javax.swing.TIMER");
}
}