gpt4 book ai didi

java - 如何删除 Action 监听器?

转载 作者:行者123 更新时间:2023-12-01 13:40:17 25 4
gpt4 key购买 nike

我是 Java 初学者,我不知道我的代码出了什么问题。我几乎尝试了一切,但没有成功。具体来说,我正在设计一款将垃圾收集到垃圾箱的游戏如果显示垃圾图像,我想在我的 JLabel 上显示它是否可以

        final JLabel smiec=new JLabel();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.weighty = 1.0;
c.anchor = GridBagConstraints.PAGE_END;
c.gridx = 2;
c.gridwidth = 2;
c.gridheight = 3;
smiec.setTransferHandler(new TransferHandler(""));

final Random generator = new Random();
smiec.setText("Kliknij aby rozpocząć!");
smiec.setFont(new Font("Serif", Font.PLAIN, 30));
background.add(smiec,c);

final String text="ok";
final String text2="zle";

smiec.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
ActionListener tak, tak2, tak3, tak4, tak5;
int a=0;
smiec.setText(null);
domowyBudzet.setText(null);
a=16- generator.nextInt(17);
ImageIcon smiecImg = new ImageIcon("odpad"+a+".png");
smiec.setIcon(smiecImg);
background.repaint();
boolean flagA, flagB, flagC, flagD, flagE, flagF;
if(a==0||a==6||a==11){
flagA=true;
}
else{
flagA=false;
}
if(a==1){
flagB=true;
}
else{
flagB=false;
}
if(a==2){
flagC=true;
}
else{
flagC=false;
}
if(a==3||a==4||a==14||a==16){
flagD=true;
}
else{
flagD=false;
}
if(a==5||a==9||a==10||a==13){
flagE=true;
}
else{
flagE=false;
}
if(a==7||a==8||a==12||a==15){
flagF=true;
}
else{
flagF=false;
}
if(flagA==true){
tak = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==buttonPapier){
domowyBudzet.setText(text);
domowyBudzet.repaint();
}
else{
domowyBudzet.setText(text2);
domowyBudzet.repaint();
}

}
};
buttonPapier.removeActionListener(tak);
flagA=false;
}
if(flagB==true){
ActionListener tak1 = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==buttonBiale){
domowyBudzet.setText(text);
domowyBudzet.repaint();
}
else{
domowyBudzet.setText(text2);
domowyBudzet.repaint();
}
}
};
buttonBiale.removeActionListener(tak1);
flagB=false;

}
if(flagC==true){
tak2 = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==buttonKolor){
domowyBudzet.setText(text);
domowyBudzet.repaint();
}
else{
domowyBudzet.setText(text2);
domowyBudzet.repaint();
}
}
};
flagC=false;


}
if(flagD==true){
tak3 = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==buttonReszta){
domowyBudzet.setText(text);
domowyBudzet.repaint();
}
else{
domowyBudzet.setText(text2);
domowyBudzet.repaint();
}
}
};
buttonReszta.removeActionListener(tak3);
flagD=false;
}
if(flagE==true){
tak4 = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==buttonPlastik){
domowyBudzet.setText(text);
domowyBudzet.repaint();
}
else{
domowyBudzet.setText(text2);
domowyBudzet.repaint();
}
}
};
flagE=false;
buttonPlastik.removeActionListener(tak4);

}
if(flagF==true){
tak5 = new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(evt.getSource()==buttonKompost){
domowyBudzet.setText(text);
domowyBudzet.repaint();
}
else{
domowyBudzet.setText(text2);
domowyBudzet.repaint();
}
}
};
buttonKompost.removeActionListener(tak5);
flagF=false;

}


}
});

最佳答案

您没有保留对监听器的引用。为了删除监听器,您应该保存对它的引用,然后可以将该引用传递给删除监听器方法。

例如你正在做的:

smiec.addMouseListener(new MouseAdapter(){ ... });

但是如果您执行了类似的操作,您稍后就可以删除鼠标监听器。

MouseListener mouseListener = new MouseAdapter() {...};
smiec.addMouseListener(mouseListener);
...
smiec.removeMouseListener(mouseListener);

您可以查看JLabel的Java文档,特别是removeMouseListener方法here .

关于java - 如何删除 Action 监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20870191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com