java 右键菜单_界面操作--添加右键菜单

[java]代码库package 添加右键菜单;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;

/*

* java中鼠标右击弹出菜单怎样实现

*/

public class main

{

public static void main(String[] args)

{

final JFrame jf = new JFrame("窗体");

jf.setSize(300, 300);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jf.setLocationRelativeTo(null);

// 创建弹出菜单

final JPopupMenu jp = new JPopupMenu();

JMenuItem item1 = new JMenuItem("功能1");

item1.addMouseListener(new MouseAdapter()

{

@Override

public void mouseReleased(MouseEvent e)

{

if (e.getButton() == MouseEvent.BUTTON1)

{

System.out.println("左键单击");

}

}

});

jp.add(item1);

jf.addMouseListener(new MouseAdapter()

{

@Override

public void mouseClicked(MouseEvent e)

{

if (e.getButton() == MouseEvent.BUTTON3)

{

// 弹出菜单

jp.show(jf, e.getX(), e.getY());

}

}

});

jf.setVisible(true);

}

}

694748ed64b9390909c0d88230893790.png

你可能感兴趣的:(java,右键菜单)