如何获得JButton的默认背景颜色?

问题描述 投票:7回答:7

我使用这个myButton.setBackground(myColor)JButton背景颜色更改为我的颜色,如何找到它的原始默认背景颜色,以便我可以将其更改回来?我知道在更改和使用它之前我可以保存它的默认背景颜色,但是我想知道Java是否将它存储在某个地方以便我可以调用类似于:myButton.getClass.getDefaultBackground()来取回它?

colors background default jbutton
7个回答
11
投票

btn.setBackground(new JButton().getBackground());

怎么样...它将获得按钮的默认颜色


8
投票
myButton.setBackground(null)

将其更改回默认颜色。


3
投票

这可能有所帮助:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/SystemColor.html

Toolkit.getDesktopProperty(java.lang.String)
Toolkit.getDesktopProperty("control");
// control - The color rendered for the background of control panels and control objects, such as pushbuttons.

0
投票

不要试图从Jframe或其他元素获取背景以将其应用于按钮;如果你已经改变它,请执行以下操作

ElementToStyle.setBackground(null);

0
投票
  1. 制作一个新按钮“db”
  2. 制作一个新的变量类型Color“jbb”
  3. ie - Color jbb = db.getBackground();

现在,默认背景颜色存储在Color jbb中,您现在可以将其用作要查找/使用的颜色


0
投票
   Color cbt= jButton6.getBackground();

        String color_button=cbt.getRed()+","+cbt.getGreen()+","+cbt.getBlue();

如果你不会得到RGB颜色按钮尝试此代码


0
投票

它适用于:

button.setBackground(null);

button.setBackground(new JButton().getBackground());

(当您创建新的JButton时,其背景颜色初始化为空颜色) 因此,选择您认为最适合您项目的那个

© www.soinside.com 2019 - 2024. All rights reserved.