如何删除动作监听器?

时间:2014-01-01 15:36:14

标签: java swing jbutton

我是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;

                }


              }
        });

1 个答案:

答案 0 :(得分:1)

您没有提及您的听众。为了删除一个监听器,你应该保持对它的引用,然后你可以将引用传递给remove listener方法。

例如你正在做:

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

但是如果你做了类似这样的事情,你以后可以删除鼠标监听器。

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

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