Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Random;
- public class ModernClockDisplay extends JFrame {
- private JLabel timeLabel;
- private JLabel dateLabel;
- private JLabel tempLabel;
- private JLabel iconLabel;
- private Random random;
- public ModernClockDisplay() {
- setTitle("Modern Clock Display");
- setSize(500, 300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new BorderLayout());
- // Panel utama dengan background gradasi
- JPanel mainPanel = new JPanel() {
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d = (Graphics2D) g;
- GradientPaint gradient = new GradientPaint(
- 0, 0, new Color(30, 30, 60),
- 0, getHeight(), new Color(10, 10, 30)
- );
- g2d.setPaint(gradient);
- g2d.fillRect(0, 0, getWidth(), getHeight());
- }
- };
- mainPanel.setLayout(new GridLayout(4, 1));
- add(mainPanel, BorderLayout.CENTER);
- // Ikon cuaca
- iconLabel = new JLabel("☀", SwingConstants.CENTER); // default matahari
- iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 48));
- iconLabel.setForeground(Color.YELLOW);
- // Jam digital
- timeLabel = new JLabel("", SwingConstants.CENTER);
- timeLabel.setFont(new Font("Consolas", Font.BOLD, 48));
- timeLabel.setForeground(Color.CYAN);
- // Tanggal
- dateLabel = new JLabel("", SwingConstants.CENTER);
- dateLabel.setFont(new Font("Arial", Font.PLAIN, 22));
- dateLabel.setForeground(Color.WHITE);
- // Suhu
- tempLabel = new JLabel("", SwingConstants.CENTER);
- tempLabel.setFont(new Font("Arial", Font.ITALIC, 20));
- tempLabel.setForeground(Color.ORANGE);
- // Tambahkan komponen ke panel
- mainPanel.add(iconLabel);
- mainPanel.add(timeLabel);
- mainPanel.add(dateLabel);
- mainPanel.add(tempLabel);
- random = new Random();
- // Timer update tiap detik
- Timer timer = new Timer(1000, e -> updateDisplay());
- timer.start();
- }
- private void updateDisplay() {
- // Format jam
- SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
- timeLabel.setText(timeFormat.format(new Date()));
- // Format tanggal
- SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, dd MMMM yyyy");
- dateLabel.setText(dateFormat.format(new Date()));
- // Suhu random
- double temp = 20 + (10 * random.nextDouble());
- tempLabel.setText(String.format("Suhu Ruangan: %.2f °C", temp));
- // Ganti ikon sesuai suhu
- if (temp < 23) {
- iconLabel.setText("❄"); // dingin
- iconLabel.setForeground(Color.CYAN);
- } else if (temp < 27) {
- iconLabel.setText("☀"); // normal
- iconLabel.setForeground(Color.YELLOW);
- } else {
- iconLabel.setText("🔥"); // panas
- iconLabel.setForeground(Color.RED);
- }
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> {
- ModernClockDisplay app = new ModernClockDisplay();
- app.setVisible(true);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment