我正在尝试为我创建的游戏制作GUI,最初这个游戏是基于控制台的,但我正在努力提高我的技能并添加GUI。
我设法添加了一个按钮并添加了一个事件处理程序,所以当单击该按钮时,代码将会运行,但是,我想知道......
由于我的游戏以前是基于控制台的,我将把主要方法中的所有代码粘贴到GUI部分,然后如何将它用于打印出我用于GUI中的文本框的文本,然后我怎么能让它显示按钮(在这种情况下,攻击,防御或逃跑)。
我也想知道如何让代码等待用户单击其中一个按钮,然后单击该按钮时如何运行代码。
这是我在Main方法中的代码。这很冗长。
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 :(得分:2)
我建议首先阅读更多关于java swing的内容,甚至在youtube上观看并完成教程。你的问题:
问题:我也想知道如何让代码等待用户单击其中一个按钮,然后单击该按钮时如何运行代码。
答案:当您运行GUI代码时,它不会连续运行,它会等待并“监听”用户的输入。当您单击此按钮时,操作“侦听器”或您实现的任何侦听器将运行您要在按钮单击上运行的代码。
问题:由于我的游戏以前是基于控制台的,我将把主要方法中的所有代码粘贴到GUI部分,然后我将如何打印出我用于GUI中文本框的文本那么我怎么能让它显示按钮(在这种情况下,攻击,防御或逃跑)。
答案:GUI是一个巨大的领域,要弄清楚你希望窗口的外观,文本的打印位置和方式,请查看:
http://da2i.univ-lille1.fr/doc/tutorial-java/ui/features/components.html
对于这些按钮,你总是可以使用JButton,你仍然可以使用从键盘输入的1,2,3,4,就如何在这种情况下按下攻击,防御或逃跑。
关于windowsbuilder的另一件事是,在你的情况下,既然你已经把程序放在控制台上并且你想将它转换成GUI,那么如果你 don'它可能更容易,更快,更有价值使用windowbuilder 。手动完成。