按钮单击时的 JavaFX 新场景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42841930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX New Scene on Button Click
提问by
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.
标题可能有点含糊,所以请允许我更好地定义它。我有一段工作代码(如下):我正在开发的游戏的简单主菜单。除了“开始”按钮外,一切正常。
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?
我希望能够做的是单击“开始”按钮,并在同一舞台(窗口)上出现一个新场景。我不想看到一个新窗口打开。我曾与在 Java 方面更有经验的人交谈过,他们告诉我为 MenuFX 和 GameFX 创建单独的类。如果是这种情况,我需要从 MenuFX 类调用 GameFX 类上的某些启动或启动方法,对吗?这是最好的方法,还是我想将所有与 FX 相关的代码保留在一个类中?另外,我应该为所有 FX 工作保持同一个阶段,不是吗?
Thispost 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.
这篇文章阐明了一些事情,但我对讨论的一些术语并不精通 - 例如,我仍然不理解 Root 的概念。
Also, thispost talks about a similar application, but I am not using FXML or SceneBuilder... I do not know if any of it is relatable.
此外,这篇文章讨论了一个类似的应用程序,但我没有使用 FXML 或 SceneBuilder...我不知道是否有任何相关性。
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.
MenuFX.java- 为了简洁起见,我删除了一些工作代码。您可以看到我需要帮助的只是将“开始”按钮与某些功能相关联,从而创建一个新的空场景。
/*
* 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.
重申:我想单击“开始”按钮并将当前打开的窗口更改为空场景。
Here is a pastebin of the MenuFX class in its entirety: http://pastebin.com/n6XbQfhc
这是 MenuFX 类的完整粘贴箱:http://pastebin.com/n6XbQfhc
Thank you for any help,
感谢您的任何帮助,
Bagger
巴格
采纳答案by James_D
The basic idea here is you would do something like:
这里的基本思想是你会做这样的事情:
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...
}
Now back in the MenuFX
class you would do
现在回到MenuFX
课堂上你会做
Button startButton = new Button("Start Game");
startButton.setOnAction(e -> {
GameFX game = new GameFX();
primaryStage.getScene().setRoot(game.getRootPane());
});