将表单添加到弹出阶段(自定义阶段内容)

add a form into pop up stage (Customize Stage content)

我有一个名为“添加新用户”的按钮,点击后会出现一个包含添加用户表单的弹出窗口 window。 这是我对该按钮的功能:

public void  btnAddeNewClicked(){


        final TextField name = new TextField() ;
        final TextField addr = new TextField() ;
        final TextField wp = new TextField() ;
        final TextField state = new TextField() ;
        final TextField loginName = new TextField() ;
        final Label labelUsername = new Label();
        final Label labelAddress = new Label();
        final Label labelWebPage = new Label();
        final Label labelState = new Label();
        final Label labelloginName = new Label();
        final Button btn = new Button("Add");


        Stage dialog = new Stage();
        dialog.initStyle(StageStyle.UTILITY);
        Scene scene = new Scene(btn);
        dialog.setScene(scene);
        dialog.show();


}

问题是我不知道如何将我的表单添加到我的舞台中。非常感谢任何帮助:)

选择合适的布局 Parent 以添加您的 Node。有很多可能性,所以我就用一个很简单的例子来演示一下:VBox,就是把所有children的地方都竖排成一行:

final TextField name = new TextField() ;
final TextField addr = new TextField() ;
final TextField wp = new TextField() ;
final TextField state = new TextField() ;
final TextField loginName = new TextField() ;
final Label labelUsername = new Label();
final Label labelAddress = new Label();
final Label labelWebPage = new Label();
final Label labelState = new Label();
final Label labelloginName = new Label();
final Button btn = new Button("Add");

VBox root = new VBox();
root.getChildren().addAll(name,
                          addr,
                          wp,
                          state,
                          loginName,
                          labelUsername,
                          labelAddress,
                          labelWebPage,
                          labelState,
                          labelloginName,
                          btn);

Stage dialog = new Stage();
dialog.initStyle(StageStyle.UTILITY);
Scene scene = new Scene(root);
dialog.setScene(scene);
dialog.show();

请注意,如果需要更复杂的布局,这些布局也可以用作其他布局的 children。使用 SceneBuilder 对初学者来说可能更容易,但无论如何您都应该知道布局如何定位 children。很容易看到生成的 fxml 文件中的 child-parent 关系在 java 代码中重现了相同的布局。

使用任何 layout 添加弹出窗口的内容。稍后,将此布局添加为场景的根。

在下面的示例中,我使用了 GridPane

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button button = new Button("Show Popup");
        button.setOnAction(e -> btnAddeNewClicked());
        Scene scene = new Scene(new StackPane(button), 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void  btnAddeNewClicked(){

        final TextField name = new TextField() ;
        final TextField addr = new TextField() ;
        final TextField wp = new TextField() ;
        final Label labelUsername = new Label("Username");
        final Label labelAddress = new Label("Address");
        final Label labelWebPage = new Label("Web Page");
        final Button btn = new Button("Add");

        GridPane gridPane = new GridPane();
        gridPane.setVgap(10);
        gridPane.setHgap(10);
        gridPane.setPadding(new Insets(10));

        gridPane.add(labelUsername, 0, 0);
        gridPane.add(name, 1, 0);
        gridPane.add(labelAddress, 0, 1);
        gridPane.add(addr, 1, 1);
        gridPane.add(labelWebPage, 0, 2);
        gridPane.add(wp, 1, 2);
        gridPane.add(btn, 0, 3, 2, 1);
        GridPane.setHalignment(btn, HPos.CENTER);

        Stage dialog = new Stage();
        dialog.initStyle(StageStyle.UTILITY);
        Scene scene = new Scene(gridPane);
        dialog.setScene(scene);
        dialog.show();
    }
}

输出