gpt4 book ai didi

java - 添加 JMenuItems 的有效方法

转载 作者:行者123 更新时间:2023-12-01 15:56:59 26 4
gpt4 key购买 nike

好吧,在我的 INV 程序是删除菜单项之前,但后来我就想……这太多了。那么,如果我为特定的右键单击项目添加菜单项而不是每次都删除呢?

因此,如果您右键单击第 1 项,则会将“使用”和“放置”添加到菜单中。然后,一旦您选择了选项,JMenu 就会删除所有内容,因此它就在我们开始的地方。然后,如果您右键单击 ITem 2,它会添加“使用”和“取消”。看到我要去哪里了吗?

我尝试自己做,但我就是不知道该怎么做——例如,要添加一个新的 JMenuItem,您需要这样做:

popup.add(item = new JMenuItem("Cancel"));
item.addActionListener(menuListener);

并且,如您所见,添加一个 Action 监听器。我无法在 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)
{
}
}

最佳答案

为每种类型的项目设置不同的 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 分配给方法调用内部的项目。这是不好的做法。还可以考虑为每个菜单项使用不同的 Action 监听器,以便您可以分离每个菜单项的功能代码,例如:

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.
}

关于java - 添加 JMenuItems 的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4878016/

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