【问题标题】:change buttons backgroundcolor in awt在awt中更改按钮背景颜色
【发布时间】:2018-09-02 16:25:22
【问题描述】:

所以我有一个名为 Safe25 的 GUI 程序。基本上,如果您按正确的顺序(即“15032018”)按下按钮,程序会自行关闭。 如果您输入了正确的数字,假设您在开始时按 1,按钮应将其背景颜色更改为绿色,如 this

如果你按错了按钮,按钮的颜色应该会变成红色。

但我的代码逻辑与我的问题无关。 正如我所说,我想更改链接图像中的按钮背景颜色。我的问题是它改变了框架的背景颜色,而不是像this

重要的一行是75,我评论了这一行。

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();
    }
}

【问题讨论】:

    标签: java swing awt jbutton


    【解决方案1】:

    你为 JButton 标记了javadoc 吗?

    编辑:

    抱歉,我快速查看了您的代码。您现在所做的是设置当前容器中每个组件的背景颜色。 虽然您的按钮数组是全局的,但您可以简单地再次遍历该集合以获取正确的组件“按钮”并设置背景颜色,如下所示:

            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
    

    【讨论】:

    • 似乎 setBackground 对我来说是错误的。如果我在您给我的代码中将 setBackground 替换为 setForeground,则一切正常。如果我使用 setBackground tho,什么也没发生,一切都保持白色。我也在另一堂课上测试过,仍然是同样的问题。我给了我的一个朋友代码,它对他有用,所以它在我这边唯一的错误,只有 setBackground 方法,setForeground 方法工作正常。我正在使用带有最新 Eclipse 版本的 Mac,所以我不确定是什么导致了该错误
    • setBackground 是一个绑定属性,它应该自己触发重绘
    【解决方案2】:

    答案是,不,不是真的——或者至少不是你想象的那样。

    按钮的内容由外观委托提供,其中大部分忽略了background 属性之类的内容(或者至少不要以您认为应该的方式使用它)。

    相反,您需要移除这些装饰并自己做一些工作

    例如...

    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); // 
    }
    

    这将禁用区域填充,替换边框并使组件透明,这会产生类似于

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 1970-01-01
      • 2021-06-22
      • 2015-04-03
      • 2021-03-18
      • 2013-12-09
      相关资源
      最近更新 更多