JList 右键单击​​显示菜单(使用、删除、取消)

JList Right-click shows menu (Use, drop, cancel)(JList 右键单击​​显示菜单(使用、删除、取消))

本文介绍了JList 右键单击​​显示菜单(使用、删除、取消)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上搜索这个答案.我有一个简单的 JList,里面有项目.当我右键单击时,我希望弹出一个菜单,上面写着使用、删除、取消"或类似的东西.但是,我被难住了.

I've been scouring the internet for this answer. I have a simple JList with items inside it. When I right-click, I want a menu to pop-up that says "Use, drop, cancel" or something of that nature. HOWEVER, I'm stumped.

下面的代码将生成一个简单的 JList,其中包含一些项目.我尝试在代码中添加右键单击,但它不起作用.帮忙?

The code below will produce a simple JList with a few items inside. I tried adding a right-click in the code but it doesn't work. Help?

这是我目前所拥有的:

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import javax.swing.*;



public class inv extends JApplet implements MouseListener {

    JList listbox;



     public void init()
     {


    String  listData[] = { "Item 1","Item 2","Item 3","Item 4" };
listbox = new JList( listData );

     listbox.addMouseListener( new MouseAdapter()
     {
        public void mousePressed(MouseEvent e)
        {
            if ( SwingUtilities.isRightMouseButton(e) )
            {
                listbox.setSelectedIndex(getRow(e.getPoint()));
            }
        }
     });

    listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


        add(listbox);
                    listbox.setVisible(true);

            listbox.setFocusable(false);

     }



     private int getRow(Point point)
     {
        return listbox.locationToIndex(point);
}

      public void mousePressed(MouseEvent e) {
      }

      public void mouseReleased(MouseEvent e) {
      }

      public void mouseEntered(MouseEvent e) {
      }

      public void mouseExited(MouseEvent e) {
      }

      public void mouseClicked(MouseEvent e) {
      }


     public void stop()
     {
     }

     public void paint(Graphics g)
     {
     }

}

推荐答案

一个典型的错误可能是调用 JPopupMenu.setVisible(true) 并期望某些事情发生.该组件需要不同的方法来启动它.重写你的鼠标监听器:

One of the typical mistakes may be to call JPopupMenu.setVisible(true) and expect something to happen. This component needs a different method to bring it up. Rewrite your mouse listener along the lines:

    listbox.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.isPopupTrigger()) {
                JPopupMenu menu = new JPopupMenu();
                JMenuItem item = new JMenuItem("Say hello");
                item.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane.showMessageDialog(inv.this, "Hello "
                                + listbox.getSelectedValue());
                    }
                });
                menu.add(item);
                menu.show(inv.this, 5, listbox.getCellBounds(
                        listbox.getSelectedIndex() + 1,
                        listbox.getSelectedIndex() + 1).y);
            }
        }
    });

为了简短起见,我只添加了一项,但肯定可以添加更多.我使用的 show 方法还需要指定菜单应该出现在组件上的哪个位置.如本例所示,可以从列表本身获取位置.

To make example short, I add one item only but surely more can be added. The show method I use also requires to specify where on the component the menu should appear. The location can be obtained from the list itself as seen in this example.

这篇关于JList 右键单击​​显示菜单(使用、删除、取消)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:JList 右键单击​​显示菜单(使用、删除、取消)

基础教程推荐