1

我想在我的应用程序中使用 nimbus 按钮样式。我不想改变 L&F。仅更改按钮的 L&F 以使用 nimbus L&F。有没有办法做到这一点?

4

1 回答 1

1

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

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,创建按钮,然后将外观更改回调用该方法时的外观。

于 2012-03-07T17:50:55.237 回答