[英]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.
除“开始”按钮外,其他所有内容均正常运行。
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.
我已经与Java方面的经验丰富的人进行了交谈,他们告诉我为MenuFX和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?
如果是这种情况,我需要从MenuFX类中调用GameFX类的一些启动或启动方法,对吗? Is this the best approach, or would I want to keep all FX-related code in one class?
这是最好的方法,还是我想将所有与FX相关的代码都放在一个类中? Also, I should keep the same stage for all FX work, no?
另外,我应该在所有外汇工作中保持同一阶段,不是吗?
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. 这篇文章阐明了一些事情,但是我对所讨论的某些术语并不精通-例如,我仍然不了解Root的概念。
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. 另外, 本文讨论了类似的应用程序,但是我没有使用FXML或SceneBuilder ...我不知道其中是否有任何关系。
MenuFX.java - I have removed some of the working code, simply for brevity. MenuFX.java-为简洁起见,我删除了一些工作代码。 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. 重新开始:我想单击开始按钮,并将当前打开的窗口更改为空场景。
Here is a pastebin of the MenuFX class in its entirety: http://pastebin.com/n6XbQfhc 以下是MenuFX类的全部pastebin: http ://pastebin.com/n6XbQfhc
Thank you for any help, 感谢您的任何帮助,
Bagger 巴格尔
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());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.