簡體   English   中英

JavaFX按鈕上的新場景單擊

[英]JavaFX New Scene on Button Click

標題可能有點含糊,所以讓我對其定義更好一些。 我有一段有效的代碼(在下面):我正在開發的游戲的簡單主菜單。 除“開始”按鈕外,其他所有內容均正常運行。

我想要做的是單擊“開始”按鈕,並在同一舞台(窗口)上出現一個新場景。 我不想看到一個新窗口打開。 我已經與Java方面的經驗豐富的人進行了交談,他們告訴我為MenuFX和GameFX創建單獨的類。 如果是這種情況,我需要從MenuFX類中調用GameFX類的一些啟動或啟動方法,對嗎? 這是最好的方法,還是我想將所有與FX相關的代碼都放在一個類中? 另外,我應該在所有外匯工作中保持同一階段,不是嗎?

這篇文章闡明了一些事情,但是我對所討論的某些術語並不精通-例如,我仍然不了解Root的概念。

另外, 本文討論了類似的應用程序,但是我沒有使用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類的全部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());
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM