简体   繁体   中英

JButton margins. Not respected when nimbus plaf

The property margin of a JButton isn't respected when the nimbus look and feel is installed.
. I need some "little" buttons, but nimbus forces the space around button text to be large, so I only get "very large" buttons.
I discovered in nimbus defaults page that there is a property called:

Button.contentMargins

that is preset with large values.
I've tryed to override it with the following code:

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

in the main , just after setting the nimbus look and feel.

But nothing happens, the empty space around buttons text still remains large. Any idea?

更改JComponent.sizeVariant的值也可能是有效的,如调整组件大小中所述

on base of thread How to change the background color for JPanels with Nimbus Look and Feel? is possible to change and assign one value for something from Nimbus Defaults ,

but are you sure that you needed this output to the GUI, nothing nice

在此输入图像描述

vs basic JButton with Nimbus L&F

在此输入图像描述

from code

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.InsetsUIResource;

public class NimbusJPanelBackGround {

    public NimbusJPanelBackGround() {
        JButton btn = new JButton("  Whatever  ");
        JButton btn1 = new JButton("  Whatever  ");
        JPanel p = new JPanel();
        p.add(btn);
        p.add(btn1);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.add(p, BorderLayout.CENTER);
        f.setSize(200, 100);
        f.setLocation(150, 150);
        f.setVisible(true);
    }

    public static void main(String[] args) {

        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white);
                    UIManager.getLookAndFeelDefaults().put("Button.contentMargins", new InsetsUIResource(0,0,0,0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                NimbusJPanelBackGround nimbusJPanelBackGround = new NimbusJPanelBackGround();
            }
        });
    }
}

previously +1 for interesting question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM