本文将通过实例介绍如何用鼠标右键启动弹出式菜单,以及如何执行菜单的
指令。
大家对弹出式菜单(popup menu)的使用相信都非常熟悉了。
我们将通过如下的例子解释如何在java中使用右键和弹出式菜单:主程序是一个java application,
叫userightbutton,它上面是一个jpanel,在此jpanel上有一个jlabel,用以显示弹出式菜单
中指令执行的结果。我们要求当用鼠标右键点击jlabel或者jpanel上时弹出一个菜单,菜单
中有"say hello","say hello again","say byebye"三个选项。选择其中的任何一个指令,将在
jlabel中显示对应的string.
在本例中有两个class。一个是userightbutton (java应用程序),另一个是mypopupmenu
(弹出式窗口)。userightbutton的源程序如下:
import javax.swing.*;import java.awt.event.mouseevent;import java.awt.event.mouselistener;import java.awt.*;public class userightbutton extends jframe implements mouselistener{ jpanel panel; jlabel display; public userightbutton() { settitle("use right button and popup menu"); setsize(400,300); panel = new jpanel(); display = new jlabel(" "); display.setopaque(true); display.setbackground(color.yellow); panel.add(display); panel.addmouselistener(this); display.addmouselistener(this); getcontentpane().setlayout(new borderlayout()); getcontentpane().add(panel, borderlayout.center); } public static void main(string[] args) { userightbutton rb = new userightbutton(); rb.setvisible(true); } public void mousepressed(mouseevent e) { if (e.getsource() == panel && e.getbutton() == mouseevent.button3) { mypopupmenu popup = new mypopupmenu(this); popup.show((component)panel, e.getx(), e.gety()); } else if (e.getsource() == display && e.getbutton() == mouseevent.button3) { mypopupmenu popup = new mypopupmenu(this); popup.show((component)display, e.getx(), e.gety()); } } public void mouseentered(mouseevent e){} public void mouseexited(mouseevent e){} public void mouseclicked(mouseevent e){} public void mousereleased(mouseevent e){}}
从上面的程序我们看到:
和使用鼠标左键一样,我们是用 来控制鼠标事件。我们所要做的是限制 mouseevent 响应鼠标右键的点击,不响应鼠标左键的点击。这可由下面的方法来实现
e.getbutton() == mouseevent.button3
其中 getbutton() 方法返回一个整数,
mouseevent.button1,
mouseevent.button2 或者
mouseevent.button3。
mouseevent.button1 代表左键,
mouseevent.button3 代表右键。如果你的鼠标有三个
键的话,mouseevent.button2 代表中间的键。
jlabel (display)仅仅占据 jpanel 的一小部分(我们用黄色背景显示它的大小和位置)。
而我们要求当右键电击jpanel,包括jlabel,都要弹出菜单,所以我们把jpanel和
jlabel都加上mouselistener。
弹出式菜单class的源程序如下:
import javax.swing.*;import java.awt.event.actionlistener;import java.awt.event.actionevent;public class mypopupmenu extends jpopupmenu implements actionlistener{ jmenuitem sayhello, sayhelloagain, saybyebye; userightbutton userightbutton; public mypopupmenu(userightbutton urb) { userightbutton = urb; sayhello = new jmenuitem("say hello"); sayhelloagain = new jmenuitem("say hello again"); saybyebye = new jmenuitem("say bye bye"); sayhello.addactionlistener(this); sayhelloagain.addactionlistener(this); saybyebye.addactionlistener(this); add(sayhello); this.addseparator(); add(sayhelloagain); add(saybyebye); } public void actionperformed(actionevent e) { if (e.getsource() == sayhello) { system.out.println(); userightbutton.display.settext("hello!"); } else if (e.getsource() == sayhelloagain) { system.out.println("hello! hello!"); userightbutton.display.settext("hello! hello!"); } else if (e.getsource() == saybyebye) { system.out.println("bye bye!"); userightbutton.display.settext("bye bye!"); } }}
这个程序很简单,是一个标准的jpopupmenu。唯一需要指出的是在
constructor中我们引入该弹出式菜单的 base 类,userightbutton。 这是因为
我们要返回弹出式窗口中指令执行的结果到原来的gui界面上。很显然我们
不能用 userightbutton = new userightbutton(),因为两个class不能互相引用。
对于一个复杂的应用程序,我们应该使用model-view-controller架构来作,即
用弹出式菜单当controller,用jlabel (display)作为view, 再写一个model类来接
收来自弹出式菜单的string,并自动更新jlabel中的显示。
从以上的例子我们看到利用鼠标右键来启动弹出式菜单是非常容易的。
闽公网安备 35060202000074号