~Java4Beginners~
~Java4Beginners~

Klasse Timerpanel


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * Timerpanel
 * @author Markus Badzura
 * @version 1.0
 */
public class Timerpanel extends JFrame
{
    ///////////////////////////////////////////////////////////////////////////
    //                                                                       //
    // Variablendeklaration                                                  //
    //                                                                       //  
    ///////////////////////////////////////////////////////////////////////////       
    private Timer timer;
    private int counterValue = 0;
    private int sekunde = 0;
    private int minute = 0;
    private int stunde = 0;
    private DecimalFormat df = new DecimalFormat( "00" );
    
    /**
     * Startpunkt des Timerpanels
     * @param args 
     * @author Markus Badzura
     * @version 1.0
     */
    public static void main(String[] args) 
    {
        Timerpanel tp = new Timerpanel();
        tp.Timerpanel();
    }
    
    /**
     * Setzen des Fensters
     * @author Markus Badzura
     * @version 1.0
     */
    void Timerpanel()
    {
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                exit();
            }
        });       
        this.setSize(100,75);
        this.getContentPane().setBackground(Color.BLUE);
        this.setLocation(0,0);
        this.setLayout(null);
        setTimer();
        this.setVisible(true);
    }
    
    /**
     * Setzten des Timelpanels und Start des Timers
     * @author Markus Badzura
     * @version 1.0
     */
    private void setTimer()
    {
        JPanel pan_timer = new JPanel();
        pan_timer.setLayout(null);
        pan_timer.setSize(100,75);
        pan_timer.setBackground(Color.BLUE);
        pan_timer.setLocation(0,0);
        JLabel lbl_timer = new JLabel("0:00:00",JLabel.CENTER);
        lbl_timer.setForeground(Color.WHITE);
        lbl_timer.setFont(lbl_timer.getFont().deriveFont(18f));
        lbl_timer.setBounds(0,10,100,25);
        pan_timer.add(lbl_timer);
        this.add(pan_timer);
            timer = new javax.swing.Timer(1000, (ActionEvent e1) -> {
                counterValue++;
                if (counterValue == 60)
                {
                    counterValue = 0;
                    minute++;
                    if (minute == 60)
                    {
                        minute = 0;
                        stunde++;
                    }
                }
                sekunde = (int)counterValue;
                lbl_timer.setText(stunde+":"+df.format(minute)+":"+df.format(sekunde));
                if(counterValue == 7200)
                {
                    lbl_timer.setOpaque(true);
                    lbl_timer.setBackground(Color.red);
                    lbl_timer.setText("2 Stunden vorbei");
                    timer.stop();
                }
            });
            timer.start();          
    }
    
     /**
     * Abfragedialog beim Beenden des Programmes, inclusive des Schließens
     * über ALT + F4 und dem Schließbutton über die Titelleiste.
     * @author Markus Badzura
     * @version 1.0
     */
    private void exit()
    {
        int result = JOptionPane.showConfirmDialog(null, "Möchten Sie wirklich beenden?",
                "Programm beenden", JOptionPane.YES_NO_OPTION);
        switch (result)
        {
            case JOptionPane.YES_OPTION:
                System.exit(0);
        }
    }      
}
nach oben Java4Beginners -- Seitenversion 1.0 -- Stand: 2017-05-02