我可以在Mac OS X上获得Swing的原生JPopupMenu外观吗?

时间:2014-01-07 06:33:41

标签: java macos swing jpopupmenu quaqua

我正在尝试在Swing中创建一个组合框(在Java 7下)看起来像一个原生的组合框。事实证明,JPopupMenu用于显示组合框的选项,因此它变成了JPopupMenu看起来足够原生的问题。

如果我使用默认的Aqua外观和感觉,我会得到方形边缘,这根本不是正确的,所以我们会立即抛弃这个想法。当然,人们会转向Quaqua,这应该可以解决这类问题。

public class PopupMenuTest implements Runnable {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new PopupMenuTest());
    }

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(QuaquaManager.getLookAndFeel());
            // Uncomment this for the second example
            //PopupFactory.setSharedInstance(new CustomisedScreenPopupFactory());
        } catch (Exception ignore) {}

        JComboBox<String> comboBox = new JComboBox<>();
        comboBox.setModel(new DefaultComboBoxModel<>(
            new String[] { "One", "Two", "Three" }));

        JFrame frame = new JFrame("Test");
        frame.setLayout(new FlowLayout());
        frame.add(comboBox);
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

这给了我a popup menu which does have rounded corners but has filled in the corners with black

上面注释了代码,我尝试在自定义屏幕弹出工厂中进行干扰。

public class CustomisedScreenPopupFactory extends PopupFactory {
    private final PopupFactory delegate;

    public CustomisedScreenPopupFactory() {
        PopupFactory delegate;
        try {
            Class<? extends PopupFactory> clazz =
                Class.forName("com.apple.laf.ScreenPopupFactory")
                    .asSubclass(PopupFactory.class);
            Constructor<? extends PopupFactory> constructor =
                clazz.getDeclaredConstructor();
            constructor.setAccessible(true); // hacks
            delegate = constructor.newInstance();
        } catch (Exception ignore) {
            delegate = new PopupFactory(); // has to be set to something
        }
        this.delegate = delegate;
    }

    @Override
    public Popup getPopup(Component owner, Component contents, int x, int y) {
        Popup popup = delegate.getPopup(owner, contents, x, y);

        try {
            Method method = Popup.class.getDeclaredMethod("getComponent");
            method.setAccessible(true);
            Component component = (Component) method.invoke(popup);
            if (component instanceof JWindow) {    // always is, so far
                JWindow window = (JWindow) component;
                JRootPane rootPane = window.getRootPane();

                // This call here is what all the rest of the boilerplate was
                // added in order to access.
                AWTUtilities.setWindowOpaque(window, false);
            }
        } catch (Exception e) {
            Logger.getLogger(getClass()).error("Couldn't customise the popup window", e);
        }

        return popup;
    }
}

这给了我an interesting result where the black corners are gone, but so is the shadow

问题是,我想要圆角和阴影。有可能同时获得两者吗?

顺便提一下,我注意到IDEA确实做对了,但是我无法从他们的来源中找出它为什么会起作用,所以我想知道是不是因为它是在Java 6而不是Java 7上运行......

1 个答案:

答案 0 :(得分:-1)

您可以按照以下方式匹配系统的外观:

    try{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName);
    catch(Exception e){}

只是得到系统L&amp; F,无论如何都比原来的L&amp; F更原始。

希望这有帮助!