在不同场景javafx之间共享窗格

时间:2019-03-17 15:14:59

标签: java javafx

我正在尝试实现一系列菜单按钮,这些菜单按钮将在不同的场景之间切换,但是,我试图以一种可以对所有场景重复使用菜单窗格(所有菜单按钮都位于其中)的方式对其进行优化。将显示它。

代码如下:

public class Main extends Application {

    Stage window;
    Scene scene1, scene2;

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

@Override
public void start(Stage primaryStage) throws Exception {
    window = primaryStage;
    window.setTitle("Arty's Drone Delivery Server");

    //MainMenu pane
    final VBox vpane = new VBox(10);
    final Button postB = new Button("Postcodes");
    final Button dronesB = new Button("Drones");
    final Button staffB = new Button("Staff");
    vpane.getChildren().addAll(postB, dronesB, staffB);

    //Drones Box
    DronesBox dronesPane = new DronesBox();
    //Poscodes Box
    PostCodeBox postCodesPane = new PostCodeBox();

    //Layout 2
    BorderPane border2 = new BorderPane();
    border2.setPadding(new Insets(20, 20, 20, 20));
    border2.setLeft(vpane);
    border2.setCenter(dronesPane);
    scene2 = new Scene(border2, 600, 400);

    //Layout 1
    BorderPane border1 = new BorderPane();
    border1.setPadding(new Insets(20, 20, 20, 20));
    border1.setLeft(vpane);
    border1.setCenter(postCodesPane);
    scene1 = new Scene(border1, 600, 400);

    //button event listeners
    postB.setOnAction(e -> window.setScene(scene1));
    dronesB.setOnAction(e -> window.setScene(scene2));

    window.setScene(scene1);
    window.show();
    }


}

class PostCodeBox extends VBox{
    PostCodeBox(){
        Label title = new Label("Postcodes:");
        TextField textbox = new TextField();
        textbox.setPrefSize(200, 350);
        textbox.setEditable(false);
        textbox.appendText("SO16 3UH \n");
        textbox.appendText("SO14 3WL \n");

        HBox actionPane = new HBox(3);
        Button add = new Button("Add");
        Button remove = new Button("Remove");
        Button edit = new Button("Edit");
        actionPane.getChildren().addAll(add, remove, edit);

        this.getChildren().addAll(textbox, actionPane);

    }
}

DronesBox的处理原理与PostCodeBox相同。

现在,据我所知,当按下Drones按钮时,它会跳至下一个场景,但是在继承vpane时存在问题,这在技术上已被添加到这两个场景中。在我看来,我必须以某种方式使其成为“主面板”,该面板将显示在所有按钮按下的变体上。

Scene 1

Scene 2

我可以尝试执行的另一种方法是从边界窗格中删除一个对象并添加一个新对象,但是我根本就不会切换场景。

0 个答案:

没有答案