WEB开发网
开发学院WEB开发Jsp java右键弹出菜单 阅读

java右键弹出菜单

 2008-01-05 09:03:32 来源:WEB开发网   
核心提示:package test1; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Applet1 extends Applet { boolean isStandalone = false; PopupMenu p

  package test1;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Applet1 extends Applet {
boolean isStandalone = false;
PopupMenu popupMenu1 = new PopupMenu();
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
MenuItem menuItem3 = new MenuItem();
Button button1 = new Button();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getPRoperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**ConstrUCt the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
menuItem1.setLabel("1");
menuItem2.setLabel("2");
menuItem3.setLabel("3");
button1.setLabel("button1");
file://这是button1的监听事件,
button1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
button1_mousePressed(e);
}
});
popupMenu1.add(menuItem1);
popupMenu1.add(menuItem2);
popupMenu1.add(menuItem3);
this.add(button1, null);
add(popupMenu1);
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
void button1_mousePressed(MouseEvent e) {
/*通过InputEvent.BUTTON3_MASK对右键的判定
要判定是左键用InputEvent.BUTTON1_MASK,
*/
int mods=e.getModifiers();
if((mods&InputEvent.BUTTON3_MASK)!=0)
{
popupMenu1.show(button1,e.getX(),e.getY());
button
}
}
}
file://applet1.java is over

Tags:java 菜单

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接