Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是什么导致我的代码冻结?我没有主意了_Java - Fatal编程技术网

Java 是什么导致我的代码冻结?我没有主意了

Java 是什么导致我的代码冻结?我没有主意了,java,Java,我正在用Java开发一个游戏引擎,我还在努力学习如何正确地制作GUI。我的代码的工作方式(或者,至少,我希望它如何工作)是我有一个名为Main的类和一个名为BattleFrame的类。BattleFrame有一种方法,可以将系统输出重置为其中的文本字段。BattleFrame包含GUI,而Main控制按下按钮时发生的事情以及游戏中的事件顺序。在main中,有一种方法可以根据先前保存的数据(存储在文本文件中)加载游戏世界。当main启动时,它调用BattleFrame,它以只有一个按钮的形式加载,

我正在用Java开发一个游戏引擎,我还在努力学习如何正确地制作GUI。我的代码的工作方式(或者,至少,我希望它如何工作)是我有一个名为Main的类和一个名为BattleFrame的类。BattleFrame有一种方法,可以将系统输出重置为其中的文本字段。BattleFrame包含GUI,而Main控制按下按钮时发生的事情以及游戏中的事件顺序。在main中,有一种方法可以根据先前保存的数据(存储在文本文件中)加载游戏世界。当main启动时,它调用BattleFrame,它以只有一个按钮的形式加载,continue(稍后我将添加新游戏)。当用户按continue时,actionPerformed将转到Main,然后它将打印“Hello”。然后它对按下的按钮是continue这一事实做出反应,并加载世界。这就是问题发生的地方,因为当我删除它时,“Hello”会被打印到文本字段中,但当该方法存在时,gui会冻结

通过调试,我发现所有冻结的方法都是Load类中类Player的初始化。在我调用player=newplayer(name)(该名称是先前从文本文件读取的,并且是正确的)之后,调试器本身停止。在这一点上,我不知道为什么会发生这种情况。加载播放器之前是有效的,直到我开始处理主类,在我开始处理gui之前,所以我怀疑问题一定在哪里,但我真的不知道。提前为我的丑陋代码感到抱歉,我知道我在写一些可以理解的东西方面还有很多需要学习的地方

public class Main {

    static World world;
    static Player player;
    static Clerks c;

    static BattleFrame bf;
    static InputCheckers i;
    static Species species;
    static Load l;
    static Specie bear;
    static Shop sh;

    static Save s;
    static Battle b;

