简体   繁体   English

除非我调整窗口javaFx的大小,否则GridPane不可见

[英]GridPane not visible unless i resize window javaFx

when i start the application, TitledPane does not show the GridPane I have added. 当我启动应用程序时,TitledPane不会显示我添加的GridPane。 Surprisingly it's visible the moment i increase/decrease the window width. 出乎意料的是,当我增加/减小窗口宽度时,它是可见的。 where am i missing? 我在哪里想念?

Here is the Complete Code: 这是完整的代码:

package com.ct.bic.comparator;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

public class Comparator extends Application {

    @Override
    public void start(Stage stage) {

        GridPane dbGrid = new GridPane();
        dbGrid.setId("dbGrid");

        dbGrid.setAlignment(Pos.TOP_LEFT);
        dbGrid.setHgap(10);
        dbGrid.setVgap(10);
        dbGrid.setPadding(new Insets(25, 25, 25, 25));

        Label dbConnection = new Label("Database Configuration");
        dbConnection.setId("dbConnection");
        dbConnection.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        dbGrid.add(dbConnection, 0, 0, 2, 1);

        Label server = new Label("Server");
        server.setId("server");
        dbGrid.add(server, 0, 1);
        TextField serverText = new TextField();
        serverText.setId("serverText");
        dbGrid.add(serverText, 1, 1);

        Label database = new Label("Database");
        database.setId("database");
        dbGrid.add(database, 0, 2);
        TextField databaseText = new TextField();
        databaseText.setId("databaseText");
        dbGrid.add(databaseText, 1, 2);

        Label user  = new Label("User");
        user.setId("user");
        dbGrid.add(user, 0, 3);

        TextField userText = new TextField();
        userText.setId("userText");
        dbGrid.add(userText, 1, 3);

        Label password  = new Label("Password");
        password.setId("password");
        dbGrid.add(password, 0, 4);

        PasswordField passwordText = new PasswordField();
        passwordText.setId("passwordText");
        dbGrid.add(passwordText, 1, 4);
        dbGrid.setId("passwordText");

        /*GridPane dbGrid = DatabaseInputGrid.getDatabaseGrid();*/
        TitledPane tp = new TitledPane("Database Configuration", dbGrid);

        Scene scene = new Scene(tp, 500,500);

        stage.setScene(scene);
        stage.setTitle("Bic-Java Output Comparator Pro");
        stage.show();
    }

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

I don't know the exact reason why, but it seems TitledPane is not resized if used as root for the Scene . 我不知道确切的原因,但是如果将TitledPane用作Scene根目录,则似乎不会调整其大小。

I tried something like this: 我尝试过这样的事情:

VBox vbox = new VBox();
vbox.getChildren().add(tp);
Scene scene = new Scene(vbox, 500,500);

Then everything works as expected. 然后一切都会按预期进行。

Edit: I have found this here : 编辑:在这里找到这个:

Do not explicitly set the minimal, maximum, or preferred height of a titled pane, as this may lead to unexpected behavior when the titled pane is opened or closed. 不要显式设置标题窗格的最小,最大或首选高度,因为在打开或关闭标题窗格时,这可能导致意外的行为。

So my guess is, when you set your TitledPane as root for the scene, it will try to set the preferred height, that leads to the mentioned "unexpected behaviour". 所以我的猜测是,当您将TitledPane设置为场景的根目录时,它将尝试设置首选的高度,从而导致提到的“意外行为”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM