2

Swing (Java 7 の下) でコンボ ボックスをネイティブ コンボ ボックスのように見せようとしています。JPopupMenuコンボ ボックスのオプションを表示するためにaが使用されていることがわかりました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);
    }
}

これは私に与えます角が丸くなっているが角が黒で塗りつぶされているポップアップメニュー

上記のコードがコメントアウトされている場所で、カスタム画面ポップアップ ファクトリでジャムを試みました。

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 7 ではなく Java 6 で実行されているためではないかと思います...

4

1 に答える 1

-1

システムのルック アンド フィールを次のように一致させることができます。

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

とにかく、外部の L&F よりも独創的なシステム L&F を取得するだけです。

お役に立てれば!

于 2014-02-04T12:31:13.037 に答える