Java自动“刷新”功能

时间:2013-05-13 10:29:57

标签: java swing colors jpanel refresh

我正在使用setColorgetColor方法更改JPanel的颜色。

现在我想更改它,这样您就不必单击调用getColor所有100毫秒的函数中的getColor按钮。

但经过大量使用谷歌之后我根本找不到办法。

希望你能帮助我。

代码

package tutorial9a;

/**
 *
 * @author HiveMind
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.util.Timer;

// 1. Import 
// hier fehlt Code: zwei Imports
// hier fehlt Code: eine Implementierung
public class Tutorial09a extends JFrame implements ActionListener {

    private static int st_fensternummer = 0;
    private int fensternummer = 0;
    // global fuer addActionListener
    JButton bnCreate = new JButton("create1");
    JButton bnsetBlue = new JButton("setBlue");
    JButton bnsetRed = new JButton("setRed");
    JButton bnsetGreen = new JButton("setGreen");
    JButton bnClose = new JButton("Close");
    JPanel panelBn = new JPanel();
    Farbe farbe = Farbe.getInstance();



    public Tutorial09a(Farbe farbe) {
        this.farbe = farbe;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setGUI();
    }

    private void setGUI() {
        setSize(800, 100);
        fensternummer = ++st_fensternummer;
        setTitle("Tutorial09a: " + fensternummer);

        this.getContentPane().setLayout(new BorderLayout());

        panelBn.setLayout(new FlowLayout(FlowLayout.RIGHT));
        panelBn.add(bnCreate);
        panelBn.add(bnsetBlue);
        panelBn.add(bnsetRed);
        panelBn.add(bnsetGreen);
        panelBn.add(bnClose);


        panelBn.setBackground(farbe.color);
        // hier fehlt Code: eine Registrierung

        this.getContentPane().add(panelBn, BorderLayout.NORTH);

        setFonts(getContentPane(), 18);

        bnCreate.addActionListener(this);
        bnsetBlue.addActionListener(this);
        bnsetRed.addActionListener(this);
        bnsetGreen.addActionListener(this);
        bnClose.addActionListener(this);
    } // setGUI

    private void setFonts(Container cont, int size) {
        for (int i = 0; i < cont.getComponentCount(); i++) {
            Component c = cont.getComponent(i);
            if (c instanceof JPanel) {
                setFonts((JPanel) c, size);
            } else {
                c.setFont(new Font("Arial", Font.BOLD, size));
            }
        }
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bnCreate) {
            create_click();
        }
        if (e.getSource() == bnsetBlue) {
            setBlue_click();
        }
        if (e.getSource() == bnsetRed) {
            setRed_click();
        }
        if (e.getSource() == bnsetGreen) {
            setGreen_click();
        }

        if (e.getSource() == bnClose) {
            System.exit(0);
        }
    }

    private void create_click() {
        Tutorial09a frame = new Tutorial09a(farbe);
        frame.setVisible(true);
        frame.setLocation((int) (Math.random() * getWidth()), (int) (Math.random() * getHeight()));
    }

    private void setBlue_click() {
        farbe.color = Color.BLUE;
        getColor_click();
    }

    private void setRed_click() {
        farbe.color = Color.RED;
        getColor_click();
    }

    private void setGreen_click() {
        farbe.color = Color.GREEN;
        getColor_click();
    }

    private void getColor_click() {
        panelBn.setBackground(farbe.color);
    }

    public static void main(String[] args) {
        Farbe farbe2 = new Farbe();
        Tutorial09a frame = new Tutorial09a(farbe2);
        frame.setVisible(true);

    }
} // Tutorial09a

// hier fehlt Code: eine Ableitung
class Farbe {

    private static Farbe instance = new Farbe();

    public Farbe() {
        color = Color.BLUE;
    }

    public static Farbe getInstance() {
        return instance;
    }
    public Color color;
    // hier fehlt Code: eine Methode
}

1 个答案:

答案 0 :(得分:4)

您可以使用javax.swing.Timer课程。