dh-Materialien
Java Projekte
// JavaProject ColoredButtons
ColoredButtons.java

// ColoredButtons.java

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


class BFrame extends JFrame implements ActionListener { // Vererbung Interfaces
    private static final long serialVersionUID = 0L;

    Image icon;
    JButton bred; // Swing-Komponenten
    JButton bgreen;
    JButton bblue;

    Color black = Color.BLACK; // Konstanten
    Color blue = Color.BLUE;
    Color green = Color.GREEN;
    Color red = Color.RED;
    Color yellow = Color.YELLOW;
    Color white = Color.WHITE;

    BFrame() {
        setTitle("Farbige Schaltflächen");
        icon = Toolkit.getDefaultToolkit().getImage("dh.png");
        setIconImage(icon);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(349, 301);
        setLayout(new FlowLayout(FlowLayout.CENTER, 40, 116));
        setLocationRelativeTo(null);

        bred = new JButton("Rot");
        add(bred);
        bred.addActionListener(this); // Schlüsselworte

        bgreen = new JButton("Grün");
        add(bgreen);
        bgreen.addActionListener(this);

        bblue = new JButton("Blau");
        add(bblue);
        bblue.addActionListener(this);

        getContentPane().setBackground(Color.LIGHT_GRAY);

        setVisible(true);
    }

    void changeColors(JButton b, Color fgcol, Color bgcol) {
        b.setForeground(fgcol);
        b.setBackground(bgcol);
    }

    @Override
    public void
actionPerformed (ActionEvent e) {
        Object sender = e.getSource();

        if (sender.equals(bred)) {
            changeColors(bred, white, red);
            changeColors(bgreen, white, red);
            changeColors(bblue, white, red);
        }

        if (sender.equals(bgreen)) {
            changeColors(bred, black, green);
            changeColors(bgreen, black, green);
            changeColors(bblue, black, green);
        }

        if (sender.equals(bblue)) {
            changeColors(bred, yellow, blue);
            changeColors(bgreen, yellow, blue);
            changeColors(bblue, yellow, blue);
        }
    }
}


public class ColoredButtons {

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

Download ColoredButtons