怎么样鼠标右击事件功能? 假设我想右击鼠标时,能够自动弹出一个可以供你选择的面板选项,就像是菜单一样的条目,我该怎么实现这种功能呢?有请高手指点迷津!谢谢拉! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 创建一个JPopupMenu,添加你的菜单项,最后在鼠标事件处理方法里把他show出来。 package socket;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JComboBoxDemo extends JFrame{ String[] s={"西瓜","苹果","草莓","香蕉","葡萄"}; JComboBox combo=new JComboBox(s); JPopupMenu popupMenu = new JPopupMenu(); JMenuItem jadd = new JMenuItem("add"); JMenuItem jremove = new JMenuItem("remove"); public JComboBoxDemo(){ super("JComboBoxDemo"); this.setSize(200,200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = this.getContentPane(); c.setLayout(new FlowLayout(FlowLayout.LEADING)); this.getContentPane().add(combo); JButton add = new JButton("add"); JButton remove = new JButton("remove"); add.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ combo.addItem("test"); } }); remove.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ combo.removeAllItems(); } }); jadd.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ combo.addItem("test"); } }); jremove.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ combo.removeAllItems(); } }); popupMenu.add(jadd); popupMenu.addSeparator(); popupMenu.add(jremove); this.getContentPane().add(add); this.getContentPane().add(remove); this.addMouseListener(new MouseAdapter(){ //添加右键菜单 public void mouseReleased(MouseEvent e){ if (e.isPopupTrigger()) { popupMenu.show(e.getComponent(), e.getX(), e.getY()); } } }); this.setVisible(true); } public static void main(String[] args){ new JComboBoxDemo(); } } 将一个字符串中的"\"全部转换为"/" 我的布局老是对不齐,请教大虾 如何去掉String中的所有空格 Java程序如何自我保护,不被结束,像杀毒软件 怎样写个在指定时间段运行一个线程 字符串高手请进 CSDN.南京区程序员CS大赛正在报名中... 用Swing画的界面(Java默认的显示风格)想变成Windows的风格可以吗? sun one studio中对于他自己产生的一些代码不允许编辑,如何才能去掉这个特性啊? 一个hello,world的程序出错。 搞不明白~~~~~~~ 问个函数 StrTo...
import java.awt.event.*;import javax.swing.*;public class JComboBoxDemo extends JFrame{ String[] s={"西瓜","苹果","草莓","香蕉","葡萄"};
JComboBox combo=new JComboBox(s);
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem jadd = new JMenuItem("add");
JMenuItem jremove = new JMenuItem("remove");
public JComboBoxDemo(){
super("JComboBoxDemo");
this.setSize(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = this.getContentPane();
c.setLayout(new FlowLayout(FlowLayout.LEADING));
this.getContentPane().add(combo);
JButton add = new JButton("add");
JButton remove = new JButton("remove");
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
combo.addItem("test");
}
});
remove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
combo.removeAllItems();
}
});
jadd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
combo.addItem("test");
}
});
jremove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
combo.removeAllItems();
}
});
popupMenu.add(jadd);
popupMenu.addSeparator();
popupMenu.add(jremove);
this.getContentPane().add(add);
this.getContentPane().add(remove);
this.addMouseListener(new MouseAdapter(){
//添加右键菜单
public void mouseReleased(MouseEvent e){
if (e.isPopupTrigger()) {
popupMenu.show(e.getComponent(), e.getX(), e.getY());
}
}
});
this.setVisible(true);
}
public static void main(String[] args){
new JComboBoxDemo();
}
}