class friend extends JLabel{
private accustom a;
String nicheng;
String zaixian="";
public static PopupMenu popupMenu1 = new PopupMenu(); //弹出菜单
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
MenuItem menuItem3 = new MenuItem();
public friend(){}//缺省构造方法
public friend(accustom b){
Font f = new Font("新宋体",Font.PLAIN,16);
this.add(popupMenu1);
popupMenu1.add(menuItem1);
popupMenu1.add(menuItem2);
popupMenu1.add(menuItem3);
//this.setFont(f);
//this.setOpaque(true);
this.setOpaque(false);
this.a=b;
this.setForeground(Color.BLACK);
this.setBackground(Color.darkGray);
nicheng=a.getNiCheng();//获取昵称
String image=a.getImage();//获取头像名称
System.out.print(image+"\n");
if(image==null)
image="image/3.jpg";
ImageIcon face=new ImageIcon(image);
this.setIcon(face);
this.setText(nicheng+"("+a.getZaiXian()+")");
this.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseReleased(java.awt.event.MouseEvent e){
if(e.getClickCount()==1&&e.getButton()==MouseEvent.BUTTON3)
//popupMenu1.show(this.getContatin(),0,0);
if(e.getClickCount()==2&&a.getZaiXian().equals("在线")){
//landSocket server=new landSocket();
//server.cout(3+"");
//server.cout(getAccustom().getZhangHao());//发送帐号
//String str=server.cin();
//System.out.print(server.cin());
new chatThread(getAccustom()).start();
}
if(e.getClickCount()==2&&a.getZaiXian().equals("离线"))
JOptionPane.showMessageDialog(null,"对方不在线!");
}
public void mouseEntered(java.awt.event.MouseEvent e){
setOpaque(true);
setBackground(new Color(232,249,255));
repaint();
}
public void mouseExited(java.awt.event.MouseEvent e){
setOpaque(false);
System.out.print("go");
repaint();
}
}
);
}
public void setZaiXian(String zaixian){
this.setText(nicheng+"("+zaixian+")");
}
public accustom getAccustom(){//返回一个friend对象对应的accustom对象
return this.a;
}
}我想创建一个Label类,在此类构造方法中添加鼠标监听器,但当添加弹出菜单时需要调用show()方法,此方法中的一个参数是菜单所出现时所在的组件,但在构造方法中怎能获得此构造方法所构造的对象本身呀?我想的是能不能通过某种方法在构造方法中获得对象本身
private accustom a;
String nicheng;
String zaixian="";
public static PopupMenu popupMenu1 = new PopupMenu(); //弹出菜单
MenuItem menuItem1 = new MenuItem();
MenuItem menuItem2 = new MenuItem();
MenuItem menuItem3 = new MenuItem();
public friend(){}//缺省构造方法
public friend(accustom b){
Font f = new Font("新宋体",Font.PLAIN,16);
this.add(popupMenu1);
popupMenu1.add(menuItem1);
popupMenu1.add(menuItem2);
popupMenu1.add(menuItem3);
//this.setFont(f);
//this.setOpaque(true);
this.setOpaque(false);
this.a=b;
this.setForeground(Color.BLACK);
this.setBackground(Color.darkGray);
nicheng=a.getNiCheng();//获取昵称
String image=a.getImage();//获取头像名称
System.out.print(image+"\n");
if(image==null)
image="image/3.jpg";
ImageIcon face=new ImageIcon(image);
this.setIcon(face);
this.setText(nicheng+"("+a.getZaiXian()+")");
this.addMouseListener(new java.awt.event.MouseAdapter(){
public void mouseReleased(java.awt.event.MouseEvent e){
if(e.getClickCount()==1&&e.getButton()==MouseEvent.BUTTON3)
//popupMenu1.show(this.getContatin(),0,0);
if(e.getClickCount()==2&&a.getZaiXian().equals("在线")){
//landSocket server=new landSocket();
//server.cout(3+"");
//server.cout(getAccustom().getZhangHao());//发送帐号
//String str=server.cin();
//System.out.print(server.cin());
new chatThread(getAccustom()).start();
}
if(e.getClickCount()==2&&a.getZaiXian().equals("离线"))
JOptionPane.showMessageDialog(null,"对方不在线!");
}
public void mouseEntered(java.awt.event.MouseEvent e){
setOpaque(true);
setBackground(new Color(232,249,255));
repaint();
}
public void mouseExited(java.awt.event.MouseEvent e){
setOpaque(false);
System.out.print("go");
repaint();
}
}
);
}
public void setZaiXian(String zaixian){
this.setText(nicheng+"("+zaixian+")");
}
public accustom getAccustom(){//返回一个friend对象对应的accustom对象
return this.a;
}
}我想创建一个Label类,在此类构造方法中添加鼠标监听器,但当添加弹出菜单时需要调用show()方法,此方法中的一个参数是菜单所出现时所在的组件,但在构造方法中怎能获得此构造方法所构造的对象本身呀?我想的是能不能通过某种方法在构造方法中获得对象本身
不管怎么,还是回到你的程序中。
1) PopupMenu.show(所依附的组件,相对组件的X偏移,相对组件的Y偏移);
你的PopupMenu直接依附你的JLabel就行了。直接用this就行了。X,Y偏移你自己定义。
2)你的PopupMenu事件写得不大好啊。
建议:
if(e.isPopupTrigger())
{
popupMenu1.show(....);
}
以上仅供你参考