给标签加的鼠标监听器,并把标签的引用传入监听器,为什么传入的标签引用会跟原本的引用不相等呢?

时间:2022-10-24 22:38:36
JLabel ab1 = new JLabel(image1);
JLabel ab2 = new JLabel(image3);
JLabel ab3 = new JLabel("聊天器",JLabel.LEFT);
ab1.setBounds(737,5,23,23);
ab2.setBounds(712, 5,23,23);
ab3.setBounds(10,5,600,23);
ab3.setIcon(image5);
ab3.setForeground(Color.WHITE);
ab3.setFont(new Font("宋体",Font.BOLD,15));
ab1.addMouseListener(new KeyMouseMonitor(ab1,image1,image2));
ab2.addMouseListener(new KeyMouseMonitor(ab2,image3,image4));

add.(ab1);
add.(ab2);
add.(ab3);


private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}
 
//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}

//弹起时
public void mouseReleased(MouseEvent e) {

}
}

private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}
 
//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}

//弹起时
public void mouseReleased(MouseEvent e) {

}
}


private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}
 
//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}

//弹起时
public void mouseReleased(MouseEvent e) {

}
}

打印的信息
false
Esc:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
bel:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc_l.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]


3 个解决方案

#1


JLabel ab1 = new JLabel(image1);
JLabel ab2 = new JLabel(image3);
JLabel ab3 = new JLabel("聊天器",JLabel.LEFT);
ab1.setBounds(737,5,23,23);
ab2.setBounds(712, 5,23,23);
ab3.setBounds(10,5,600,23);
ab3.setIcon(image5);
ab3.setForeground(Color.WHITE);
ab3.setFont(new Font("宋体",Font.BOLD,15));
ab1.addMouseListener(new KeyMouseMonitor(ab1,image1,image2));
ab2.addMouseListener(new KeyMouseMonitor(ab2,image3,image4));

add.(ab1);
add.(ab2);
add.(ab3);


private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}

打印的信息
false
Esc:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
bel:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc_l.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]

#2


JLabel ab1 = new JLabel(image1);
JLabel ab2 = new JLabel(image3);
JLabel ab3 = new JLabel("聊天器",JLabel.LEFT);
ab1.setBounds(737,5,23,23);
ab2.setBounds(712, 5,23,23);
ab3.setBounds(10,5,600,23);
ab3.setIcon(image5);
ab3.setForeground(Color.WHITE);
ab3.setFont(new Font("宋体",Font.BOLD,15));
ab1.addMouseListener(new KeyMouseMonitor(ab1,image1,image2));
ab2.addMouseListener(new KeyMouseMonitor(ab2,image3,image4));

add.(ab1);
add.(ab2);
add.(ab3);


private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}
 
//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}

//弹起时
public void mouseReleased(MouseEvent e) {

}
}

打印的信息
false
Esc:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
bel:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc_l.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]

老复制错


#3


//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}
Esc是什么鬼,你确定Esc和bel是同一个对象的引用?

#1


JLabel ab1 = new JLabel(image1);
JLabel ab2 = new JLabel(image3);
JLabel ab3 = new JLabel("聊天器",JLabel.LEFT);
ab1.setBounds(737,5,23,23);
ab2.setBounds(712, 5,23,23);
ab3.setBounds(10,5,600,23);
ab3.setIcon(image5);
ab3.setForeground(Color.WHITE);
ab3.setFont(new Font("宋体",Font.BOLD,15));
ab1.addMouseListener(new KeyMouseMonitor(ab1,image1,image2));
ab2.addMouseListener(new KeyMouseMonitor(ab2,image3,image4));

add.(ab1);
add.(ab2);
add.(ab3);


private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}

打印的信息
false
Esc:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
bel:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc_l.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]

#2


JLabel ab1 = new JLabel(image1);
JLabel ab2 = new JLabel(image3);
JLabel ab3 = new JLabel("聊天器",JLabel.LEFT);
ab1.setBounds(737,5,23,23);
ab2.setBounds(712, 5,23,23);
ab3.setBounds(10,5,600,23);
ab3.setIcon(image5);
ab3.setForeground(Color.WHITE);
ab3.setFont(new Font("宋体",Font.BOLD,15));
ab1.addMouseListener(new KeyMouseMonitor(ab1,image1,image2));
ab2.addMouseListener(new KeyMouseMonitor(ab2,image3,image4));

add.(ab1);
add.(ab2);
add.(ab3);


private class KeyMouseMonitor extends MouseAdapter {
JLabel bel = null;
ImageIcon image1 = null;   //离开时图标
ImageIcon image2 = null; //进入时 图标
ImageIcon image3 = null; //按下时图标

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
}

public KeyMouseMonitor(JLabel bel,ImageIcon image1,ImageIcon image2,ImageIcon image3) {
this.bel = bel;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}

//进入时
public void mouseEntered(MouseEvent e) {
//System.out.println(F2 == bel);
bel.setIcon(image2);
repaint();
}

//离开时
public  void mouseExited(MouseEvent e) {
bel.setIcon(image1);
repaint();
}
 
//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}

//弹起时
public void mouseReleased(MouseEvent e) {

}
}

打印的信息
false
Esc:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]
bel:  javax.swing.JLabel[,10,40,45x38,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=KyeImage/Esc_l.png,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=,verticalAlignment=CENTER,verticalTextPosition=CENTER]

老复制错


#3


//按下时
public void mousePressed(MouseEvent e) {
System.out.println(Esc == bel);
System.out.println("Esc:  " + Esc);
System.out.println("bel:  " + bel);
if(bel == ab1) {
//退出系统
akm.exit();
repaint();
} else if(bel == ab2) {
//最小化窗体
akm.iconified();
repaint();
} else {

}
}
Esc是什么鬼,你确定Esc和bel是同一个对象的引用?