gpt4 book ai didi

java - 使用 Eclipse WindowBuilder 创建 GUI

转载 作者:行者123 更新时间:2023-11-30 08:42:51 24 4
gpt4 key购买 nike

<分区>

我正在尝试为我创建的游戏制作一个 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!");
}
}
}
}
}
}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com