java jmenu 点击事件_使用自定义弹出窗口在Java 1.7中由JMenuItem ActionListener错过了鼠标单击...

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

在下面的代码中(主要基于Java Popup Demo),弹出窗口将出现在鼠标右键上。弹出菜单项在鼠标翻转时会突出显示,点击JmenuItem会弹出消失;然而,JMenuItem的actionEvent在点击时不会被触发(应该在JTextArea中报告)。

其他金块:

如果我键入JMenuItem的助记符(这里是“a”),那么会触发actionEvent(事件在JTextArea中报告)。

如果我没有附加自定义Popup(工厂),那么点击鼠标按照预期触发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)此代码表现良好:

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)任何帮助/想法,非常感谢!独立的代码示例如下。

谢谢

安德鲁

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JMenuItem的addActionListener()方法来添加一个监听器,当用户点菜单项时,监听器会被调用。在监听器,你可以使用JFileChooser对话框来让用户选择要更换的图片。 以下是一个简单的示例代码,实现了这个功能: ``` import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; public class ChangeImageMenuItem extends JFrame { private JLabel label; private JFileChooser fileChooser; private ImageIcon image; public ChangeImageMenuItem() { super("Change Image on MenuItem Click"); image = new ImageIcon("image.jpg"); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); JMenuItem menuItem = new JMenuItem("Change Image"); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { fileChooser = new JFileChooser(); FileFilter filter = new FileNameExtensionFilter("Images", "jpg", "jpeg", "png", "gif"); fileChooser.setFileFilter(filter); int result = fileChooser.showOpenDialog(ChangeImageMenuItem.this); if (result == JFileChooser.APPROVE_OPTION) { image = new ImageIcon(fileChooser.getSelectedFile().getAbsolutePath()); label.setIcon(image); } } }); menu.add(menuItem); menuBar.add(menu); setJMenuBar(menuBar); label = new JLabel(image); getContentPane().add(label, BorderLayout.CENTER); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); } public static void main(String[] args) { new ChangeImageMenuItem(); } } ``` 在这个示例,我们创建了一个JFrame窗口,并在其添加了一个JLabel,用于显示图片。然后,我们创建了一个JMenuBar,并在其添加了一个JMenu和一个JMenuItem。我们使用JMenuItem的addActionListener()方法来添加一个监听器,当用户点菜单项时,监听器会被调用。在监听器,我们创建了一个JFileChooser对话框,并设置了文件过滤器,以确保用户只能选择图片文件。然后,我们显示对话框,并在用户选择了文件后,使用JLabel的setIcon()方法来更换图片。最后,我们将JLabel添加到窗口,并设置窗口的大小和可见性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值