2017-02-09 61 views
0

我環顧四周,我不知道爲什麼它不斷地啓動一堆窗口。我還沒有發現任何其他職位與我的確切問題,但如果有讓我知道。我對java也很陌生,在看完一段視頻後,我決定嘗試用一些我自己的代碼做一些簡單的遊戲,所以如果這樣的話我的道歉可能會有點混亂。我的Java Swing應用程序不斷啓動相同的窗口

代碼:

import java.awt.event.*; 
import java.util.Random; 

import javax.swing.*; 

public class Game extends JFrame { 

    // Game Variables 
    Enemies newEnemy = new Enemies(); 
    String enemy; 
    static int enemyHealth; 
    static int enemyAttackDamage; 
    static String imput = ""; 

    public static void main(String[] args) { 

     new Game(); 

    } 

    public Game() { 
     // System Variables 
     boolean running = true; 
     JTextField textField1; 
     JTextArea textArea1; 

     // GUI Variables 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setSize(900, 600); 
     this.setLocationRelativeTo(null); 
     this.setResizable(false); 
     this.setTitle("Dungeon Drivers Alpha 0.2.3"); 

     JPanel thePanel = new JPanel(); 

     textArea1 = new JTextArea(33, 80); 
     textArea1.setText("Window launch was Successful.\n"); 
     textArea1.setEditable(false); 
     textArea1.setLineWrap(true); 
     textArea1.setWrapStyleWord(true); 
     thePanel.add(textArea1); 

     JScrollPane scrollbar1 = new JScrollPane(textArea1, 
       JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
       JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     thePanel.add(scrollbar1); 

     textField1 = new JTextField("", 80); 
     textField1.setText("Enter Text..."); 
     thePanel.add(textField1); 

     this.add(thePanel); 
     this.setVisible(true); 

     // System Objects 
     Random rand = new Random(); 

     // Player Variables 
     int health = 100; 
     int attackDamage = 50; 
     int numHealthPotions = 5; 
     int healthPotionHealAmount = 30; 
     int healthPotionDropChance = 25; // Percentage 

     // GAME 
     textArea1.append("\tWelcome to the Dungeon!"); // THIS IS THE LAST 
                 // MESSAGE SEEN THEN IT 
                 // CONTINUES TO OPEN 
                 // WINDOWS 
     newEnemy.getEnemy(); 
     FIGHT: while (running) { 

      textArea1.append("\n\t-------------------------------------\n"); 
      textArea1.append("\t# A " + enemy + " has appeared! #"); 

      while (enemyHealth > 1) { 
       textArea1.append("\n\tHealth: " + health); 
       textArea1.append("\t" + enemy + "'s Health: " + enemyHealth); 
       textArea1.append("\n\tWhat would you like to do?"); 
       textArea1.append("\t1. Attack!"); 
       textArea1.append("\t2. Drink Health Potion!"); 
       textArea1.append("\t3. Run!\n"); 

       while (running) { 
        textField1.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent e) { 
          imput = textField1.getText(); 
          textField1.setText(""); 
         } 
        }); 
        if (imput.equals("1")) { 
         int damageDealt = rand.nextInt(attackDamage); 
         int damageTaken = rand.nextInt(enemyAttackDamage); 

         enemyHealth -= damageDealt; 
         health -= damageTaken; 

         if (enemyHealth < 1) { 
          textArea1.setText(""); 
         } 
         textArea1.setText(""); 

         textArea1.append(
           "\t> You strike the " + enemy + " for " + damageDealt + " Damage."); 
         textArea1.append("\n\t> You recieve " + damageTaken + " in retaliation!"); 

         if (health < 1) { 
          textArea1.append("You have no Health left so you ran away."); 
          break; 
         } 
         break; 
        } else if (imput.equals("2")) { 
         if (numHealthPotions > 0) { 
          health += healthPotionHealAmount; 
          numHealthPotions--; 
          textArea1.append("\t> You Drink a health potion for " 
            + healthPotionHealAmount + "." + "\n\t> You now have " + health 
            + " Health." + "\n\t> You have " + numHealthPotions 
            + " Health Potions left.\n"); 
          imput = ""; 
         } else { 
          textArea1.append(
            "\t> You have no Health Potions left! Kill enemies to get them."); 
          imput = ""; 
         } 
        } else if (imput.equals("3")) { 
         textArea1.setText(null); 
         textArea1.append("\tYou run from the " + enemy + "!"); 
         imput = ""; 
         continue FIGHT; 
        } 
        imput = ""; 
       } 
       imput = ""; 
      } 
     } 
    } 

} 

