您可以将两个不同的 Java FX 场景编写为两个单独的类吗?

Pet*_*ete 5 java javafx stage scene

我是一名初级 Java 程序员,在当地大学完成了“Java 101”课程。我也在推动自己学习一些额外的主题,包括 Java FX。我已经学习了 Oracle 网站上的 Java FX 教程,还浏览了一些 YouTube 视频,还阅读了“Java FX for Dummies”(这是我能找到的最适合初学者的书。)所有这些材料都教会了我很多基础知识,但有些(应该)相对简单的东西让我望而却步。

例如:假设我有一个 Java FX 程序,它在一个舞台上使用多个场景。当用户点击“切换!” 按钮,第二个场景被换出第一个。简单。我可以在一个 .java 文件中完成所有这些,没问题。(见下面的代码)

但是我的 .java 类文件变得很长而且很难进行故障排除。如果我可以将一个场景定义/声明/初始化为一个 .java 文件中的一个类,并将第二个场景定义/声明/初始化为另一个 .java 文件中的另一个类,那就太好了。这将使跟踪每个场景的组件变得更加容易。问题是,我不知道如何做到这一点。

我想你会先写一个 Scene1.java 类,然后写一个 Scene2.java 类,当你想切换场景时,只需在两者之间传递舞台对象。但是我找不到一个例子来说明这是如何完成的,我所有的尝试都会导致编译器错误或非常可怕的运行时错误。

有谁知道如何做到这一点?如果是这样,我需要怎么做才能修改SwitchScenes2()下面的方法以创建新Scene2对象并将其传递给舞台?

谢谢!饶

/*
    JavaFXExample.java
*/

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.geometry.*;

public class JavaFXExample extends Application{

    public static void main(String[] args){
    launch(args);   
}

Button btnSw1;
Button btnSw2;
Button btnClose;
HBox hbox1;
VBox vbox1;
Scene scene1;
Scene scene2;
Stage stage;

@Override public void start(Stage primaryStage){
    btnSw1 = new Button("Switch Scenes!");
    btnSw1.setOnAction(
        e -> SwitchScenes2() );
    btnSw2 = new Button("Switch back!");
    btnSw2.setOnAction(
        e -> SwitchScenes1() );
    btnClose = new Button();
    btnClose.setText("Close me!");
    btnClose.setOnAction(e -> CloseWindowClick());

    hbox1 = new HBox(10);
    hbox1.getChildren().addAll(btnSw1);
    vbox1 = new VBox(10);
    vbox1.getChildren().addAll(btnSw2, btnClose);

    scene1 = new Scene(hbox1, 300, 300);
    scene2 = new Scene(vbox1, 200, 400);

    stage = primaryStage;
    stage.setScene(scene1);
    stage.setTitle("Example App");
    stage.show();
   }

    public void SwitchScenes1(){
        stage.setScene(scene1);
    }
    public void SwitchScenes2(){
        stage.setScene(scene2);
    }
    public void CloseWindowClick(){
        stage.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

Kon*_*n F 4

据我了解,Pete 您希望将一个大的 java 文件分成小文件,在每个类中创建 Java 类,创建将返回布局(HBox、VBox、Flowpane 或 ....)的方法(函数),然后在主文件中创建一个对象该 Java 类并使用这些方法构建大型应用程序。

在我的示例中,我创建了一个主要类和一个具有一个函数的独立类,只是为了向您展示它是如何工作的。在我的主目录中,有 2 个标签、2 个按钮、一个布局和一个分离类的对象,通过单击按钮场景将更改我的主目录:

public class SwitchSceneSample extends Application {
public static void main(String[] args) {
    launch(args);
}

Stage window;
Scene scene1, scene2;

@Override
public void start(Stage primaryStage) throws Exception {
    // I am using window as primaryStage
    window = primaryStage;
    // Label 1
    Label label1 = new Label("Welcome to the first scene!");
    // Label 2
    Label label2 = new Label("This is second scene!");
    // Button 1, by pressing this button primaryStage will be set as scene 2
    Button button1 = new Button("Go to scene 2");
    button1.setOnAction(e -> window.setScene(scene2));
    // Button 2, by pressing this button primaryStage will be set as scene 1
    Button button2 = new Button("Click to go scene 1");
    button2.setOnAction(e -> window.setScene(scene1));
    // Creating an object of the class'LayoutOne.java'
    LayoutOne l1 = new LayoutOne();
    // set my scene 1(by calling method called 'sceneView1()' from class 'LayoutOne.java')
    scene1 = new Scene(l1.sceneView1(label1, button1), 200, 200);
    // Set my scene 2 inside my main class
    StackPane layout2 = new StackPane();
    layout2.getChildren().addAll(label2, button2);
    scene2 = new Scene(layout2, 600, 300);
    // Making my 
    window.setScene(scene1);
    window.setTitle("Scene Switch Sample");
    window.show();
}
Run Code Online (Sandbox Code Playgroud)

}

我的第二堂课:

public class LayoutOne {
public VBox sceneView1(Label label, Button button) {

    // Layout 1 - children are laid out in vertical column
    VBox layout1 = new VBox(20);
    layout1.getChildren().addAll(label, button);

    return layout1;
}
Run Code Online (Sandbox Code Playgroud)

}