当前位置: 技术问答>java相关
如何在控件上按右键弹出自己的菜单?有具体例子吗?
来源: 互联网 发布时间:2015-01-02
本文导语: | //=========Test.java============================ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test extends JApplet { JButton jButton1 = new JButton(); FlowLayout flowLayout1 = new FlowLayout(); PopupTest pt=new PopupTest(); ...
|
//=========Test.java============================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet
{
JButton jButton1 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
PopupTest pt=new PopupTest();
public Test()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
jButton1.setText("jButton1");
pt.setHostSrc(jButton1);
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
});
this.getContentPane().setLayout(flowLayout1);
this.getContentPane().add(jButton1, null);
}
void jButton1_mousePressed(MouseEvent e)
{
pt.showUp(e);
}
}
///============File PopupTest.java======================
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class PopupTest extends JPopupMenu
{
Component compSrc;
public PopupTest()
{
add(new ImagePopupMenuHandle(1,"Test menu item1"));
this.setDefaultLightWeightPopupEnabled(false);
}
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)
class ImagePopupMenuHandle extends AbstractAction
{
int handleType;//this is used to distinct which action to do
public ImagePopupMenuHandle(int type,String title)
{
super(title);
handleType = type;
}
public void actionPerformed(ActionEvent e)
{
switch(handleType)
{
case 1:
System.out.println("menu clicked");
break;
}
}
}
}//end of class PopupTest extends JPopupMenu
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JApplet
{
JButton jButton1 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
PopupTest pt=new PopupTest();
public Test()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
jButton1.setText("jButton1");
pt.setHostSrc(jButton1);
jButton1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
jButton1_mousePressed(e);
}
});
this.getContentPane().setLayout(flowLayout1);
this.getContentPane().add(jButton1, null);
}
void jButton1_mousePressed(MouseEvent e)
{
pt.showUp(e);
}
}
///============File PopupTest.java======================
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class PopupTest extends JPopupMenu
{
Component compSrc;
public PopupTest()
{
add(new ImagePopupMenuHandle(1,"Test menu item1"));
this.setDefaultLightWeightPopupEnabled(false);
}
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)
class ImagePopupMenuHandle extends AbstractAction
{
int handleType;//this is used to distinct which action to do
public ImagePopupMenuHandle(int type,String title)
{
super(title);
handleType = type;
}
public void actionPerformed(ActionEvent e)
{
switch(handleType)
{
case 1:
System.out.println("menu clicked");
break;
}
}
}
}//end of class PopupTest extends JPopupMenu