How to get JButton default background color?

2019-02-21 21:53发布

I use this myButton.setBackground(myColor) to change the JButton background color to my color, how to find it's original default background color so I can change it back? I know I can save its default background color before I change and use that, but I wonder if Java stores it somewhere so that maybe I can call something like: myButton.getClass.getDefaultBackground() to get it back ?

7条回答
smile是对你的礼貌
2楼-- · 2019-02-21 22:13
   Color cbt= jButton6.getBackground();

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

if you wont get RGB color button try this code

查看更多
甜甜的少女心
3楼-- · 2019-02-21 22:15

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

how about this... it will get the default color of button

查看更多
时光不老,我们不散
4楼-- · 2019-02-21 22:15
  1. make a new button "db"
  2. make a new variable type Color "jbb"
  3. i.e. - Color jbb = db.getBackground();

now the default background color is stored in the Color jbb which you can now use as the color you want to find/use

查看更多
走好不送
5楼-- · 2019-02-21 22:31
myButton.setBackground(null)

changes it back to the default color.

查看更多
Evening l夕情丶
6楼-- · 2019-02-21 22:33

This might help:

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.
查看更多
7楼-- · 2019-02-21 22:37

It works both with:

button.setBackground(null);

and

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

(when you create a new JButton, its background color is initialized as a null color)
So, choose the one you consider to be the best for your project

查看更多
登录 后发表回答