JavaFx 变化场景

Mic*_* H. 0 user-interface javafx netbeans-8

我是 javaFX 的新手,对 java 有初级水平的理解。我正在尝试构建一个最终会生成表单的简单应用程序。我想在选择按钮时更改场景,但我不知道该怎么做,我读过的所有内容都超出了我的水平。`

@Override
public void start(Stage primaryStage) {

         primaryStage.setTitle("Welcome to the Log Book Generator");
         /*Defining Options on Home screen*/

         Button btnR = new Button("Repair");
         Button btnM = new Button("Maintenance");
         Button btnW = new Button("Weather");
         Button btnO = new Button ("Other");
         Button btnU = new Button ("Filter Pickup");
         Button btnVC = new Button ("Verification/Calibration");
         Button btnE = new Button ("Exit");



    /*Actions upon button selection*/    
    btnR.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Repair");
        }
    });
    btnM.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Maintenance");
        }
    });
    btnW.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Weather");
        }
    });
    btnO.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Other");
        }
    });
    btnU.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Filter Pickup");
        }
    });
    btnVC.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Verification/Calibration");
        }
    });
    btnE.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.exit(0);
        }
    });
    Pane root = new Pane();
    /*StackPane root = new StackPane();
    /* Setting Button Layout*/
    btnM.setLayoutX(150);btnM.setLayoutY(150);
    btnR.setLayoutX(150);btnR.setLayoutY(250);
    btnW.setLayoutX(150);btnW.setLayoutY(350);
    btnO.setLayoutX(150);btnO.setLayoutY(150);
    btnU.setLayoutX(150);btnU.setLayoutY(450);
    btnVC.setLayoutX(150);btnVC.setLayoutY(550);
    btnE.setLayoutX(350);btnE.setLayoutY(650);
    /*Ask user for Selection*/
    Label label;
    label = new Label("Please select a task.");
    label.setFont(Font.font("Arial", 32));            
    root.getChildren().add(label);
    root.getChildren().add(btnE);
    root.getChildren().add(btnVC);
    root.getChildren().add(btnU);
    root.getChildren().add(btnO);
    root.getChildren().add(btnW);
    root.getChildren().add(btnM);
    root.getChildren().add(btnR);
    primaryStage.setScene(new Scene(root, 500, 750));
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}
Run Code Online (Sandbox Code Playgroud)

} ` 我计划在同一阶段将不同的部分制作成自己的场景。任何帮助,将不胜感激。我正在使用 NetBeans8.2。

Zep*_*hyr 5

而不是尝试更改Scene,看起来您只是希望更新屏幕的某些部分。

下面是一个简单的例子,它使用 aBorderPane作为根布局,然后在CENTER单击按钮时更改窗格的内容。

下面只是采样开关的内容进行不同的标签,但你可以很容易地使用root.setCenter()通过整体VBoxHBox或任何其他填充容器。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    private BorderPane root;

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

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setWidth(500);

        // Simple interface. The buttons will be on the left and the contents of the center section will change
        // when buttons are clicked.
        root = new BorderPane();
        root.setPadding(new Insets(10));

        // Create the left pane, containing buttons to switch the CENTER content
        VBox paneButtonBox = new VBox(5);
        paneButtonBox.setAlignment(Pos.TOP_CENTER);
        paneButtonBox.setPadding(new Insets(10));

        // Create 3 buttons to change the contents of the CENTER
        Button btnView1 = new Button("View 1");
        btnView1.setOnAction(e -> switchContent(1));
        Button btnView2 = new Button("View 2");
        btnView2.setOnAction(e -> switchContent(2));
        Button btnView3 = new Button("View 3");
        btnView3.setOnAction(e -> switchContent(3));

        // Add the Buttons to the button box
        paneButtonBox.getChildren().addAll(btnView1, btnView2, btnView3);

        // Add the button box to the LEFT of root pane
        root.setLeft(paneButtonBox);

        primaryStage.setScene(new Scene(root));

        primaryStage.show();
    }

    /**
     * @param view is the # of the content we want to display. For this sample, just to demonstrate.
     */
    private void switchContent(int view) {

        // Change the content of the CENTER node based on button clicked
        switch (view) {
            case 1:
                root.setCenter(new Label("THIS IS VIEW 1"));
                break;
            case 2:
                root.setCenter(new Label("THIS IS VIEW 2"));
                break;
            case 3:
                root.setCenter(new Label("THIS IS VIEW 3"));
                break;

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码产生以下布局:

截屏

Label右边的变化,因为每个按钮被点击。希望这有助于引导您朝着正确的方向前进。

基本概念是创建布局的一部分,您可以更改其内容。BorderPane为您提供了这些部分,但您也可以VBox自己创建一个单独的部分,VBox.getChildren().addAll()并在Node需要更改内容时调用并传入任何您想要的对象。

另外一个选项

实现类似界面功能的另一种方法是使用TabPane. Tab您添加到 的每个TabPane都有一个content属性,您可以使用该属性将任何节点设置为您选择的,类似于BorderPane上面的工作方式:

@Override
public void start(Stage primaryStage) {

    primaryStage.setHeight(300);
    primaryStage.setWidth(500);

    TabPane root = new TabPane();

    // Create Separate Tabs
    Tab tab1 = new Tab("Section 1");
    tab1.setContent(new Label("This is Section 1!"));
    Tab tab2 = new Tab("Section 2");
    tab2.setContent(new Label("This is Section 2!"));
    Tab tab3 = new Tab("Section 3");
    tab3.setContent(new Label("This is Section 3!"));

    root.getTabs().addAll(tab1, tab2, tab3);

    primaryStage.setScene(new Scene(root));
    primaryStage.show();

}
Run Code Online (Sandbox Code Playgroud)

结果:

截屏