【问题标题】:How to change the look of a disabled JButton in java如何在java中更改禁用的JButton的外观
【发布时间】:2010-12-08 03:01:44
【问题描述】:

我正在制作游戏,当我使用setEnabled(false); 禁用按钮时,按钮会变成灰色,与游戏中的其他颜色发生冲突。他们是一种在禁用按钮时更改按钮颜色的方法吗?

【问题讨论】:

    标签: java colors jbutton


    【解决方案1】:

    您可以为按钮添加 HTML 编码,这提供了很大的灵活性,甚至是禁用的按钮。

    Example: button.setText("<html><font color = red>3</font></html>");
    

    【讨论】:

    • 作为一种快速解决方法可以正常工作!我和问题开始者有同样的问题,我正在编写一个小游戏,它大量使用禁用按钮。
    【解决方案2】:

    如果您谈论的是文本,那么您也许可以使用 UIManager 来更改禁用的前景色。查看UIManager Defaults

    【讨论】:

      【解决方案3】:

      在按钮上调用 setContentAreaFilled(false) 将使按钮看起来好像被禁用,即使它不是。然而,它似乎并没有反过来起作用。

      如果您不需要让按钮看起来已启用,那么这是一个很好的解决方法。

      Docs Oracle

      【讨论】:

        【解决方案4】:

        您还可以使用JButton.setDisableIcon() 设置禁用图标

        【讨论】:

          【解决方案5】:

          您需要修改您正在使用的Look and Feel。有大量可供下载,您当然可以自己制作。

          【讨论】:

            【解决方案6】:

            按钮的ButtonUI 控制禁用的文本颜色。幸运的是,您可以更改它:

            button1.setUI(new MetalButtonUI() {
                protected Color getDisabledTextColor() {
                    return Color.BLUE;
                }
            });
            button1.setEnabled(false);
            button2.setUI(new MetalButtonUI() {
                protected Color getDisabledTextColor() {
                    return Color.RED;
                }
            });
            button2.setEnabled(false);
            

            【讨论】:

              【解决方案7】:

              最好和最简单的解决方案是:

              yourButton.setContentAreaFilled(false);
              

              只需将布尔值更改为 true 即可轻松恢复:

              yourButton.setContentAreaFilled(true);
              

              【讨论】:

                【解决方案8】:

                我对此不是 100%,但我认为您可以覆盖按钮玻璃窗格的绘制方法,以使用您选择的颜色覆盖它。

                这是关于容器窗格的教程:

                http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html

                【讨论】:

                • 单个组件没有玻璃板。玻璃板适用于整个框架。
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-21
                • 1970-01-01
                • 2012-01-19
                • 1970-01-01
                • 2011-01-07
                • 1970-01-01
                相关资源
                最近更新 更多