好的,所以在我的INV程序要删除菜单项之前,然后我就像...那太多了。那么如果我为每次删除特定的右键单击项目INSTEAD添加菜单项会怎么样?
因此,如果右键单击第1项,则会在菜单中添加“使用”和“删除”。然后ONCE你选择你的选项,JMenu会删除所有内容,所以它就在我们开始的地方。然后,如果右键单击ITem 2,它将添加“使用”和“取消”。看看我要去哪里?
我尝试自己做,但我无法弄清楚如何做 - 例如,添加新的JMenuItem,你需要这样做:
popup.add(item = new JMenuItem("Cancel"));
item.addActionListener(menuListener);
并且,如您所见,添加一个actionlistener。我不能在if (actItemx == "Item 1") {
下这样做,所以......我该怎么做?
无论如何,这是我到目前为止所拥有的:
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class inv extends JApplet implements MouseListener
{
public JList listbox;
public JPopupMenu popup;
public JMenuItem item;
public void init()
{
ActionListener menuListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String invAction = event.getActionCommand();
int itemSelect = listbox.getSelectedIndex();
Object actItem = listbox.getModel().getElementAt(itemSelect);
System.out.println("Popup menu item [" + invAction + "] [ " + actItem + " ] was pressed.");
}
};
popup = new JPopupMenu();
popup.add(item = new JMenuItem("Use"));
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Drop"));
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Cancel"));
item.addActionListener(menuListener);
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) )
{
System.out.println("Row: " + getRow(e.getPoint()));
listbox.setSelectedIndex(getRow(e.getPoint()));
}
}
}
);
listbox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(listbox);
listbox.setVisible(true);
listbox.setFocusable(false);
listbox.addMouseListener(new MousePopupListener());
}
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())
{
int itemSelectx = listbox.getSelectedIndex();
Object actItemx = listbox.getModel().getElementAt(itemSelectx);
System.out.println("You pressed on " + actItemx);
if (actItemx == "Item 1") {
System.out.println("Removed cancel for " + actItemx);
popup.remove(itemSelectx); // So upon right-click on Item 1, you won't see "Cancel" menu.
}
popup.show(inv.this, e.getX(), e.getY());
popup.revalidate();
}
}
}
private int getRow(Point point)
{
return listbox.locationToIndex(point);
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
答案 0 :(得分:1)
如何为每种类型的项目设置不同的JPopup菜单。我的意思是你有一些看起来像这样的东西:
public JPopupMenu useDropPopup;
public JPopupMenu useCancelPopup;
public void init() {
ActionListener menuListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String invAction = event.getActionCommand();
int itemSelect = listbox.getSelectedIndex();
Object actItem = listbox.getModel().getElementAt(itemSelect);
System.out.println("Popup menu item [" + invAction + "] [ " + actItem + " ] was pressed.");
}
};
useDropPopup = new JPopupMenu();
useCancelPopup = new JPopupMenu();
JMenuItem useMenuItem = new JMenuItem("Use");
useMenuItem.addActionListener(menuListener);
JMenuItem dropMenuItem = new JMenuItem("Drop");
dropMenuItem.addActionListener(menuListener);
JMenuItem cancelMenuItem = new JMenuItem("Cancel");
cancelMenuItem.addActionListener(menuListener);
useDropPopup.add(useMenuItem);
useDropPopup.add(dropMenuItem);
useCancelPopup.add(useMenuItem);
useCancelPopup.add(cancelMenuItem);
// ... etc bring up the appropriate popup depending on the item.
}
此外,您不应将JMenuItem分配给方法调用内的项。这是不好的做法。还要考虑为每个菜单项使用不同的actionlistener,这样你就可以分开每个菜单项的功能代码,例如:
useMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
useMenuAction(event);
}
};
// ... after the init method
public void useMenuAction(ActionEvent evt) {
// Add specific use menu code here.
}
答案 1 :(得分:0)
这主要是对jluzwick答案的补充:
除了创建JMenuItem并向其添加ActionListener之外,您还可以使用Action - 这基本上是ActionListener与名称,可选Icon和其他一些属性的组合。 (大多数只是从AbstractAction扩展,覆盖actionPerformed-Method。)
然后将Action添加到您的JMenu,它将构建JMenuItem本身。 (您也可以在其他位置使用相同的Action对象,如Buttons,“普通”菜单栏等。)