java右键弹出菜单
类别: JAVA教程
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
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
-= 资 源 教 程 =-
文 章 搜 索