Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.time.ZonedDateTime;
- import java.time.ZoneId;
- import java.time.format.DateTimeFormatter;
- import javax.swing.border.EmptyBorder;
- public class TimeDisplayApp extends JFrame {
- private JLabel telAvivTimeLabel;
- private JLabel tehranTimeLabel;
- public TimeDisplayApp() {
- // Set the native Windows look and feel
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- }
- catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
- e.printStackTrace();
- // Fallback to default if Windows look and feel is not available
- }
- setTitle("World Clocks");
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(300, 150);
- setLocationRelativeTo(null); // Center the window
- // Create a panel with a GridLayout for the labels
- JPanel panel = new JPanel(new GridLayout(2, 1));
- panel.setBorder(new EmptyBorder(10, 10, 10, 10)); // Add some padding
- telAvivTimeLabel = new JLabel();
- tehranTimeLabel = new JLabel();
- // Set font for better readability
- Font font = new Font("SansSerif", Font.BOLD, 24);
- telAvivTimeLabel.setFont(font);
- tehranTimeLabel.setFont(font);
- // Center the text in the labels
- telAvivTimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
- tehranTimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
- panel.add(telAvivTimeLabel);
- panel.add(tehranTimeLabel);
- add(panel);
- // Timer to update the time every second
- Timer timer = new Timer(1000, e -> updateTimes());
- timer.start();
- // Initial update
- updateTimes();
- }
- private void updateTimes() {
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
- // Tel Aviv time
- ZoneId telAvivZone = ZoneId.of("Asia/Jerusalem");
- ZonedDateTime telAvivDateTime = ZonedDateTime.now(telAvivZone);
- telAvivTimeLabel.setText("Tel Aviv: " + telAvivDateTime.format(formatter));
- // Tehran time
- ZoneId tehranZone = ZoneId.of("Asia/Tehran");
- ZonedDateTime tehranDateTime = ZonedDateTime.now(tehranZone);
- tehranTimeLabel.setText("Tehran: " + tehranDateTime.format(formatter));
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> {
- new TimeDisplayApp().setVisible(true);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement