java中鼠标右击弹出菜单怎样实现
3个回答
展开全部
简单的实现代码如下:
import java.awt.MenuBar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;
/*
* java中鼠标右击弹出菜单怎样实现
*/
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.*;
import java.awt.event.*;
import javax.swing.*;
//左键弹出菜单
public class JPopMenuDemo extends JFrame {
JRadioButtonMenuItem items[]; //菜单项
Color[] colors={Color.blue,Color.pink,Color.yellow,Color.red,Color.orange}; //颜色数组
JPopupMenu popupMenu; //弹出菜单
public JPopMenuDemo()
{
super( "右键弹出菜单" ); //调用父类构造函数
ChangeColorAction action = new ChangeColorAction(); //菜单项事件处理
String[] str = {"Blue","Pink","Yellow","Red","Orange"}; //菜单项名称
ButtonGroup colorGroup=new ButtonGroup(); //实例化按钮组
popupMenu=new JPopupMenu(); //实例化弹出菜单
items=new JRadioButtonMenuItem[5]; //初始化数组
for (int i=0;i items[i]=new JRadioButtonMenuItem(str[i]); //实例化菜单项
popupMenu.add(items[i]); //增加菜单项到菜单上
colorGroup.add(items[i]); //增加菜单项到按钮组
items[i].addActionListener(action); //菜单项事件处理
}
addMouseListener(new MouseAdapter(){ //窗口的鼠标事件处理
public void mousePressed( MouseEvent event ) { //点击鼠标
triggerEvent(event); //调用triggerEvent方法处理事件
}
public void mouseReleased( MouseEvent event ) { //释放鼠标
triggerEvent(event);
}
private void triggerEvent(MouseEvent event) { //处理事件
if (event.isPopupTrigger()) //如果是弹出菜单事件(根据平台不同可能不同)
popupMenu.show(event.getComponent(),event.getX(),event.getY()); //显示菜单
}
});
getContentPane().setBackground(Color.white); //窗口的默认背景色为白色
setSize(230,160); //设置窗口大小
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); //关闭窗口时退出程序
}
class ChangeColorAction implements ActionListener { //菜单项事件处理
public void actionPerformed(ActionEvent event) {
for (int i=0;i if (event.getSource()==items[i]) { //判断事件来自于哪个菜单项
getContentPane().setBackground(colors[i]); //设置窗口背景
repaint(); //重绘窗口
return;
}
}
}
public static void main( String args[]) {
new JPopMenuDemo();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
引用陈信422的回答:
简单的实现代码如下:
import java.awt.MenuBar;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JFrame;import javax.swing.JPopupMenu;/* * java中鼠标右击弹出菜单怎样实现 */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;/* * java中鼠标右击弹出菜单怎样实现 */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.*;
import java.awt.event.*;
import javax.swing.*;
//左键弹出菜单
public class JPopMenuDemo extends JFrame {
JRadioButtonMenuItem items[]; //菜单项
Color[] colors={Color.blue,Color.pink,Color.yellow,Color.red,Color.orange}; //颜色数组
JPopupMenu popupMenu; //弹出菜单
public JPopMenuDemo()
{
super( "右键弹出菜单" ); //调用父类构造函数
ChangeColorAction action = new ChangeColorAction(); //菜单项事件处理
String[] str = {"Blue","Pink","Yellow","Red","Orange"}; //菜单项名称
ButtonGroup colorGroup=new ButtonGroup(); //实例化按钮组
popupMenu=new JPopupMenu(); //实例化弹出菜单
items=new JRadioButtonMenuItem[5]; //初始化数组
for (int i=0;i<items.length;i++){
items[i]=new JRadioButtonMenuItem(str[i]); //实例化菜单项
popupMenu.add(items[i]); //增加菜单项到菜单上
colorGroup.add(items[i]); //增加菜单项到按钮组
items[i].addActionListener(action); //菜单项事件处理
}
addMouseListener(new MouseAdapter(){ //窗口的鼠标事件处理
public void mousePressed( MouseEvent event ) { //点击鼠标
triggerEvent(event); //调用triggerEvent方法处理事件
}
public void mouseReleased( MouseEvent event ) { //释放鼠标
triggerEvent(event);
}
private void triggerEvent(MouseEvent event) { //处理事件
if (event.isPopupTrigger()) //如果是弹出菜单事件(根据平台不同可能不同)
popupMenu.show(event.getComponent(),event.getX(),event.getY()); //显示菜单
}
});
getContentPane().setBackground(Color.white); //窗口的默认背景色为白色
int width = 280;//窗口宽
int height = 360;//窗口高
setSize(width,height); //设置窗口大小
setVisible(true); //设置窗口为可视
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screensize.width-width)/2, (screensize.height-height)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); //关闭窗口时退出程序
}
class ChangeColorAction implements ActionListener { //菜单项事件处理
public void actionPerformed(ActionEvent event) {
for (int i=0;i<=items.length;i++){
if (event.getSource()==items[i]) { //判断事件来自于哪个菜单项
getContentPane().setBackground(colors[i]); //设置窗口背景
repaint(); //重绘窗口
return;
}
}
}
}
public static void main( String args[]) {
new JPopMenuDemo();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//左键弹出菜单
public class JPopMenuDemo extends JFrame {
JRadioButtonMenuItem items[]; //菜单项
Color[] colors={Color.blue,Color.pink,Color.yellow,Color.red,Color.orange}; //颜色数组
JPopupMenu popupMenu; //弹出菜单
public JPopMenuDemo()
{
super( "右键弹出菜单" ); //调用父类构造函数
ChangeColorAction action = new ChangeColorAction(); //菜单项事件处理
String[] str = {"Blue","Pink","Yellow","Red","Orange"}; //菜单项名称
ButtonGroup colorGroup=new ButtonGroup(); //实例化按钮组
popupMenu=new JPopupMenu(); //实例化弹出菜单
items=new JRadioButtonMenuItem[5]; //初始化数组
for (int i=0;i<items.length;i++){
items[i]=new JRadioButtonMenuItem(str[i]); //实例化菜单项
popupMenu.add(items[i]); //增加菜单项到菜单上
colorGroup.add(items[i]); //增加菜单项到按钮组
items[i].addActionListener(action); //菜单项事件处理
}
addMouseListener(new MouseAdapter(){ //窗口的鼠标事件处理
public void mousePressed( MouseEvent event ) { //点击鼠标
triggerEvent(event); //调用triggerEvent方法处理事件
}
public void mouseReleased( MouseEvent event ) { //释放鼠标
triggerEvent(event);
}
private void triggerEvent(MouseEvent event) { //处理事件
if (event.isPopupTrigger()) //如果是弹出菜单事件(根据平台不同可能不同)
popupMenu.show(event.getComponent(),event.getX(),event.getY()); //显示菜单
}
});
getContentPane().setBackground(Color.white); //窗口的默认背景色为白色
int width = 280;//窗口宽
int height = 360;//窗口高
setSize(width,height); //设置窗口大小
setVisible(true); //设置窗口为可视
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screensize.width-width)/2, (screensize.height-height)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); //关闭窗口时退出程序
}
class ChangeColorAction implements ActionListener { //菜单项事件处理
public void actionPerformed(ActionEvent event) {
for (int i=0;i<=items.length;i++){
if (event.getSource()==items[i]) { //判断事件来自于哪个菜单项
getContentPane().setBackground(colors[i]); //设置窗口背景
repaint(); //重绘窗口
return;
}
}
}
}
public static void main( String args[]) {
new JPopMenuDemo();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询