Timer mehrere Male ausführen

Buba235

Mitglied
Hallo Leute!

Ich hab da mal ne Frage. Ich versuche mich langsam aber sicher in Java ein zu arbeiten und habe mir ein kleines Programm geschrieben, dass nichts anderes macht, als einen Timer runter laufen zu lassen. Das klappt auch ganz gut, nur brauche ich das ganze jetzt öfter. Mal zum Verständnis: Ich habe eine GUI Anwendung, die insegsamt 8 JTextFields beinhaltet (alle vorbelegt mit bestimmten Sekunden z.B. 60), einen Button (der startet die ganze Geschichte) und ein paar Labels für Ausgabe. Mein Problem ist jetzt dass ich durch das Starten des Timers nur den ersten laufen lassen kann. Das passiert in meiner event Klasse:

Java:
public class event implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int count = (int) (Double.parseDouble(tf1.getText()));
            timerLabel.setText("Time left: " + count);
           
            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            timer.start();
        }
    }

Ich denke, dass mir auch bewusst ist, wo ich ansetzen muss, um das ganze zu implementieren, aber ich weiß nicht genau wie. In dem Code oben wird der Inhalt des JTextFields (eines von den 8) genommen und runter gezählt. Wie kann ich denn da jetzt nacheinander tf1 - tf8 runter laufen lassen? Ich hoffe ihr könnt mir da weiter helfen. Falls ihr noch mehr Infos benötigt, gebe ich gerne weiter.

Vielen Dank schon mal
 
Moin,

verstehe ich das richtig, Du willst mit einem Button die Sekundenanzeige in 8 Textfeldern gleichzeitig starten?
Wenn es in allen Textfelder gleich laufen soll, kannst Du ja alle 8 in Deinen Timer einbauen oder sonst halt acht Timer basteln 😉

Du hast ja vermutlich die actionPerformed-Methode zum Button gepostet, oder?
Ok, dort wird der Wert aus dem ersten Textfeld auf das Label geschrieben ....
Und was macht der Timer dann genau?? Code??

Gruß Klaus
 
Hallo Klaus!

Das ist nicht ganz richtig. Ich möchte mit dem Button jeden Timer nach und nach ablaufen lassen. Wenn also das erste "Feld" fertig ist, dann soll erst das nächste anfangen usw. Hier einmal mein gesamter Code:

Java:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;


public class mainTimer extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JLabel promptLabel1, promptLabel2, promptLabel3, promptLabel4, promptLabel5, promptLabel6, promptLabel7, promptLabel8, timerLabel;
    int counter;
    JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7, tf8;
    JButton button;
    Timer timer;

    public mainTimer () {
        setLayout(new GridLayout(9, 9, 5, 5));

        // Label and TextField (60 secs)
        promptLabel1 = new JLabel("1st Timer: ", SwingConstants.CENTER);
        add(promptLabel1);
        tf1 = new JTextField("60", 5);
        add(tf1);

        // Label and TextField (30 secs)
        promptLabel2 = new JLabel("2nd Timer: ", SwingConstants.CENTER);
        add(promptLabel2);
        tf2 = new JTextField("30", 5);
        add(tf2);

        // Label and TextField (30 secs)
        promptLabel3 = new JLabel("3rd Timer: ", SwingConstants.CENTER);
        add(promptLabel3);
        tf3 = new JTextField("30", 5);
        add(tf3);

        // Label and TextField (30 secs)
        promptLabel4 = new JLabel("4th Timer: ", SwingConstants.CENTER);
        add(promptLabel4);
        tf4 = new JTextField("30", 5);
        add(tf4);

        // Label and TextField (30 secs)
        promptLabel5 = new JLabel("5th Timer: ", SwingConstants.CENTER);
        add(promptLabel5);
        tf5 = new JTextField("30", 5);
        add(tf5);

        // Label and TextField (30 secs)
        promptLabel6 = new JLabel("6th Timer: ", SwingConstants.CENTER);
        add(promptLabel6);
        tf6 = new JTextField("30", 5);
        add(tf6);

        // Label and TextField (30 secs)
        promptLabel6 = new JLabel("7th Timer: ", SwingConstants.CENTER);
        add(promptLabel6);
        tf6 = new JTextField("30", 5);
        add(tf6);

        // Label and TextField (60 secs)
        promptLabel6 = new JLabel("8th Timer: ", SwingConstants.CENTER);
        add(promptLabel6);
        tf6 = new JTextField("60", 5);
        add(tf6);

        button = new JButton("Start timing");
        add(button);

        timerLabel = new JLabel("Waiting...", SwingConstants.CENTER);
        add(timerLabel);

        event e = new event();
        button.addActionListener(e);
    }

    public class event implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int count = (int) (Double.parseDouble(tf1.getText()));
            timerLabel.setText("Time left: " + count);
           
            TimeClass tc = new TimeClass(count);
            timer = new Timer(1000, tc);
            timer.start();
        }
    }

    public class TimeClass implements ActionListener {
        int counter;
       
        public TimeClass (int counter) {
            this.counter = counter;
        }
       
        public void actionPerformed (ActionEvent tc) {
            counter--;
           
            if (counter >= 1) {
                timerLabel.setText("Time left: " + counter);
            } else {
                timer.stop();
                timerLabel.setText("Done!");
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }

    // Main start method
    public static void main (String args[]) {
        mainTimer gui = new mainTimer();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(310, 250);
        gui.setTitle("Timer Program");
        gui.setVisible(true);
    }
}
 
Hallo an alle!

Hat keiner eine Idee oder einen Denkansatz für mich? Ich wäre euch sehr dankbar. Es kann auch ein komplett neues Konzept sein.
 
Spontane Idee : Alle Felder in ein Array speichern. Wenn der Button gedrückt wird eine for-Schleife erstellen, in der du alle Felder durchgehst. Dann noch eine Innere Schleife while(feld!=0) in der du das Feld dann immer um einen Wert runter zählst.
 

Neue Themen


Zurück
Oben