我的Java Swing应用程序不断启动相同的窗口

时间:2017-02-09 18:34:19

标签: java swing

我环顾四周,我不知道为什么它会不断推出一堆窗户。我还没有找到任何其他类似的帖子,但是如果有让我知道的话。我对java也很陌生,在观看视频后我决定尝试使用我自己的代码制作一些简单的游戏,所以如果我的道歉可能会有点麻烦。

代码:

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 = "";
            }
        }
    }

}

另一个文件被命名为Enemies,这里是代码:

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;
    }
}
}

最终文件名为Items and heres,代码为:

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

1 个答案:

答案 0 :(得分:3)

那是你的问题

在这个方法的敌人课程中:

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循环应该存在,因为它们完全违背事件驱动的编码实践。你的第一份工作应该是完全摆脱它们,而是编写代码来对事件作出反应。