更改 JButton 的外观和感觉

发布于 2025-01-07 04:11:57 字数 92 浏览 0 评论 0原文

我想在我的应用程序中使用 nimbus 按钮样式。我不想改变 L&F。只需更改按钮的 L&F 即可使用 Nimbus L&F。有办法做到这一点吗?

I would like use the nimbus button style in my application. I don't want change L&F. Only change the L&F of the button to use nimbus L&F. Is there a way to do this?

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

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

发布评论

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

评论(1

恬淡成诗 2025-01-14 04:11:57

可能有更好的方法,但以下实用程序类应该适合您:

import javax.swing.JButton;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;


public class NimbusButton {
    private static LookAndFeel nimbus;

    public static JButton generateNimbusButton() {
        try {
            LookAndFeel current = UIManager.getLookAndFeel(); //capture the current look and feel

            if (nimbus == null) { //only initialize Nimbus once
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
                nimbus = UIManager.getLookAndFeel();
            }
            else
                UIManager.setLookAndFeel(nimbus); //set look and feel to nimbus
            JButton button = new JButton(); //create the button
            UIManager.setLookAndFeel(current); //return the look and feel to its original state
            return button;
        }
        catch (Exception e) {
            e.printStackTrace();
            return new JButton();
        }
    }
}

generateNimbusButton() 方法将外观更改为 Nimbus,创建按钮,然后将外观更改回调用该方法时的状态。

There might be a better way, but the following utility class should work for you:

import javax.swing.JButton;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;


public class NimbusButton {
    private static LookAndFeel nimbus;

    public static JButton generateNimbusButton() {
        try {
            LookAndFeel current = UIManager.getLookAndFeel(); //capture the current look and feel

            if (nimbus == null) { //only initialize Nimbus once
                for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
                nimbus = UIManager.getLookAndFeel();
            }
            else
                UIManager.setLookAndFeel(nimbus); //set look and feel to nimbus
            JButton button = new JButton(); //create the button
            UIManager.setLookAndFeel(current); //return the look and feel to its original state
            return button;
        }
        catch (Exception e) {
            e.printStackTrace();
            return new JButton();
        }
    }
}

The generateNimbusButton() method changes the look and feel to Nimbus, creates the button, then changes the look and feel back to whatever it was when the method was called.

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