button ändern mittels timer

chris45

Grünschnabel
hi

hab folgendes problem.

ich hab ein timer den ich per button click start.
der timer soll 5 mal durch laufen und im sekunden abstand wird die buttonfarbe geändert(5 verschidene farben).
hab da schon einiges rumprobiert , häng aber immer an irgendwelchen static variablen/methoden.

kann mir da wer helfen?
 
also
event:
Code:
    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
// TODO add your handling code here:
    ThreadusInterruptus t = new ThreadusInterruptus();
    t.start();
    try {
        Thread.sleep( 2000 );
        } catch ( InterruptedException e ) { }
        t.interrupt();
    }
class ThreadusInterruptus :
Code:
  public void run(){
     while(true){
         System.out.println("los");
              //hier soll die farbe geändert werden bzw. methode in der main classe aufgerufen werden
      if(isInterrupted())
        break;
      System.out.println("1");
      try{
        Thread.sleep(500);
      }
      catch(InterruptedException e){
         interrupt();
      }
    }

in der ThreadusInterruptus classe kann ich den button nicht ändern - meint immer die wären private auch wenn ich sie auf public änder.
hab dann in der main ne methode geschrieben die das übernimmt , wenn ich die aber aufruf kommt er mit dem static/non-static fehler.
 
Wie hast du deinen JButton deklariert? Mit static oder nicht?

Der Button sollte auf jeden Fall nicht static sein

ThreadInterruptus:

Code:
private JComponent component;

public void start(JComponent component) {
    this.component = component;
    this.start(); //dadurch wird die run() gerufen
}

public void run() {
    while (true) {
        if (this.component != null) {
            component.setBackground(Color.BLUE/*hier kannst du jetzt die Farbe setzen*/);
        }

        if (isInterrupted()) break;

        try {
            Thread.sleep(500);
        }
        catch(InterruptedException e){
            interrupt();
        }
    }


Der Event:

Code:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      

    if (e.getSource() instanceof JComponent) {

        ThreadusInterruptus t = new ThreadusInterruptus();

        t.start(e.getSource());

        try {
            Thread.sleep( 2000 );
        }
        catch (InterruptedException e) {
            // do nothing
        }

        t.interrupt();
    }
}

Habs noch nicht ausprobiert Aber es sollte funktionieren.
 
Hier hab ichs getestet und es funktioniert:

Änder das package noch ab...

Code:
package thread;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * ButtonTest.
 * <p/>
 * User: rro
 * Date: 11.07.2005
 * Time: 15:13:14
 *
 * @author Romsl
 * @version $Id$
 */
public class ButtonTest extends JFrame {

    private JButton button = new JButton("Ich bin ein Button");

    public ButtonTest() {
        this.setTitle("Button Test");
        this.getContentPane().add(button);

        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ThreadInterruptus ti = new ThreadInterruptus();
                ti.start(ButtonTest.this.button, 7);
            }
        });

        this.pack();
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ButtonTest();
    }
}

Code:
package thread;

import javax.swing.*;
import java.awt.*;

/**
 * ThreadInterruptus.
 * <p/>
 * User: rro
 * Date: 11.07.2005
 * Time: 15:08:47
 *
 * @author Romsl
 * @version $Id$
 */
public class ThreadInterruptus extends Thread {

    private JComponent component;
    private int runs = 0;

    public void start(JComponent component, int runs) {
        this.component = component;
        this.runs = runs;
        this.start();
    }

    public void run() {

        for (int i = 0; i < runs; i++) {
            if (this.component != null) {
                int j = i % 4;

                switch (j) {
                    case 0:
                        component.setBackground(Color.BLUE);
                        break;
                    case 1:
                        component.setBackground(Color.RED);
                        break;
                    case 2:
                        component.setBackground(Color.YELLOW);
                        break;
                    case 3:
                        component.setBackground(Color.PINK);
                        break;
                    default: component.setBackground(Color.LIGHT_GRAY);
                }
            }

            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) {
                interrupt();
            }
        }
    }
}
 
sehr schön
danke

eine blöde frage hab ich noch :
kann man das nicht irgendwie in einer klasse machen , spart einem die ganzen übergaben
 
