gpt4 book ai didi

java - jMenuItem.addMouseListener(this) 上的错误

转载 作者:行者123 更新时间:2023-12-01 17:06:25 44 4
gpt4 key购买 nike

我尝试向我的 JMenuItems 添加一个 Action 监听器以进行右键单击。我看到很多例子,他们使用 item.addActionListener(this) 向 JMenuItem 添加监听器

 JMenuItem item;
item.addActionListener(this);

但是我尝试了一下,却收到错误

 the method addActionListener(ActionListener) in the type AbstractButton is not 
applicable for the arguements (PopUpMenuExample)

error

代码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.BevelBorder;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class PopUpMenuExample extends JPanel {

public JPopupMenu popup;

public PopUpMenuExample() {
popup = new JPopupMenu();

JMenuItem item;
popup.add(item = new JMenuItem("Add"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(this); <-- error!

popup.add(item = new JMenuItem("Delete"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(this); <-- error!

popup.setLabel("Justification");
popup.setBorder(new BevelBorder(BevelBorder.RAISED));
popup.addPopupMenuListener(new PopupPrintListener());

addMouseListener(new MousePopupListener());
}

// An inner class to check whether mouse events are the popup trigger
class MousePopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
checkPopup(e);
}

public void mouseClicked(MouseEvent e) {
checkPopup(e);
}

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

private void checkPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(PopUpMenuExample.this, e.getX(), e.getY());
}
}
}

public static void main(String s[]) {
JFrame frame = new JFrame("Popup Menu Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new PopUpMenuExample());
frame.setSize(300, 300);
frame.setVisible(true);
}
}
<小时/>

已编辑(在 JPopUpMenu 中添加 JMenuitem)

table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == java.awt.event.MouseEvent.BUTTON3) {
System.out.println("Right Click");
int r = table.rowAtPoint(e.getPoint());
if (r >= 0 && r < table.getRowCount()) {
table.setRowSelectionInterval(r, r);

} else {
table.clearSelection();
}

int rowindex = table.getSelectedRow();
if (rowindex < 0)
return;
if (e.isPopupTrigger() && e.getComponent() instanceof JTable ) {
JPopupMenu popup = new JPopupMenu();
JMenuItem item;
popup.add(item = new JMenuItem("Add"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addMouseListener(this);
popup.add(item = new JMenuItem("Delete"));
item.setHorizontalTextPosition(JMenuItem.RIGHT);;
popup.setComponentPopupMenu(popup);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}

最佳答案

this 是一个 JPanel,它没有实现 ActionListener...

看看How to Write an Action ListenersHow to Use Menus你不妨用 How to Use Actions 来农场

您还可以使用JComponent#setComponentPopupMenu不要使用 MouseListener 并且您应该确保在事件调度线程的上下文中构建 UI,请查看 Initial Threads了解更多详情

弹出示例

对我来说效果很好......

JTable table = new JTable(model);

JPopupMenu popupMenu = new JPopupMenu();
JMenuItem mi = popupMenu.add("Boo!");
mi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Ah, a ghost!");
}
});
table.setComponentPopupMenu(popupMenu);

关于java - jMenuItem.addMouseListener(this) 上的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378618/

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