我试图为我的JMenuItems添加一个动作监听器,以进行右键单击。
我看到许多示例,它们使用item.addActionListener(this)向JMenuItem添加侦听器

 JMenuItem item;
 item.addActionListener(this);


但是我尝试了一下,但出现错误

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






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是未实现JPanelActionListener ...

看看How to Write an Action ListenersHow to Use Menus,您可能会与How to Use Actions成为农场主

您也可以使用JComponent#setComponentPopupMenu而不是MouseListener,并且应确保您是从事件调度线程的上下文中构建用户界面的,请查看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/

10-10 20:18