[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);
}
}
初级程序员
by: 云代码会员 发表于:2017-06-04 15:16:14 顶(0) | 踩(0) 回复
谢谢
回复评论