How can you alter the margins in Nimbus Look and Feel

耗尽温柔 提交于 2019-12-05 02:13:07

问题


I'm trying to use Nimbus Look and Feel and I can't simply plug it in to replace other Look and feel because it adds some external padding around each component.

So, I would like to remove this padding. This is the list of defaults that can be changed, however the following code changes nothing.

UIManager.put("Button.contentMargins", new Insets(0, 0, 0, 0));

Other values are working as expected.

How can I remove this padding?

EDIT

I believe content margins is referring to the internal padding.

EDIT

Looking at the source code the external padding seems to be hard-coded. I believe it's added to allow for the focus selection property.

Should this be considered a bug? No other L&F does this, and with this extra padding it's not possible to reuse the same layout (as previous layouts all will be assuming no padding).


回答1:


I found that you have to set those defaults right after you set the look and feel (i.e., before you start creating any UI components):

try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    UIManager.getLookAndFeelDefaults().put("Table.cellNoFocusBorder", new Insets(0,0,0,0));
    UIManager.getLookAndFeelDefaults().put("Table.focusCellHighlightBorder", new Insets(0,0,0,0));
    Insets buttonInsets = new Insets(3, 6, 3, 6);
    UIManager.getLookAndFeelDefaults().put("Button.contentMargins", buttonInsets);
}
catch (Exception e) {
    e.printStackTrace();
}

Also note that you need to set them on the "LookAndFeelDefaults" not the "developer" defaults.




回答2:


Force nimbus to create the defaults first by calling getDefaults():

NimbusLookAndFeel laf = new NimbusLookAndFeel();
UIManager.setLookAndFeel(laf);
UIDefaults def = laf.getDefaults();
def.put("Button.contentMargins", new Insets(0,0,0,0));
def.put("DesktopIcon.contentMargins", new Insets(0,0,0,0));
def.put("Label.contentMargins", new Insets(0,0,0,0));

UIManager.put() won't store the settings in the nimbus defaults if the defaults was not initialized first.



来源:https://stackoverflow.com/questions/718977/how-can-you-alter-the-margins-in-nimbus-look-and-feel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!