gpt4 book ai didi

当我在代码中放入 while 循环时,JavaFX GUI 不会运行,但在删除时会运行

转载 作者:行者123 更新时间:2023-12-02 03:05:59 26 4
gpt4 key购买 nike

所以我目前正在 javaFX 中制作一个简单的回合制游戏,但遇到了问题。我试图在代码中放入一个 while 循环(在倒数第二个代码块中找到),但是当我执行程序时没有任何东西可以运行。我没有收到任何错误,也没有弹出窗口。

如果我去掉 while 循环,那么 GUI 就可以正常运行。

我尝试移动 while 循环,但这并没有改变任何东西。我还尝试转到以前的版本,看看 while 循环是否可以在那里工作,但同样的问题也发生了。

package application;

import javafx.scene.image.*;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;



public class Game extends Application {
Button start, battle, attack, defend;
Scene intro, beginBattle, standby;
MediaPlayer mediaPlayer;

int monsterHealth = 100;
int playerHealth = 100;

public void start(Stage primaryStage) {
primaryStage.setTitle("Dungeon Crawler"); // Gives the title to the window.
primaryStage.setResizable(false); // Prevents the window from being resized.

Rectangle rectDungeon = new Rectangle(75, 15, 650, 125); // creates a Rectangle object that consists of the following parameters: X, Y , WIDTH, HEIGHT.
rectDungeon.setStroke(Color.WHITE); // Colors the borders of the rectangle white.
//
// Media musicFile = new Media("file:Night%20Cave.mp3");
// mediaPlayer = new MediaPlayer(musicFile);
// mediaPlayer.setAutoPlay(true); // Sets the music to start as soon as the program runs. NOTE: TRYING TO GET A MP3 FILE IN AND WHENEVER I USE THIS IT CAUSES THE PROGRAM TO NOT RUN.
// NEED TO FIGURE SOMETHING OUT.

// Sets up the button that will be used
// to start the game when it runs.
start = new Button();
start.setText("Start");

start.setOnAction(e -> primaryStage.setScene(beginBattle)); // Sets the button to activate an event handler for the next scene (beast battle starts).
start.setLayoutX(360); // Sets the x axis location of the button.
start.setLayoutY(720); // Sets the y axis location of the button.

Image dungeonImage = new Image("file:Dungeon.png"); // Sets the image object as an image of a door.
ImageView dungeonView = new ImageView(); // Sets up an object of the ImageView class.
dungeonView.setImage(dungeonImage); // sets the image of the door to be viewed in the GUI

// Dialogue initializing the player to what their task is.
Text dungeonText = new Text(" You traverse the sewers of your city. "
+ "\n You heard of a monster who lurked here and took a quest to destroy it. "
+ "\n Worrying for the safety of the citizens you now search for it."
+ "\n Suddenly, you hear a growl and see a mass bellowing towards you.");

dungeonText.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20)); // Sets the text as Sans serif with no bolding, italics, and a size of 20
dungeonText.setStyle("-fx-fill: white;"); // Sets the text with the color white.
dungeonText.setLayoutX(90); // Sets the x axis location of the text.
dungeonText.setLayoutY(40); // Sets the y axis location of the text.

Pane root = new Pane(); // Sets an object to collect all items for the GUI for the scene in question.
root.getChildren().addAll(dungeonView, rectDungeon, dungeonText, start); // Collects the items needed in the scene.
intro = new Scene(root, 788, 788); // Sets up the GUI items, and the window size.

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Rectangle rectMonsterDungeon = new Rectangle(75, 15, 650, 65); // X, Y , WIDTH, HEIGHT ------------> CHANGE COMMENT
rectMonsterDungeon.setStroke(Color.WHITE);

// Sets up the button that will be used
// to move forward if in a one way hall.
battle = new Button();
battle.setText("Battle!");

battle.setOnAction(e -> primaryStage.setScene(standby)); // Sets the button to activate an event handler for the next scene (a hallway).
battle.setLayoutX(355); // Sets the x axis location of the button.
battle.setLayoutY(720); // Sets the y axis location of the button.

Image monsterDungeonImage = new Image("file:MonsterDungeon.png"); // Sets the image object as an image of a one way hallway.
ImageView monsterDungeonView = new ImageView(); // Sets up an object of the ImageView class.
monsterDungeonView.setImage(monsterDungeonImage); // sets the image of the one way hallway to be viewed in the GUI

// Dialogue warning the player of possible dangers
// ahead when they enter the first room of the crypt.
Text monsterDungeonText = new Text(" The beast has reared it's ugly head in front of you!"
+ "\n Prepare to battle or else perish!");

