Test4.java
Code:
import javax.swing.*; import java.awt.*; public class Test4 extends JFrame { public Test4() { super("Test Toolkit"); setSize(700, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); FlowLayout beginButtons = new FlowLayout(); setLayout(beginButtons); JLabel startLabels = new JLabel("Current System"); TimePanel time = new TimePanel(); add(startLabels); add(time); setVisible(true); } public static void main(String[] args) { Test4 StartWindow = new Test4(); } }
TimePanel.java
Code:
import javax.swing.*; import java.util.*; public class TimePanel extends JPanel { public TimePanel() { super(); String currentTime = getTime(); JLabel time = new JLabel("Time: "); JLabel current = new JLabel(currentTime); add(time); add(current); } String getTime() { String time; // get current time and date 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); String monthName = ""; switch (month) { case (1): monthName = "January"; break; case (2): monthName = "February"; break; case (3): monthName = "March"; break; case (4): monthName = "April"; break; case (5): monthName = "May"; break; case (6): monthName = "June"; break; case (7): monthName = "July"; break; case (8): monthName = "August"; break; case (9): monthName = "September"; break; case (10): monthName = "October"; break; case (11): monthName = "November"; break; case (12): monthName = "December"; } time = monthName + " " + day + ", " + year + " " + hour + ":" + minute; return time; } }