2015-12-23 66 views
2

我想为我创建的游戏创建一个GUI,最初这个游戏是基于控制台的,但是我试图提高我的技能并添加一个GUI。使用Eclipse WindowBuilder创建一个GUI

我已经成功地添加一个按钮,并添加一个事件处理程序,以便在单击按钮时,代码将运行,但是,我不知道......

由于基于控制台的,在我的游戏以前我会将我的主要方法中的所有代码粘贴到GUI部分,然后如何让它将我用于文本框的文本打印出来,然后如何让它显示按钮(在这种情况下,攻击,防守或逃跑)。

我也想知道如何让代码等待用户点击其中一个按钮,然后当按钮被点击时我该如何运行代码。

这是我在我的主要方法中的代码。这很长。

import java.util.Scanner; 
import java.util.Random; 
import java.util.concurrent.TimeUnit; 

public class Start { 
    public static void main(String[] args) throws InterruptedException { 
     Scanner reader = new Scanner(System.in); 
     Random Randint = new Random(); 

     int level = playerstats.level; 
     int experiance = playerstats.experiance; 
     int defence = playerstats.defence; 
     int levelcap = playerstats.levelcap; 
     int attackBase = playerstats.attackbase; 
     int agility = playerstats.agility; 
     String location = "Hell"; 
     int health = 100; 

     int defended = 0; 
     Boolean alive = true; 
     if (alive = true) { 
      while (alive) { 
       if (experiance >= levelcap) { 
        level++; 
        experiance = experiance * 2; 
        attackBase = attackBase + 5; 
       } 
       boolean enemyalive = false; 
       byte Choice; 
       int fleeattempt; 
       System.out.println("You're still alive. Surprising."); 
       System.out.println("Your current level is " + level + " you require " + (levelcap - experiance) 
         + " more experiance to level up"); 
       System.out.println("Press enter to continue walking"); 
       reader.nextLine(); 
       System.out.println("You've encountered an enemy!"); 
       int enemyencountered = Randint.nextInt(1); 
       if (enemyencountered == 0) { 
        enemyencountered = enemyencountered + 1; 
       } else { 
       } 
       enemyalive = true; 
       while (enemyalive) { 
        switch (enemyencountered) { 
        case 1: 
         int enemyhealth = goblin.health; 
         int enemydefence = goblin.defence; 
         String enemyname = goblin.name; 
         int enemyagility = goblin.agility; 
         while (enemyalive) { 
          if (defended == 1) { 
           defence = defence - 5; 
           defended = 0; 
          } 
          TimeUnit.SECONDS.sleep(1); 
          System.out.println("You have encountered a " + enemyname); 
          TimeUnit.SECONDS.sleep(1); 
          System.out.println("You have " + health + " remaining HP"); 
          TimeUnit.SECONDS.sleep(1); 
          System.out.println("It has " + enemyhealth + " Remaining HP"); 
          System.out.println("1. attack"); 
          System.out.println("2. Inspect Enemy"); 
          System.out.println("3. Defend"); 
          System.out.println("4. Flee"); 
          System.out.println("5. Quit."); 
          Choice = reader.nextByte(); 
          switch (Choice) { 
           case 1: 
            int Hit = attackBase - enemydefence; 
            int attack = Randint.nextInt(Hit); 
            System.out.println("You hit the enemy for " + attack); 
            enemyhealth = enemyhealth - attack; 
            if (enemyhealth <= 0) { 
             System.out.println(enemyname + " has been killed! Welldone!"); 
             enemyalive = false; 
            } 
            break; 
           case 2: 
            System.out.println(enemyname + " has " + enemydefence + " defence"); 
            break; 
           case 3: 
            System.out.println("You prepare yourself for the enemies attack!"); 
            defence = defence + 5; 
            defended = 1; 
            break; 
           case 4: 
            System.out.println("You're trying to flee. Coward!"); 
            TimeUnit.SECONDS.sleep(1); 
            fleeattempt = Randint.nextInt(agility); 
            if (fleeattempt > enemyagility) { 
             System.out.println("You successfuly Escape!"); 
             enemyalive = false; 
            } else { 
             System.out.println("You accidently fall over while trying to escape."); 
             TimeUnit.SECONDS.sleep(1); 
             health = health - 5; 
             System.out.println("You lose 5 HP."); 
            } 
            break; 
           case 5: 
            System.out.println("Quitting, eh?... Well, bye!"); 
            alive = false; 
            enemyalive = false; 
            break; 
          } 

          if (enemyalive) { 
           int enemyattack; 
           enemyattack = goblin.baseattack; 
           int enemydamage = Randint.nextInt(enemyattack); 
           health = health - enemydamage; 
           System.out.println(enemyname + " Has hit you for " + enemydamage + " HP!"); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

根据Java命名约定,变量名应该以小写字母开头。以这种方式阅读代码是非常困难的。请修复。 – user1803551

+0

我已将所有的变量小写。 –

+0

“playerstats”和“goblin”的定义在哪里?这不是[SSCCE](http://sscce.org/)。 –

回答

2

我建议先阅读关于java swing的更多内容,甚至可以在youtube上观看并完成教程。你的问题:

问题:我也想知道如何让代码等待用户点击其中一个按钮,然后当按钮被点击时我该如何运行代码。

回答:那么当你运行GUI代码时,它不会持续运行,它会等待并“监听”用户的输入。当你点击这个按钮时,动作“监听者”或者你已经实现的监听者将运行你想要在按钮单击上运行的代码。

问题:由于我的游戏之前是基于控制台的,我将我的主要方法中的所有代码粘贴到GUI部件的哪个位置,然后如何让它将我用于文本框的文本打印出图形用户界面,那么我将如何显示按钮(在这种情况下,攻击,保卫或逃离)。

答:GUI是一个巨大的领域,并找出如何你想你的窗口看,在哪里以及如何文本将被打印检查了这一点:

http://da2i.univ-lille1.fr/doc/tutorial-java/ui/features/components.html

这些按钮可以随时使用JButton,你仍然可以使用从键​​盘输入的1,2,3,4,至于在这种情况下如何按下Attack,Defend或Flee。

关于windowsbuilder的另外一件事情就是您的情况,因为您已经在控制台上创建了程序并且想要将它转换为GUI,所以如果您不需要,它可能会更容易,更快速,更有学问值得使用windowbuilder。手动做。

+0

看来这个答案已经回答了我的问题。谢谢! –

相关问题