2

我正在尝试使 Swing 中的组合框(在 Java 7 下)看起来像本机组合框。原来aJPopupMenu是用来显示组合框的选项的,所以就变成了让aJPopupMenu看起来足够原生的问题。

如果我使用默认的 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);
    }
}

这给了我一个弹出菜单,它确实有圆角,但用黑色填充了角落.

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

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

这给了我一个有趣的结果,黑角消失了,但阴影也消失了.

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

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

4

1 回答 1

-1

您可以像这样匹配系统的外观和感觉:

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

这只是得到系统 L&F,无论如何这将比外部 L&F 更具原创性。

希望这可以帮助!

于 2014-02-04T12:31:13.037 回答