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

编程入门 行业动态 更新时间:2024-10-27 16:32:07
本文介绍了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 右键单击​​显示菜单(使用、删除、取消)

本文发布于:2023-08-06 05:08:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:右键   单击   菜单   JList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!