如何 select JMenuItem 仅在 JPopupMenu 内单击鼠标左键?
How to select JMenuItem only with left-click inside a JPopupMenu?
我想通过右键单击弹出 table 上的菜单(在示例中有效),但我想让菜单项仅通过左键单击(并通过右键取消菜单) -clicks,但现在左键和右键单击都会调用该操作)。
我该怎么做?我尝试了 MouseListeners,但无法正常工作。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class PopupDemo {
protected void initUI() {
final JFrame frame = new JFrame(PopupDemo.class.getSimpleName());
Vector<String> columns = new Vector<String>(Arrays.asList("Coumn 1", "Column 2"));
Vector<Vector<String>> data = new Vector<Vector<String>>();
for (int i = 0; i < 5; i++) {
Vector<String> row = new Vector<String>();
for (int j = 0; j < columns.size(); j++) {
row.add("Cell " + (i + 1) + "," + (j + 1));
}
data.add(row);
}
final JTable table = new JTable(data, columns);
final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem deleteItem = new JMenuItem("Delete");
deleteItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "DELETE OPTION CHOOSED");
}
});
popupMenu.add(deleteItem);
table.setComponentPopupMenu(popupMenu);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PopupDemo().initUI();
}
});
}
}
在 deleteItem 上,您要做的是在 MouseListener 内部检查单击了哪个鼠标按钮
deleteItem.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
// do whatever it is when the right button is pressed
} else {
// do whatever it is when the left button is pressed
}
}
});
我想通过右键单击弹出 table 上的菜单(在示例中有效),但我想让菜单项仅通过左键单击(并通过右键取消菜单) -clicks,但现在左键和右键单击都会调用该操作)。
我该怎么做?我尝试了 MouseListeners,但无法正常工作。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class PopupDemo {
protected void initUI() {
final JFrame frame = new JFrame(PopupDemo.class.getSimpleName());
Vector<String> columns = new Vector<String>(Arrays.asList("Coumn 1", "Column 2"));
Vector<Vector<String>> data = new Vector<Vector<String>>();
for (int i = 0; i < 5; i++) {
Vector<String> row = new Vector<String>();
for (int j = 0; j < columns.size(); j++) {
row.add("Cell " + (i + 1) + "," + (j + 1));
}
data.add(row);
}
final JTable table = new JTable(data, columns);
final JPopupMenu popupMenu = new JPopupMenu();
JMenuItem deleteItem = new JMenuItem("Delete");
deleteItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "DELETE OPTION CHOOSED");
}
});
popupMenu.add(deleteItem);
table.setComponentPopupMenu(popupMenu);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new PopupDemo().initUI();
}
});
}
}
在 deleteItem 上,您要做的是在 MouseListener 内部检查单击了哪个鼠标按钮
deleteItem.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
// do whatever it is when the right button is pressed
} else {
// do whatever it is when the left button is pressed
}
}
});