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