JavaFX按钮上的新场景单击 [英] JavaFX New Scene on Button Click
问题描述
标题可能有点含糊,所以让我对其定义更好一些。我有一段有效的代码(在下面):我正在开发的游戏的简单主菜单。除开始按钮外,其他所有内容均正常运行。
The title may be a bit vague, so allow me to define it a little better. I have a working piece of code (down below): a simple main menu for a game I am working on. Everything works well, except for the Start button.
我想要做的就是单击开始按钮,并在同一阶段(窗口)中出现一个新场景。我不想看到一个新窗口打开。我已经与Java经验丰富的人进行了交谈,他们告诉我为MenuFX和GameFX创建单独的类。如果是这种情况,我需要从MenuFX类中调用GameFX类上的一些启动或启动方法,对吗?这是最好的方法,还是我想将所有与FX相关的代码都放在一个类中?另外,我应该为所有外汇工作保持相同的阶段,不是吗?
What I want to be able to do is click the Start button, and have a new scene appear on the same stage (window). I do not want to see a new window open. I have talked with someone more experienced in Java, and they told me to create separate classes for the MenuFX and the GameFX. If that is the case, I would need to call some start or launch method on the GameFX class from the MenuFX class, correct? Is this the best approach, or would I want to keep all FX-related code in one class? Also, I should keep the same stage for all FX work, no?
这篇文章为您提供了一些启示,但是我对所讨论的某些术语并不精通-例如,我仍然不了解Root的概念。
This post shed some light on things, but I am not well-versed in some of the terms discussed- for instance, I still do not understand the concept of Root.
此外,这篇帖子谈到了类似的应用程序,但是我没有使用FXML或SceneBuilder ...我不知道其中的任何一个都是相关的。
Also, this post talks about a similar application, but I am not using FXML or SceneBuilder... I do not know if any of it is relatable.
MenuFX.java -为简洁起见,我删除了一些工作代码。您会看到我需要帮助的是将开始按钮与一些功能结合在一起,从而创建了一个新的空白场景。
MenuFX.java - I have removed some of the working code, simply for brevity. You can see that all I need help with is tying the Start button to some functionality that makes a new empty scene.
/*
* This is simply working on the title screen.
*/
// Asssume all imports are correct
import java.everythingNeeded
public class MenuFX extends Application {
@Override
public void start (Stage primaryStage) {
// Make the window a set size...
primaryStage.setResizable(false);
// Create menu vbox and set the background image
VBox menuVBox = new VBox(30);
menuVBox.setBackground(new Background(new BackgroundImage(new
Image("image/bambooBG.jpg"), null, null, null, new BackgroundSize(45,
45, true, true, true, true))));
// Create start button
Button startButton = new Button("Start Game");
// TODO Some things...
// Need assistance here
// Create help button
Button helpButton = new Button("Help");
helpButton.setOnAction(e -> THINGS);
// Create music toggle button
ToggleButton musicButton = new ToggleButton("Music On/Off");
musicButton.setOnAction(e -> THINGS);
// Create credits button
Button creditsButton = new Button("Credits");
creditsButton.setOnAction(THINGS);
// Create exit button and set it to close the program when clicked
Button endButton = new Button("Exit Game");
endButton.setOnAction(e -> Platform.exit());
// Add all nodes to the vbox pane and center it all
// Must be in order from top to bottom
menuVBox.getChildren().addAll(startButton, helpButton, musicButton, creditsButton, endButton);
menuVBox.setAlignment(Pos.CENTER);
// New scene, place pane in it
Scene scene = new Scene(menuVBox, 630, 730);
// Place scene in stage
primaryStage.setTitle("-tiles-");
primaryStage.setScene(scene);
primaryStage.show();
}
// Needed to run JavaFX w/o the use of the command line
public static void main(String[] args) {
launch(args);
}
}
重新命名:我想点击开始按钮,将当前打开的窗口更改为一个空场景。
Restating: I want to click the Start button and have the currently open window change to an empty scene.
这里是MenuFX类的全部粘贴框:
http://pastebin.com/n6XbQfhc
Here is a pastebin of the MenuFX class in its entirety: http://pastebin.com/n6XbQfhc
感谢您的帮助,
Bagger
推荐答案
这里的基本思想是
public class GameFX {
private final BorderPane rootPane ; // or any other kind of pane, or Group...
public GameFX() {
rootPane = new BorderPane();
// build UI, register event handlers, etc etc
}
public Pane getRootPane() {
return rootPane ;
}
// other methods you may need to access, etc...
}
现在回到 MenuFX
类中
Button startButton = new Button("Start Game");
startButton.setOnAction(e -> {
GameFX game = new GameFX();
primaryStage.getScene().setRoot(game.getRootPane());
});
这篇关于JavaFX按钮上的新场景单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!