JButton margins. Not respected when nimbus plaf

南楼画角 提交于 2019-12-10 19:22:18

问题


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?


回答1:


Altering the value of the JComponent.sizeVariant may also be effective, as discussed in Resizing a Component.




回答2:


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

v.s. 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



来源:https://stackoverflow.com/questions/8288144/jbutton-margins-not-respected-when-nimbus-plaf

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