单击按钮时的 JavaFX 新场景

标签 java user-interface button javafx scene

标题可能有点模糊,所以请允许我更好地定义它。我有一段代码(在下方):我正在开发的游戏的简单主菜单。一切正常,除了“开始”按钮。

我希望能够做的是单击“开始”按钮,并在同一舞台(窗口)上出现一个新场景。我不想看到一个新窗口打开。我曾与一位在 Java 方面更有经验的人交谈过,他们告诉我为 MenuFX 和 GameFX 创建单独的类。如果是这种情况,我需要从 MenuFX 类调用 GameFX 类的一些启动或启动方法,对吗?这是最好的方法,还是我想将所有与 FX 相关的代码放在一个类中?另外,我应该为所有 FX 工作保持相同的阶段,不是吗?

This帖子阐明了一些事情,但我对所讨论的某些术语并不精通 - 例如,我仍然不理解 Root 的概念。

此外,this帖子讨论了类似的应用程序,但我没有使用 FXML 或 SceneBuilder ...我不知道它们是否相关。

MenuFX.java - 为了简洁起见,我删除了一些工作代码。您可以看到,我需要帮助的只是将“开始”按钮绑定(bind)到一些创建新空场景的功能。

/* 
 * 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 类的一个完整的 pastebin: http://pastebin.com/n6XbQfhc

感谢您的帮助,

装袋机

最佳答案

这里的基本思想是你会做这样的事情:

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 新场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42841930/

相关文章:

java - Apache Spark 可以使用 TCP 监听器作为输入吗?

java - Android:以编程方式将 ListView 设置为对话框内容

java - 创建可运行的 Jar 文件。 (java.lang.NoClassDefFoundError)

c++ - 实现自定义 QListWidgetItem?

ios - 点击时 SwiftUI 按钮不灰显

delphi - 应用程序忙时显示的表单上的按钮没有响应

java - 使用 Elasticsearch API 实现不等于

c# - 透明背景显示黑色

javascript - html5表单提交问题

java - 采用 "not"但不采用 "not like"的词法分析器