簡體   English   中英

如何刪除內部類ActionListener?

[英]How to remove an inner class ActionListener?

我正在制作一個基於文本的游戲,但是我遇到了一個很大的問題。 問題是,當我將一個新的ActionListener分配給一個已經為其分配了ActionListener的按鈕時,它將同時執行兩個操作。 這是我的代碼:

       while(shouldLoop) {
       if(Player.loc == 1) {
       left.setText("Do nothing");
       left.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
           nar1.setText("You are still in a dungeon."); //Here's my first assignment
       }
       });
       right.setText("Pick the lock");
       right.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               Player.loc = 2;
           }
       });
       } if(Player.loc == 2) {
               nar1.setText("You are now outside");
               nar2.setText("the door. What now?");
               forward.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                       nar1.setText("You hear a guard.");
                       nar2.setText("What do you do now?");
                       Player.loc = 3;
       }
               });
               left.addActionListener(new ActionListener() {   //Here's another
                   @Override                                   //assignment to
                   public void actionPerformed(ActionEvent e) {//the same button
                       nar1.setText("You hear a guard.");      //so when I press
                       nar2.setText("What do you do now?");    //it here, it
                       Player.loc = 3;                         //performs the
                   }                                           //original assignment
               });
               right.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                       nar1.setText("You hear a guard.");
                       nar2.setText("What do you do now?");
                       Player.loc = 3;
                   }
               });
               right.setText(rgt);
               forward.setText(fwd);
               back.setText(bck);
               left.setText(lft);
               forward.setVisible(true);
       } if(Player.loc == 3) {
           forward.setVisible(false);
           right.setText("Sneak around him!");
           left.setText("Fight him!");
       }

感謝您的幫助,

乒乓球

您為什么不只將addActionListener代碼提升到while循環之外?

匿名內部類偵聽器在快速而骯臟的示例代碼中看起來不錯,但是在實踐中,它們是一個可怕的想法。 您正在學習原因之一。 其他原因是它們不能輕易通過Dependency Injection類型的事物進行子類化或修改,不能輕易共享(例如按鈕,工具欄圖標和菜單執行相同的事情),不能輕易啟用/禁用,(例如,應禁用“另存為”,因為沒有打開任何內容)等。

對於此用例,將它們分成一些實際的偵聽器系列,可能以類,數組或某些Enum的形式組織,以便可以將它們交換進出。

通過在程序本身中對程序的邏輯進行硬編碼,正在以一種不好的方式將數據與代碼混合,這是當前和將來問題的主要根源。 這是行不通的,至少沒有很多麻煩就行了。

我建議您嘗試將數據與代碼分離,使程序更具MVC風格,即Model-View-Controller(或其眾多變體之一)。 例如,程序的邏輯由模型掌握,其中包括所探索土地的非可視化地圖,玩家在該地圖中的位置,他的同伴以及您所收集物品的清單。 因此,您可能會有幾種非GUI類,例如Player,Map,Item(可能是接口),Room等。Map本身,可能的項目以及那里的位置將在數據文件中指定,可能是一個簡單程序的文本文件,或者一個更復雜程序的數據庫,您將需要類來讀取和解析此文件。

程序的視圖就是您的GUI,它應該盡可能的簡單。 在這里,您將有自己的按鈕,文本顯示,以及花哨的游戲圖形顯示。 這里沒有程序邏輯。 沒有。

控件將是您添加到JButton中的動作偵聽器,以及其他必須處理用戶輸入的代碼。 但是這段代碼可能相對簡單,它的主要任務是將用戶交互傳遞給模型。 因此,例如,如果按下左按鈕,則可能只是類似

model.leftButtonPress();

然后,模型將決定用左鍵按下(如果有)該怎么做。 例如,模型會知道您在游戲中的位置,左邊是否有一扇門,或者它是否是牆,並且模型根據模型狀態執行必要的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM