java中鼠标右击弹出菜单怎样实现
答案:2 悬赏:70 手机版
解决时间 2021-02-01 18:16
- 提问者网友:你独家记忆
- 2021-02-01 01:48
java中鼠标右击弹出菜单怎样实现
最佳答案
- 五星知识达人网友:撞了怀
- 2021-02-01 02:19
简单的实现代码如下:
import java.awt.MenuBar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
public class Demo {
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();
jp.add("红色");
jp.add("蓝色");
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);
}
}
import java.awt.MenuBar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
public class Demo {
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();
jp.add("红色");
jp.add("蓝色");
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);
}
}
全部回答
- 1楼网友:傲气稳了全场
- 2021-02-01 02:45
简单的实现代码如下:
import java.awt.menubar;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import javax.swing.jframe;
import javax.swing.jpopupmenu;
public class demo {
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();
jp.add("红色");
jp.add("蓝色");
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);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