gpt4 book ai didi

java - Java 1.7 中使用自定义 Popup 时 JMenuItem ActionListener 错过了鼠标单击

转载 作者:行者123 更新时间:2023-12-01 05:04:47 25 4
gpt4 key购买 nike

我不确定问题是出在我的代码上还是 Java 1.7 上。

在下面的代码中(主要基于 Java 弹出窗口演示),单击鼠标右键将出现弹出窗口。鼠标悬停时,弹出菜单项将突出显示,单击 JmenuItem 可使弹出菜单消失;但是,单击时不会触发 JMenuItem 的 actionEvent(这应该在 JTextArea 中报告)。

其他金 block :如果我输入 JMenuItem 的助记符(此处为“a”),则会触发 actionEvent(该事件在 JTextArea 中报告)。

如果我没有附加自定义 Popup(Factory),则单击鼠标将按预期触发 actionEvent。

我使用的是 OSX 10.7.5

出现此问题的原因是:

    java version "1.7.0_07"    Java(TM) SE Runtime Environment (build 1.7.0_07-b10)    Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

This code behaves fine with:

    java version "1.6.0_33"    Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)    Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)

Any help/thoughts are greatly appreciated! Self-contained code example is below.

Thanks

Andrew

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PopupMenuDemo implements ActionListener {
JTextArea output;
JScrollPane scrollPane;
String newline = "\n";

public Container createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}

public void createPopupMenu() {
JMenuItem menuItem;
JPopupMenu popup = new JPopupMenu();
menuItem = new JMenuItem("A popup menu item", 'a');
menuItem.addActionListener(this);
popup.add(menuItem);
MouseListener popupListener = new PopupListener(popup);
output.addMouseListener(popupListener);
}

public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
String s = "Action event detected."
+ newline
+ " Event source: " + source.getText()
+ " (an instance of " + source.getClass().getName() + ")";
output.append(s + newline);
output.setCaretPosition(output.getDocument().getLength());
}

private void createAndShowGUI() {
JFrame frame = new JFrame("PopupMenuDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PopupFactory.setSharedInstance(new MyPopupFactory());
PopupMenuDemo demo = new PopupMenuDemo();
frame.setContentPane(demo.createContentPane());

demo.createPopupMenu();

frame.setSize(450, 260);
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PopupMenuDemo().createAndShowGUI();
}
});
}

class PopupListener extends MouseAdapter {
JPopupMenu popup;

PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}

public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}

public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}

class MyPopupFactory extends PopupFactory {
public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
return new MyPopup(owner, contents, x, y);
}
}

class MyPopup extends Popup {
private JWindow popupWindow;

MyPopup(Component owner, Component contents, int ownerX, int ownerY) {
popupWindow = new JWindow();
popupWindow.setLocation(ownerX, ownerY);
popupWindow.getContentPane().add(contents, BorderLayout.CENTER);
contents.invalidate();
}

public void show() {
popupWindow.setVisible(true);
popupWindow.pack();
}

public void hide() {
popupWindow.setVisible(false);
popupWindow.removeAll();
popupWindow.dispose();
}
}
}

最佳答案

问题在于构造函数new JWindow()的使用,这使得新窗口与所有者组件无关。因此,单击弹出窗口的处理方式就像单击 UI 的任何不相关部分一样,从而取消弹出窗口。

您必须使用类似 new JWindow(SwingUtilities.getWindowAncestor(owner)) 的方法来使弹出窗口成为所有者窗口的子窗口。

关于java - Java 1.7 中使用自定义 Popup 时 JMenuItem ActionListener 错过了鼠标单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12876917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com