簡體   English   中英

添加內容時如何自動調整對話框窗格大小?

[英]How to automatically resize dialog pane when content is added?

對於我正在開發的應用程序,我需要用戶輸入多行數據。 確切的數字是可變的,用戶也應該能夠自己添加行。 我現在正在使用JavaFX Dialog,除了當你添加行時,Dialog沒有相應調整大小。 有沒有辦法讓Dialog在添加行時自動調整大小?

下面是一個演示應用程序,其中包含一個示例測試應用程序,其中的Dialog類似於我想要的。

package test_dialog;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Test_Dialog extends Application {

    class ScalableDialog extends Dialog<String> {

        int nrRows = 2;

        public ScalableDialog() {

            // We are resizable
            setResizable(true);

            // Set up the grid pane.
            GridPane grid = new GridPane();
            grid.setHgap(10);
            grid.setVgap(5);
            grid.setMaxWidth(Double.MAX_VALUE);
            grid.setAlignment(Pos.CENTER_LEFT);

            // Set up dialog pane
            DialogPane dialogPane = getDialogPane();
            dialogPane.setHeaderText(null);
            dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
            dialogPane.setContent(grid);

            // Create some fields to start with
            for (int i = 0; i < nrRows; i++) {
                grid.addRow(i, new TextField("Row: " + i));
            }

            // Add button
            final Button buttonAdd = new Button("Add Row");
            buttonAdd.setOnAction((ActionEvent e) -> {
                // Move Button to next row
                GridPane.setRowIndex(buttonAdd, nrRows + 1);
                // Insert new text field row
                grid.addRow(nrRows, new TextField("New: " + nrRows++));
            });
            grid.add(buttonAdd, 0, nrRows);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        Button button = new Button();
        button.setText("Open Dialog");
        button.setOnAction(e -> {
            new ScalableDialog().showAndWait();
        });

        StackPane root = new StackPane();
        root.getChildren().add(button);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Scalable Dialog Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

}

dialogPane.getScene().getWindow().sizeToScene();

on“添加”按鈕的動作事件處理程序。
Stage.sizeToScene()類似於Swing的jframe.pack()。 在底部,對話框被添加到某個(子,次)階段,我們可以通過getScene()。getWindow()獲取它。

暫無
暫無

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

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