public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setSize(400,400);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
//给面板添加鼠标右键 MouseAdapter用于适配所有方法
panel.addMouseListener(new MouseAdapter() {
@Override //鼠标弹起,释放
public void mouseReleased(MouseEvent e) { //e有此次事件包含的所有信息
// System.out.println(e.getButton());
// System.out.println(e.isMetaDown());//是不是右键
if(e.isMetaDown()){
//创建弹出菜单
JPopupMenu meun=new JPopupMenu("右键菜单");
//1 菜单项
JMenuItem findMenuItem=new JMenuItem("查找");
//2 菜单
JMenu editMenuItem=new JMenu("编辑");
//编辑菜单中添加菜单选项
JMenuItem copyMeunItem=new JMenuItem("复制");
JMenuItem pastMeunItem=new JMenuItem("粘贴");
editMenuItem.add(copyMeunItem);
editMenuItem.add(pastMeunItem);
meun.add(findMenuItem);
meun.addSeparator();
meun.add(editMenuItem);
//粘贴事件 给粘贴按钮添加事件
pastMeunItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("发送粘贴事件");
}
});
//显示
meun.show(e.getComponent(),e.getX(),e.getY());
}
}
});
jf.setContentPane(panel);
jf.setVisible(true);
}
Swing测试弹出菜单
最新推荐文章于 2021-02-24 16:46:09 发布