    public static void main(String[] args) throws IOException, UnsupportedLookAndFeelException, InterruptedException 
    {

        c = new Clerks();

        species = new Species();

        bear = species.getBasicBear();
        sh = new Shop(c.getFirstClerk());

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    bf = new BattleFrame();
                    JTextAreaOutputStream out = new JTextAreaOutputStream(bf.textOut);
                    System.setOut(new PrintStream(out));
                    bf.setVisible(true);
                } 
                catch (InterruptedException ex) 
                {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        });
   }

    public void perform(JButton e) throws IOException 
       {
           System.out.println("Po");
           String button = e.getName();
           if(button.equals("continue"))
           {

               newGameOrContinue("c");
           }
           else if(button.equals("new game"))
           {
               System.out.println("    ");
               newGameOrContinue("n");
           }
       }

    public static void newGameOrContinue(String nOrC) throws IOException 
    {

        System.out.println("Hello");
        if (nOrC.equals("c")) {
            Load l = new Load();

            try{
            world = l.loadWorld();
            }
            catch(IOException a)
            {
                System.out.println("WTF");
            }
   }
我现在想要的是代码不要冻结,应该打印出来的文本要打印出来。再一次,为这混乱的代码道歉,相信我,我已经尽力了。如果代码在这里,我试着只放必要的部分,如果格式错误,我会修改它,这是我第一次在这里提问。提前谢谢

函数
nameSetter()
Player
应用构造函数之前被调用,而静态函数
nameSetter()
需要用户输入,这是您在
BattleWindow
中禁用的,因此程序将等待永远不会发生的用户输入。只需将
Player
构造函数更改为以下内容:

public class Player extends GameObject {

  protected static Equipment equipped = new Equipment();
  protected static Species species = new Species();
  protected Specie human = species.getBasicHuman();
  protected int maxHealth;
  protected int speed;
  protected int str;
  protected Double luck;
  protected int def;
  protected int currentHealth;
  protected int wealth;

  Inventory inv = new Inventory();

  public Player() {
    super(Player.nameSetter(), "Player Controlled", System.getProperty("user.dir") + "src\\main\\java\\cz\\com\\GameFiles\\LevyBuild\\Sprites\\Objects\\Items\\Misc\\BearClaw.png");
    properties();
  }
}

请注意,无论何时创建
Player
的新实例,它都会再次崩溃。您需要再次启用用户输入。

如果您的父类被称为
对象
,那么这是非常糟糕的做法,这取决于您在那里使用参数做什么,否则-如果没有任何参数-代码应该失败,因为只有默认构造函数没有参数-
对象(字符串a,字符串b)
在给定代码中不存在代码是否编译?如果是这样,那么在
属性
方法中阅读的
人类
是什么?请了解java命名约定。所有类名都应该大写。是的:不要在自己的类中重复使用对象等名称。长话短说:请确保遵循。使用链接改进您的问题(不要通过评论添加更多信息)。否则我们将无法回答您的问题并帮助您。@xxxvodnikxxx不,我添加了点以显示该类有更多的代码,但我认为这与我的问题无关。我的代码已经足够长了。你说得对,我将把类对象重命名为类似“Generic”的东西。它的要点是,我不必为每个新游戏对象定义名称和精灵位置(不是代码,我真的应该选择一个更好的名称)。那么您认为简单地更改(并折射)类名就可以解决问题吗?谢谢你的回复!
public class Load 
{
    Attacks attacks = new Attacks(0,0);
    Items items = new Items();
    Bodyparts bodyparts = new Bodyparts(0,0);
    Inventory inv = new Inventory();

    public World loadWorld() throws IOException
    {

        Player player = loadPlayer();
        World world = new World(player);
        return world;
    }

    public Player loadPlayer() throws IOException
    {
        String name;
        Player player;
        name = loadName();



        player = new Player(name);//This is the place where the debugger stops
        .
        .
        .
        .
         return player;
    }

    public String loadName() throws FileNotFoundException, IOException
    {
        String location = (System.getProperty("user.dir") + "\\src\\main\\java\\cz\\com\\GameFiles\\Save\\Name.txt");
        File file = new File(location); 

        BufferedReader br = new BufferedReader(new FileReader(file)); 

        String name = br.readLine(); 
        br.close();
        return name;
    }
}
public class Player extends Object 
{
    protected static Species species = new Species();
    protected Specie human = species.getBasicHuman();
    protected int maxHealth;
    protected int speed;
    protected int str;
    protected Double luck;
    protected int def;
    protected int currentHealth;
    protected int wealth;
    final static protected String name = nameSetter();

    public Player() {
       super(name, "Player Controlled", System.getProperty("user.dir") + "src\\main\\java\\cz\\com\\GameFiles\\LevyBuild\\Sprites\\Objects\\Items\\Misc\\BearClaw.png");
      properties();

    }

    public Player(String givenName) 
    {
         super(givenName, "Player Controlled", System.getProperty("user.dir")+ "src\\main\\java\\cz\\com\\GameFiles\\LevyBuild\\Sprites\\Objects\\Items\\Misc\\BearClaw.png");
         properties();
    }

   public static String nameSetter() 
   {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter player name: ");
        String name = sc.nextLine();
        return name;
   }

   private void properties() 
   {

        this.maxHealth = human.getHP();
        this.speed = human.getSpeed()*2; 
        this.str = human.getStr()*2;   
        this.luck = human.getLuck();      
        this.def = human.getDef();   
        this.currentHealth = maxHealth;       
        this.wealth = 0;
   } 
}


public abstract class Object 
{
    protected String name; 
    protected String status;
    protected String spriteLocation;

public Object(String giveName, String giveStatus, String giveSpriteLocation)
    {
        name = giveName;
        status = giveStatus;
        spriteLocation = giveSpriteLocation;
    }
} 

public class Species {

        ArrayList<Specie> allSpecies = new ArrayList<Specie> ();

    Randomness rand = new Randomness();

    public Specie getBasicHuman()
    {
        String name = "Basic Human";
        int health  = 50;
        int speed = 5;
        int str = 5;
        Double luck = 0.06;
        int def = 1;
        Attacks allAttacks = new Attacks(health,def);
        Attack[] attacks = new Attack[]{allAttacks.getDoNothing(), allAttacks.getOnePunch()};
        Items items = new Items(health, def);
        Item[] loot = new Item[]{items.getApple()};

        for(Attack attack : attacks)
        {
            attack.setLevel(10);
        }

        Specie basicHuman = new Specie("Basic Human", str, speed, luck, def, health, loot, attacks,"random");
        allSpecies.add(basicHuman);
        return basicHuman;
    }
}

public class Specie extends Object{

    protected int str;//Strength
    protected int speed;
    protected Double luck;
    protected int def;//Defense
    protected int hp;//Health Points
    protected Item[] loot; //What loot will be possible
    protected Attack[] attacks;
    protected int givenExp;
    protected int levelAttacks = 1;
    protected String focus;

    public Specie(String givenName, int giveStr, int giveSpeed, Double giveLuck, int giveDef, int giveHP, Item[] giveLoot, Attack[] giveAttacks, String giveFocus) {
        super(givenName);
        properties(giveStr,giveSpeed,giveLuck,giveDef,giveHP,giveLoot,giveAttacks, giveFocus);

    }
}
public class Player extends GameObject {

  protected static Equipment equipped = new Equipment();
  protected static Species species = new Species();
  protected Specie human = species.getBasicHuman();
  protected int maxHealth;
  protected int speed;
  protected int str;
  protected Double luck;
  protected int def;
  protected int currentHealth;
  protected int wealth;

  Inventory inv = new Inventory();

  public Player() {
    super(Player.nameSetter(), "Player Controlled", System.getProperty("user.dir") + "src\\main\\java\\cz\\com\\GameFiles\\LevyBuild\\Sprites\\Objects\\Items\\Misc\\BearClaw.png");
    properties();
  }
}