setting Swing JInternalFrame lnf manually

2019-08-27 19:56发布

问题:

UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(new Color(207,255,247)));
UIManager.put("InternalFrame.inactiveTitleBackground", new ColorUIResource(new Color(207,255,247)));
JDesktopPane baTabbedPane = new JDesktopPane();
JInternalFrame iframe = new JInternalFrame("Cheapest To Deliver",true,true,true,true);
iframe.setSize(400,150);
baTabbedPane.add(iframe);

why is my Internal Frame's title background not set on startup?

I've tried setting it on the overall JFrame init but made no difference (By contrast I could change other JFrame ui component look n feel such as MenuItem.background in this location so I thought it might have been because the JInternalFrame was not a top-level component i.e. under a tabbed pane, that maybe it needed changing at some other point, but where?)

Any tips on the correct place to call UIManager.put() for JInternalFrame?

回答1:

got it eventually - the call to put() works fine after JInternalFrame creation but I did make it before I added the component to a container. I then still had to set it's UI:

JInternalFrame iframe = new JInternalFrame("blah",true,true,true,true);
UIManager.put("InternalFrame.activeTitleBackground", new ColorUIResource(new Color(248,250,175)));
UIManager.put("InternalFrame.inactiveTitleBackground", new ColorUIResource(new Color(248,250,175)));
javax.swing.plaf.basic.BasicInternalFrameUI ui = 
    new javax.swing.plaf.basic.BasicInternalFrameUI(iframe); 
iframe.setUI(ui); 


回答2:

I think you need to make all calls to UIManager.put before you create any Swing components.