Code:
part1:
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ClockPanel extends JPanel {
public ClockPanel(){
super();
String currentTime = getTime();
JLabel jl = new JLabel("Time: ");
JLabel jll = new JLabel(currentTime);
add(jl);
add(jll);
}
final String getTime(){
String time="";
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
switch (month) {
case 0:
time = "January";
break;
case 1:
time = "February";
break;
case 2:
time = "March";
break;
case 3:
time = "April";
break;
case 4:
time = "May";
break;
case 5:
time = "June";
break;
case 6:
time = "July";
break;
case 7:
time = "August";
break;
case 8:
time = "September";
break;
case 9:
time = "October";
break;
case 10:
time = "November";
break;
case 11:
time = "December";
}
time += " " + day + " " + year + " " + hour + ": " + minute;
return time;
}
}
part2:
import java.awt.*;
import javax.swing.*;
public class ClockFrame extends JFrame {
public ClockFrame(){
super();
setTitle("ClockFrame");
setSize(300, 300);
setLookAndFeel();
FlowLayout flo = new FlowLayout();
ClockPanel time = new ClockPanel();
add(time);
setVisible(true);
}
private void setLookAndFeel(){
try{
UIManager.setLookAndFeel (
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
}catch (Exception exc){
//ignore Exception
}
}
public static void main(String[] args) {
ClockFrame clf = new ClockFrame();
}
}
Result: