使用 Eclipse WindowBuilder 创建 GUI
Creating a GUI with Eclipse WindowBuilder
我正在尝试为我创建的游戏制作 GUI,最初这款游戏是基于控制台的,但我正在努力提高我的技能并添加 GUI。
我已经设法添加了一个按钮并添加了一个事件处理程序,因此当单击该按钮时,代码将 运行,但是,我想知道...
由于我的游戏之前是基于控制台的,我应该将 Main Method 中的所有代码粘贴到 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!");
}
}
}
}
}
}
}
}
我建议首先阅读更多关于 java 挥杆的内容,甚至在 youtube 上观看并完成教程。您的问题:
问题:我还想知道如何让代码等待用户单击其中一个按钮,然后如何在单击该按钮时让代码 运行。
回答:当您 运行 GUI 代码时,它不会 运行 连续,它会等待 "listens" 用户的输入。当您单击此按钮时,操作 "listener" 或您实施的任何侦听器将 运行 您想要在按钮单击时 运行 的代码。
问题:由于我的游戏之前是基于控制台的,所以我应该将 Main Method 中的所有代码粘贴到 GUI 部分的什么位置,然后如何让它打印出我在 Textbox 中使用的文本GUI,那么我如何让它显示按钮(在本例中为攻击、防御或逃跑)。
答案:GUI 是一个很大的领域,要弄清楚您希望 window 的外观、打印文本的位置和方式,请查看:
http://da2i.univ-lille1.fr/doc/tutorial-java/ui/features/components.html
对于这些按钮,您始终可以使用 JButton,并且您仍然可以使用从键盘输入的 1、2、3、4,至于在这种情况下您将如何按下,Attack、Defend 或 Flee。
关于 windowsbuilder 的另一件事是,在你的情况下,因为你已经在控制台上制作了程序并且你想将它转换成 GUI,它可能更容易,更快并且更值得学习如果您 不使用 windowbuilder。手动完成。
我正在尝试为我创建的游戏制作 GUI,最初这款游戏是基于控制台的,但我正在努力提高我的技能并添加 GUI。
我已经设法添加了一个按钮并添加了一个事件处理程序,因此当单击该按钮时,代码将 运行,但是,我想知道...
由于我的游戏之前是基于控制台的,我应该将 Main Method 中的所有代码粘贴到 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!");
}
}
}
}
}
}
}
}
我建议首先阅读更多关于 java 挥杆的内容,甚至在 youtube 上观看并完成教程。您的问题:
问题:我还想知道如何让代码等待用户单击其中一个按钮,然后如何在单击该按钮时让代码 运行。
回答:当您 运行 GUI 代码时,它不会 运行 连续,它会等待 "listens" 用户的输入。当您单击此按钮时,操作 "listener" 或您实施的任何侦听器将 运行 您想要在按钮单击时 运行 的代码。
问题:由于我的游戏之前是基于控制台的,所以我应该将 Main Method 中的所有代码粘贴到 GUI 部分的什么位置,然后如何让它打印出我在 Textbox 中使用的文本GUI,那么我如何让它显示按钮(在本例中为攻击、防御或逃跑)。
答案:GUI 是一个很大的领域,要弄清楚您希望 window 的外观、打印文本的位置和方式,请查看:
http://da2i.univ-lille1.fr/doc/tutorial-java/ui/features/components.html
对于这些按钮,您始终可以使用 JButton,并且您仍然可以使用从键盘输入的 1、2、3、4,至于在这种情况下您将如何按下,Attack、Defend 或 Flee。
关于 windowsbuilder 的另一件事是,在你的情况下,因为你已经在控制台上制作了程序并且你想将它转换成 GUI,它可能更容易,更快并且更值得学习如果您 不使用 windowbuilder。手动完成。