Sicher! Einfach das Interface Runnable implementieren.

Code:
package thread;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;

/**
 * ButtonTestRunnable.
 * <p/>
 * User: rro
 * Date: 12.07.2005
 * Time: 21:53:03
 *
 * @author Romsl
 * @version $Id$
 */
public class ButtonTestRunnable extends JFrame implements Runnable {

    private JButton button = new JButton("Ich bin ein Button");
    private Thread thread = null;

    public ButtonTestRunnable() {
        this.setTitle("Button Test");
        this.getContentPane().add(button);

        button.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                ButtonTestRunnable.this.thread = new Thread(ButtonTestRunnable.this);
                ButtonTestRunnable.this.thread.start();
            }
        });

        this.pack();
        this.setVisible(true);
    }

    public void run() {

        for (int i = 0; i < 4; i++) {

            if (this.button != null) {
                int j = i % 4;

                switch (j) {
                    case 0:
                        button.setBackground(Color.BLUE);
                        break;
                    case 1:
                        button.setBackground(Color.RED);
                        break;
                    case 2:
                        button.setBackground(Color.YELLOW);
                        break;
                    case 3:
                        button.setBackground(Color.PINK);
                        break;
                    default: button.setBackground(Color.LIGHT_GRAY);
                }
            }

            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) {
                thread.interrupt();
            }
        }
    }

    public static void main(String[] args) {
        new ButtonTestRunnable();
    }
}
 
cool ,danke

kann man ein tread eigentlich stoppen?
ich würd das ganze dann mit einer endlosschleife erweitern ,so dass man mit nem 2ten button den tread stoppt und "resetet" .bei click auf den ersten button soll das ganze dann wieder von vorner los gehen.
 
Code:
package thread;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;

/**
 * ButtonTestRunnable.
 * <p/>
 * User: rro
 * Date: 12.07.2005
 * Time: 21:53:03
 *
 * @author Romsl
 * @version $Id$
 */
public class ButtonTestRunnable extends JFrame implements Runnable {

    private JButton start = new JButton("Starte den Thread");
    private JButton stop = new JButton("Stoppe den Thread");
    private volatile Thread thread = null;

    public ButtonTestRunnable() {
        this.setTitle("Button Test");
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(start, BorderLayout.WEST);
        this.getContentPane().add(stop, BorderLayout.EAST);

        /*
         * Wenn du hier den ActionListener implementierst anstatt
         * den MouseAdapter so wie du es zu Anfang hattest dann
         * funktioniert das ganze mit Tastatur, et cetera.
         */ 
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ButtonTestRunnable.this.thread = new Thread(ButtonTestRunnable.this);
                ButtonTestRunnable.this.thread.start();
            }
        });

        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ButtonTestRunnable.this.stop();
            }
        });

        this.pack();
        this.setVisible(true);
    }

    /**
     * Diese Methode wurde eingeführt um den unsicheren stop
     * per Thread.stop() zu umgehen.
     * Desweiteren gehört in der run() noch das
     * Thread thisThread = Thread.currentThread(); und
     * die Abfrage thisThread == thread; dazu
     */ 
    public void stop() {
        thread = null;
    }

    public void run() {

        Thread thisThread = Thread.currentThread();

        for (int i = 0; thisThread == thread; i++) {

            if (this.start != null) {
                int j = i % 4;

                switch (j) {
                    case 0:
                        start.setBackground(Color.BLUE);
                        break;
                    case 1:
                        start.setBackground(Color.RED);
                        break;
                    case 2:
                        start.setBackground(Color.YELLOW);
                        break;
                    case 3:
                        start.setBackground(Color.PINK);
                        break;
                    default: start.setBackground(Color.LIGHT_GRAY);
                }
            }

            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) {
                thread.interrupt();
            }
        }
    }

    public static void main(String[] args) {
        new ButtonTestRunnable();
    }
}
 
hi
diesesmal warst du zulangsam :) bzw ich hab zu spät nachgeschaut.
bin selber schon draufgekommen.
werd mir deins trozdem mal anschauen (hab selber suspend benutzt ,soll man ja nicht).
 
Zurück