我使用这个myButton.setBackground(myColor)
将JButton
背景颜色更改为我的颜色,如何找到它的原始默认背景颜色,以便我可以将其更改回来?我知道在更改和使用它之前我可以保存它的默认背景颜色,但是我想知道Java是否将它存储在某个地方以便我可以调用类似于:myButton.getClass.getDefaultBackground()
来取回它?
btn.setBackground(new JButton().getBackground());
怎么样...它将获得按钮的默认颜色
myButton.setBackground(null)
将其更改回默认颜色。
这可能有所帮助:
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.
不要试图从Jframe或其他元素获取背景以将其应用于按钮;如果你已经改变它,请执行以下操作
ElementToStyle.setBackground(null);
现在,默认背景颜色存储在Color jbb中,您现在可以将其用作要查找/使用的颜色
Color cbt= jButton6.getBackground();
String color_button=cbt.getRed()+","+cbt.getGreen()+","+cbt.getBlue();
如果你不会得到RGB颜色按钮尝试此代码
它适用于:
button.setBackground(null);
和
button.setBackground(new JButton().getBackground());
(当您创建新的JButton时,其背景颜色初始化为空颜色) 因此,选择您认为最适合您项目的那个