monsterDungeonText.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20)); // Sets the text as Sans serif with no bolding, italics, and a size of 20
monsterDungeonText.setStyle("-fx-fill: white;"); // Sets the text with the color white.
monsterDungeonText.setLayoutX(90); // Sets the x axis location of the text.
monsterDungeonText.setLayoutY(40); // Sets the y axis location of the text.

Pane root2 = new Pane(); // Sets an object to collect all items for the GUI for the scene in question.
root2.getChildren().addAll(monsterDungeonView, rectMonsterDungeon, monsterDungeonText, battle); // Collects the items needed in the scene.
beginBattle = new Scene(root2, 800, 800); // Sets up the GUI items, and the window size.

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

while(playerHealth > 0 && monsterHealth > 0) {
Rectangle rectMonsterDungeonStandby = new Rectangle(75, 15, 650, 65); // X, Y , WIDTH, HEIGHT ------------> CHANGE COMMENT
rectMonsterDungeonStandby.setStroke(Color.WHITE);

Rectangle rectPlayerBoxSB = new Rectangle(140 , 650, 500, 125);
rectPlayerBoxSB.setStroke(Color.WHITE);

// Sets up the button that will be used
// to move forward if in a one way hall.
attack = new Button();
attack.setText("Attack!");

attack.setOnAction(e -> primaryStage.setScene(intro)); // Sets the button to activate an event handler for the next scene (a hallway).
attack.setLayoutX(300); // Sets the x axis location of the button.
attack.setLayoutY(720); // Sets the y axis location of the button.

// Sets up the button that will be used
// to defend against the monster if the player
// wished to block all or most damage from the
// monster.
defend = new Button();
defend.setText("Defend!");

defend.setOnAction(e -> primaryStage.setScene(standby));
defend.setLayoutX(410);
defend.setLayoutY(720);

Image monsterDungeonImageSB = new Image("file:MonsterDungeonSTANDBY.png"); // Sets the image object as an image of a one way hallway.
ImageView monsterDungeonViewSB = new ImageView(); // Sets up an object of the ImageView class.
monsterDungeonViewSB.setImage(monsterDungeonImageSB); // sets the image of the one way hallway to be viewed in the GUI

// Dialogue warning the player of possible dangers
// ahead when they enter the first room of the crypt.
Text monsterDungeonTextSB = new Text(" Choose from the options below."
+ "\n The Beast's Health: " + monsterHealth);

monsterDungeonTextSB.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20)); // Sets the text as Sans serif with no bolding, italics, and a size of 20
monsterDungeonTextSB.setStyle("-fx-fill: white;"); // Sets the text with the color white.
monsterDungeonTextSB.setLayoutX(90); // Sets the x axis location of the text.
monsterDungeonTextSB.setLayoutY(40); // Sets the y axis location of the text.

Text playerTextSB = new Text("Which will you do?"
+ "\n Health: " + playerHealth);
playerTextSB.setFont(Font.font("Sans serif", FontWeight.NORMAL, FontPosture.ITALIC, 20));
playerTextSB.setStyle("-fx-fill: white");
playerTextSB.setLayoutX(310);
playerTextSB.setLayoutY(685);

Pane root3 = new Pane(); // Sets an object to collect all items for the GUI for the scene in question.
root3.getChildren().addAll(monsterDungeonViewSB, rectMonsterDungeonStandby, rectPlayerBoxSB, monsterDungeonTextSB, playerTextSB, attack, defend); // Collects the items needed in the scene.
standby = new Scene(root3, 800, 800); // Sets up the GUI items, and the window size.
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


primaryStage.setScene(intro); // Sets the first scene in the GUI as the DOOR.png with its accompanying text and button.
primaryStage.show(); // Shows the GUI to the user and provides the different scenes in the program
// depending on the button choices the user makes.
}
public static void main(String[] args) {
launch(args);
}
}

我希望 GUI 能够像 while 不存在一样运行,因为playerHealth 和 monsterHealth 最初都设置为大于 0。

相反,我没有弹出 GUI 窗口,也没有出现错误。

最佳答案

您的期望完全错误。你写:

I'm expecting the GUI to run just as if the while was not there since both playerHealth and monsterHealth are initially set greater than 0.

但是 while 循环条件正好相反:

while(playerHealth > 0 && monsterHealth > 0) {  

对于您的初始条件,此循环将永远运行,从而永远阻止 JavaFX 应用程序线程。

关于当我在代码中放入 while 循环时,JavaFX GUI 不会运行,但在删除时会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57022802/

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