Java- Changing swing background colour?

2019-02-27 08:50发布

Ok so ive got a swing app going using the "System" look and feel. Now, I want to change the background colour of the main panels to black. Too easy right?

UIManager.put("Panel.background", Color.BLACK);

Well yeah, except now the controls in the app look stupid, because their 'shadows', for want of a better word, are graduated to fade towards the old system default colour(gross windows grey). So there are light grey 'corners' on all the controls, especially the tabs on JTabbedPane. I know it can be fixed, because if you change the windowsXP theme to one with a different default application colour, the controls take on this changed colour and their shadows 'fade' towards it.

But I have no idea what UIManager key it is, or even if you can do it with UIManger.

I dont really want to change the L&F engine, because apart from this it looks good.

5条回答
你好瞎i
2楼-- · 2019-02-27 09:31

You might try these:

  • control
  • controlDkShadow
  • controlHighlight
  • controlLtHighlight
  • controlShadow

(I just found them in this list: Swing [Archive] - UIManager: setting background and JScrollBar )

查看更多
叛逆
3楼-- · 2019-02-27 09:40

Some controls like JButton need to have setOpaque(false) called to allow the new background colors to fade through.

查看更多
来,给爷笑一个
4楼-- · 2019-02-27 09:47

To list out all the possible options that we can set to UIManager to change LaF, run the code below ........

 import java.util.*;
  import javax.swing.UIManager;

  public class UIManager_All_Put_Options
  {
    public static void main (String[] args)
    {
      Hashtable   properties = UIManager.getDefaults();
      Enumeration keys       = properties.keys();

      while (keys.hasMoreElements()) {
        String key   = (String) keys.nextElement();
        Object value = properties.get (key);
        System.out.printf("%-40s \t %-200s \n", key,value);
      }
    }
  }

enjoy...

查看更多
Deceive 欺骗
5楼-- · 2019-02-27 09:54

In general this is a little bit tricky. It depends on exact LaF you are using.

For example. JGoodies use own color scheme which redefine this stuff.

In general the property names are composed like

COMPONENT_NAME_WITHOUT_J + '.' + PROPERTY. 

Unfortunately, property names can be only obtained from implementation classes of LaF. These aren't shared or something. Each component has its own. Or better, it depends on laziness of author which pairs he used. In general.

A lot of help makes redefine Panel.* and Button.. A lot of components use Button. properties.

Try, play, win :). I wish you luck :).

PS: It is a lot of properties to overwrite. But this is the way how LaFs works.

查看更多
男人必须洒脱
6楼-- · 2019-02-27 09:54

You can see what the default settings (and their keys) are by using UIManager.getDefaults(); You can then iterate over the resulting keySet (it is an instance of Map).

So something like this will show all the default keys:

for (Object key: UIManager.getDefaults().keySet())
{
    System.out.println(key);
}
查看更多
登录 后发表回答