2017-02-09 24 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 –

相关问题