Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JTime - Main.java

Main.java

Caricato da: Bonny
Scarica il programma completo

  1. package jtime;
  2.  
  3. import java.awt.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6.  
  7. public class Main extends JFrame {
  8.  
  9.     private JLabel hh = new JLabel("");
  10.     private JLabel mm = new JLabel("");
  11.     private JLabel ss = new JLabel("");
  12.  
  13.     public Main() {
  14.  
  15.         super("JTime");
  16.         this.setBounds(200, 20, 150, 150);
  17.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.  
  19.         hh.setForeground(Color.GREEN);
  20.         mm.setForeground(Color.GREEN);
  21.         ss.setForeground(Color.GREEN);
  22.         hh.setFont(new Font("Serif", Font.BOLD, 46));
  23.         mm.setFont(new Font("Serif", Font.BOLD, 46));
  24.         ss.setFont(new Font("Serif", Font.BOLD, 46));
  25.  
  26.         JPanel p = new JPanel();
  27.         p.add(hh);
  28.         p.add(mm);
  29.         p.add(ss);
  30.         p.setBackground(Color.BLACK);
  31.  
  32.         TTest x = new TTest();
  33.         java.util.Timer myTimer = new java.util.Timer();
  34.         myTimer.schedule(x, 0, 1000);
  35.  
  36.         this.getContentPane().add(p, "Center");
  37.         this.setResizable(false);
  38.         this.pack();
  39.         this.setVisible(true);
  40.  
  41.     }
  42.  
  43.     public class TTest extends TimerTask {
  44.  
  45.         public TTest() {
  46.             super();
  47.         }
  48.  
  49.         @Override
  50.         public void run() {
  51.             //throw new UnsupportedOperationException("Not supported yet.");
  52.             GregorianCalendar gc = new GregorianCalendar();
  53.             int h = gc.get(Calendar.HOUR);
  54.             int m = gc.get(Calendar.MINUTE);
  55.             int s = gc.get(Calendar.SECOND);
  56.             hh.setText(Integer.toString(h) + " . ");
  57.             mm.setText(Integer.toString(m) + " : ");
  58.             if (s == 0) {
  59.                 ss.setText("6" + Integer.toString(s));
  60.             } else if (s < 10) {
  61.                 ss.setText("0" + Integer.toString(s));
  62.             } else {
  63.                 ss.setText(Integer.toString(s));
  64.             }
  65.         }
  66.     }
  67.  
  68.     public static void main(String[] args) {
  69.  
  70.         Main f = new Main();
  71.     }
  72. }