问题
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