另一個文件被命名爲敵人,這裏是代碼:

import java.util.Random; 
import java.util.concurrent.ThreadLocalRandom; 

public class Enemies { 

public void getEnemy() { 

    Random rand = new Random(); 
    String[] list = {"Skeleton", "Zombie"}; 
    Game game = new Game(); 

    String enemies = list[rand.nextInt(list.length)]; 
    if(enemies.equals("Skeleton")) { 
     int maxHealth = 30; 
     int minHealth = 25; 
     Game.enemyHealth = ThreadLocalRandom.current().nextInt(minHealth, maxHealth + 1); 
     int maxAttackDamage = 15; 
     int minAttackDamage = 10; 
     Game.enemyAttackDamage = ThreadLocalRandom.current().nextInt(minAttackDamage, maxAttackDamage + 1); 
     game.enemy = enemies; 
    } 
    else if(enemies.equals("Zombie")) { 
     int maxHealth = 40; 
     int minHealth = 30; 
     Game.enemyHealth = ThreadLocalRandom.current().nextInt(minHealth, maxHealth + 1); 
     int maxAttackDamage = 20; 
     int minAttackDamage = 15; 
     Game.enemyAttackDamage = ThreadLocalRandom.current().nextInt(minAttackDamage, maxAttackDamage + 1); 
     game.enemy = enemies; 
    } 
} 
} 

最終的文件稱爲項目和繼承人代碼:

public class Items { 
static int healthPotion; 
static int ironShard; 
static int sharpWoodenSword; 
static int averageWoodenSword; 
static int dullWoodenSword; 
} 
+1

您的代碼沒有顯示您看到多個窗口啓動的原因,但它確實顯示了其他重大問題。首先,沒有一個while循環應該存在,因爲它們完全違背事件驅動的編碼實踐。你的第一份工作應該是徹底擺脫它們,而不是寫你的代碼來對事件做出反應。 –

+1

如果你仍然需要窗口問題的幫助,那麼你會想努力創建併發佈一個有效的[mcve],一個** small **,實際上最小的,我們可以編譯和運行的程序不變,沒有外部依賴關係(如數據庫或圖像),並直接向我們展示您的問題。 –

+0

感謝您的回覆我將使用事件而不是while循環來重寫代碼。如果我有任何問題可以讓任何人訪問和使用,我還會再次確認。 –

回答

3

那是你的問題

在這個方法中你的Enemy類中:

getEnemy() 

創建一個新的Game對象。

因此,在這個新的Game對象中將創建一個新的Enemy對象,該對象將創建一個新的Game對象,該對象將創建一個新的Enemy對象....永遠。

如果是這樣,解決方案不是這樣做,而是將當前Game實例傳遞給Enemy類的而不是創建新的Game實例。

改變敵人,使它獲得單個有效的遊戲對象,如果它需要它。更改此:

public class Enemies { 

    public void getEnemy() { 

     Random rand = new Random(); 
     String[] list = {"Skeleton", "Zombie"}; 
     Game game = new Game(); 

這樣:

public class Enemies { 
    private Game game; // variable to hold Game reference: 

    public Enemies(Game game) { 
     this.game = game; // set the instance 
    } 

    public void getEnemy() { 

     Random rand = new Random(); 
     String[] list = {"Skeleton", "Zombie"}; 
     // Game game = new Game();  // no longer need this 

然後,當你創建的敵人,通過在this遊戲實例:

Enemies newEnemy = new Enemies(this); 

同樣如評論所說,while循環應該因爲它們完全違背事件驅動的編碼實踐。你的第一份工作應該是徹底擺脫它們,而不是寫你的代碼來對事件做出反應。

+0

非常感謝你很多xD –