JButton 边距。当雨云普拉夫时不受尊重

发布于 2024-12-18 09:51:50 字数 562 浏览 1 评论 0原文

安装 nimbus 外观后,不考虑 JButtonmargin 属性。
。 我需要一些“小”按钮,但 nimbus 强制按钮文本周围的空间变大,所以我只得到“非常大”的按钮。
我在 nimbus 默认页面 中发现有一个名为:

Button.contentMargins

预设有较大的值。
我尝试使用以下代码覆盖它:

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

main 中,在设置 nimbus 外观和感觉之后。

但是没有任何反应,按钮文本周围的空白区域仍然存在大的。 有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2024-12-25 09:51:50

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

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

好听的两个字的网名 2024-12-25 09:51:50

基于线程如何更改具有 Nimbus 外观和感觉的 JPanels 的背景颜色? 可以更改和分配一个值对于 Nimbus Defaults 中的某些内容,

但是您确定需要将此输出输出到 GUI,没什么好

在此处输入图像描述

与使用 Nimbus L&F 的基本 JButton

在此处输入图像描述

来自之前的代码

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();
            }
        });
    }
}

+1对于有趣的问题

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

enter image description here

v.s. basic JButton with Nimbus L&F

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文