change buttons backgroundcolor in awt

2020-04-16 02:18发布

So i have a GUI program called Safe25. Basically, if you press the buttons in the right order which is "15032018" the program closes itself. If you input a correct number, lets say you press 1 at the start, the buttons should change their backgroundcolor to green like this:

If you press a wrong button, the buttons should change their color to red.

But the logic of my code is irrelevant for my problem. As i said, i want to change the buttons backgroundcolor like in the linked image. My problem is that it changes the backgroundcolor of the frame instead like this

The important line is 75, i commented this one.

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

public class Safe25 extends Frame implements ActionListener {
    JButton[] buttons;

    Safe25() { // Konstruktor
        setSize(250, 300); 
        setLocation(300, 300);
        setTitle("Safe25"); 

        buttons = new JButton[10]; 
        for (int i = 0; i < 10; i++) { // 10 Knöpfe im Array
            buttons[i] = new JButton("" + i); 
            buttons[i].setFont(new Font("Courier", Font.BOLD, 34));
            buttons[i].addActionListener(this); // 
        }

        Panel panel0 = new Panel(); 
        panel0.setLayout(new GridLayout(1, 1)); 
        panel0.add(buttons[0]); 
        Panel panelRest = new Panel(); 
        panelRest.setLayout(new GridLayout(3, 3)); 
        setLayout(new GridLayout(2, 1)); 
        for (int i = 1; i < 10; i++) { 
            panelRest.add(buttons[i]); 
        }
        add(panel0); // Panel mit 0-Knopf
        add(panelRest); 
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent wv) {
                System.exit(0);
            }
        });
        setVisible(true);
    }

    int s = 0; 

    public void actionPerformed(ActionEvent evt) {
        // 0 1 2 3 4 5 6 7 8 Zustände ...
        // 1-5-0-3-2-0-1-8 ist richtige Kombination
        switch (Integer.parseInt(evt.getActionCommand())) {
        case 0:
            s = (s == 2 || s == 5) ? s + 1 : 0;
            break;
        case 1:
            s = (s == 0 || s == 6) ? s + 1 : 1;
            break;
        case 2:
            s = (s == 4) ? s + 1 : 0;
            break;
        case 3:
            s = (s == 3) ? s + 1 : 0;
            break;
        case 5:
            s = (s == 1) ? s + 1 : s == 7 ? 2 : 0;
            break;
        case 8:
            s = (s == 7) ? s + 1 : 0;
            break;
        default:
            s = 0;
        }
        Color col;
        if (s == 0) { 
            col = Color.red;
        } else { // richtiger Weg
            col = Color.green;
        }
        if (s == 8) { 
            System.exit(0);
        }
        for (Component c : getComponents()) // line 75, i want this one
            c.setBackground(col); // to change the buttons backgroundcolor
        repaint(); // but it changes the frames backgroundcolor instead
    }

    public static void main(String[] args) {
        Safe25 we = new Safe25();
    }
}

2条回答
叛逆
2楼-- · 2020-04-16 03:17

have you red the javadoc for JButton?

edit:

Sorry i looked over your code to quickly. What your doing right now is setting the background color of every component in the current container. While your buttons array is global you could simply loop trough that collection again to get the correct components "the buttons" and setting the background color like so:

        for (JButton b : buttons) // line 75, i want this one
           b.setBackground(col); // to change the buttons backgroundcolor
        repaint(); // but it changes the frames backgroundcolor instead
查看更多
倾城 Initia
3楼-- · 2020-04-16 03:19

The answer is, no, not really - or at least not as you might expect.

The button's content is provided by the look and feel delegate, most of which ignore things like the background property (or at least don't use it in ways you might think it should).

Instead, you need to remove these decorations and do a little of the work yourself

For example...

buttons = new JButton[10];
for (int i = 0; i < 10; i++) { // 10 Knöpfe im Array
    buttons[i] = new JButton("" + i);
    buttons[i].setFont(new Font("Courier", Font.BOLD, 34));
    buttons[i].setContentAreaFilled(false);
    buttons[i].setOpaque(true);
    buttons[i].setBorder(new EtchedBorder(EtchedBorder.LOWERED));
    buttons[i].setBackground(Color.RED);
    buttons[i].addActionListener(this); // 
}

This disables the area filling, replaces the border and makes the component transparent, which produces something along the lines of

Custom Button background

查看更多
登录 后发表回答