标题可能有点模糊,所以请允许我更好地定义它。我有一段工作代码(如下):我正在开发的游戏的简单主菜单。除了“开始”按钮之外,一切都运行良好。
我想要做的是单击“开始”按钮,然后在同一舞台(窗口)上出现一个新场景。我不想看到新窗口打开。我与 Java 方面更有经验的人交谈过,他们告诉我为 MenuFX 和 GameFX 创建单独的类。如果是这种情况,我需要从 MenuFX 类中调用 GameFX 类上的某些启动或启动方法,对吗?这是最好的方法,还是我想将所有与 FX 相关的代码保留在一个类中?另外,我应该为所有 FX 工作保留同一舞台,不是吗?
This帖子阐明了一些事情,但我不太熟悉所讨论的一些术语——例如,我仍然不理解根的概念。
Also, this帖子讨论了类似的应用程序,但我没有使用 FXML 或 SceneBuilder...我不知道其中任何一个是否相关。
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);
}
}
重申:我想单击“开始”按钮并将当前打开的窗口更改为空场景。
下面是 MenuFX 类的完整粘贴:http://pastebin.com/n6XbQfhc
感谢您的任何帮助,
Bagger