当前位置: 技术问答>java相关
有谁就用PopupMenu在applet里面写弹出菜单,请给个例子
来源: 互联网 发布时间:2015-01-02
本文导语: | //==============PopupTest.java================= import java.awt.*; import java.awt.event.*; public class PopupTest extends PopupMenu { Component compSrc; MenuItem mi=new MenuItem("item1"); public PopupTest() { add(mi); ...
|
//==============PopupTest.java=================
import java.awt.*;
import java.awt.event.*;
public class PopupTest extends PopupMenu
{
Component compSrc;
MenuItem mi=new MenuItem("item1");
public PopupTest()
{
add(mi);
mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("item 1 click");
}
});
}
public void setHostSrc(Component src)
{
compSrc=(Component)src;
}
public int showUp(MouseEvent e)
{//this popup is visible, return >0, else return 0;
int nRet=0;
int mods=e.getModifiers();
if((mods&InputEvent.BUTTON3_MASK)!=0)
{
show(compSrc,e.getX(),e.getY());
nRet=1;
}
return nRet;
}//end of int showUp(MouseEvent e)
}//end of class PopupTest extends JPopupMenu
//===============Test.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet
{
Button jButton1 = new Button("jButton1");
FlowLayout flowLayout1 = new FlowLayout();
PopupTest pt=new PopupTest();
public Test()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
pt.setHostSrc(jButton1);
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
});
setLayout(flowLayout1);
add(jButton1);
add(pt);
}
void jButton1_mousePressed(MouseEvent e)
{
pt.showUp(e);
}
}
import java.awt.*;
import java.awt.event.*;
public class PopupTest extends PopupMenu
{
Component compSrc;
MenuItem mi=new MenuItem("item1");
public PopupTest()
{
add(mi);
mi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("item 1 click");
}
});
}
public void setHostSrc(Component src)
{
compSrc=(Component)src;
}
public int showUp(MouseEvent e)
{//this popup is visible, return >0, else return 0;
int nRet=0;
int mods=e.getModifiers();
if((mods&InputEvent.BUTTON3_MASK)!=0)
{
show(compSrc,e.getX(),e.getY());
nRet=1;
}
return nRet;
}//end of int showUp(MouseEvent e)
}//end of class PopupTest extends JPopupMenu
//===============Test.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet
{
Button jButton1 = new Button("jButton1");
FlowLayout flowLayout1 = new FlowLayout();
PopupTest pt=new PopupTest();
public Test()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
pt.setHostSrc(jButton1);
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
});
setLayout(flowLayout1);
add(jButton1);
add(pt);
}
void jButton1_mousePressed(MouseEvent e)
{
pt.showUp(e);
}
}