Advertisement
Guest User

Uhr

a guest
Mar 25th, 2017
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.Date;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6.  
  7. public class Digitaluhr extends JFrame implements Runnable {
  8.  
  9.         private static final long serialVersionUID = 1L;
  10.  
  11.  
  12.         private Date date;
  13.  
  14.         private Font font = new Font("Verdana", Font.PLAIN, 20);
  15.  
  16.         private Thread thread;
  17.  
  18.         private JLabel label = null;
  19.        
  20.  
  21.        
  22.        
  23.  
  24.  
  25.         public Digitaluhr(){
  26.  
  27.  
  28.             setTitle("UHR");
  29.             setAlwaysOnTop(true);
  30.             setLocationRelativeTo(null);
  31.             setLayout(new FlowLayout());
  32.             setVisible(true);
  33.             pack();
  34.             add(getLabel());
  35.             setSize(350, 60);
  36.             getContentPane().setBackground(Color.DARK_GRAY);
  37.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.             setVisible(true);
  39.         }
  40.  
  41.  
  42.  
  43.         public JLabel getLabel() {
  44.             if (label==null){
  45.                 label = new JLabel();
  46.                 label.setFont(font);
  47.                 this.add(label);
  48.                 label.setForeground(Color.WHITE);
  49.                 start();
  50.  
  51.             }
  52.             return label;
  53.         }
  54.  
  55.         private void gibDate() {
  56.             label.setText(date.toString());
  57.         }
  58.  
  59.         public void start() {
  60.             if (thread == null) {
  61.                 thread = new Thread(this);
  62.                 thread.start();
  63.             }
  64.         }
  65.  
  66.         public void run() {
  67.  
  68.             while (true) {
  69.                 date = new Date();
  70.                 gibDate();
  71.                 try {
  72.                     Thread.sleep(1000);
  73.                 } catch (InterruptedException e) {
  74.                     e.printStackTrace();
  75.                 }
  76.             }
  77.         }
  78.  
  79.         public static void main(String[] args) {
  80.           Digitaluhr gui = new Digitaluhr();
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement