java swing中如何实现右键菜单呢?
直接上代码:
/*** java
* 设置弹出菜单
* @param qrResultLabel
*/
private void setPopupMenu(JComponent qrResultLabel)
{
final MyMenuActionListener myMenuListener=new MyMenuActionListener(this);
qrResultLabel.addMouseListener(new MouseInputAdapter()
{
@Override
public void mouseReleased(MouseEvent e)
{
// super.mousePressed(e);
if (e.getButton() == MouseEvent.BUTTON3)
{
JPopupMenu textMenu = new JPopupMenu();
JMenuItem cleanUpM = new JMenuItem(MenuUtil2.ACTION_STR_CLEANUP);
JMenuItem copy22M = new JMenuItem(
MenuUtil2.ACTION_IMAGE_COPY);
JMenuItem paste22M = new JMenuItem(
MenuUtil2.ACTION_IMAGE_PASTE);
JMenuItem enlargeM = new JMenuItem(
MenuUtil2.ACTION_ENLARGE);
JMenuItem reduceM = new JMenuItem(
MenuUtil2.ACTION_REDUCE);
// JMenuItem pasteM = new JMenuItem(MenuUtil2.ACTION_STR_PASTE);
JMenuItem exportM = new JMenuItem(
MenuUtil2.ACTION_STR_EXPORT);
JMenuItem readQRCodeM = new JMenuItem(
MenuUtil2.ACTION_READ_QR_CODE);
copy22M.addActionListener(myMenuListener);
cleanUpM.addActionListener(myMenuListener);
exportM.addActionListener(myMenuListener);
enlargeM.addActionListener(myMenuListener);
reduceM.addActionListener(myMenuListener);
paste22M.addActionListener(myMenuListener);
readQRCodeM.addActionListener(myMenuListener);
textMenu.add(cleanUpM);
textMenu.add(copy22M);
textMenu.add(paste22M);
// exportM.add(pasteM);
textMenu.add(exportM);
textMenu.add(enlargeM);
textMenu.add(reduceM);
textMenu.add(readQRCodeM);
textMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
}
注意:
1,鼠标右键对应的code是:MouseEvent.BUTTON3(3)
2,弹出菜单要使用JPopupMenu
时间: 2024-11-02 14:18:09