【问题标题】:Java: I can't change the background of JButtonJava:我无法更改 JButton 的背景
【发布时间】:2011-09-09 16:16:16
【问题描述】:

大家好。我正在工作一个java程序。我想改变一个 JButton 的背景,但是我不能改变它。当功能结束时,颜色没有一点变化。这是我的代码:

public void initialization() {
        button = new JButton[size][size];
        mainPanel.setLayout(new GridLayout(0, size));
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                button[i][j] = new JButton();
                button[i][j].setBackground(Color.BLACK);
                //button[i][j].setVisible(true);
                //button[i][j].setEnabled(false);
                mainPanel.add(button[i][j]);

            }
        }
        mainPanel.updateUI();
        System.out.print("Button Finished\n");
    }

我希望有人可以帮助我。

【问题讨论】:

    标签: java swing jbutton


    【解决方案1】:

    尝试在更改背景之前将其设置为不透明:

    button[i][j].setOpaque(true);
    button[i][j].setBackground(Color.BLACK);
    

    【讨论】:

    • 是的!这就是关键!谢谢!我将在 8 分钟内将其标记为正确。系统不允许我在 8 分钟内这样做。
    猜你喜欢
    • 1970-01-01
    • 2014-05-11
    • 2011-01-28
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多