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

时间:2016-05-12 09:48:47

标签: java javafx

当我启动应用程序时,TitledPane没有显示我添加的GridPane。令人惊讶的是,当我增加/减小窗口宽度时,它是可见的。我错过了哪里?

以下是完整代码:

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);
    }
}

1 个答案:

答案 0 :(得分:0)

我不知道原因的确切原因,但如果TitledPane用作root用户,则Scene似乎没有调整大小。

我试过这样的事情:

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

然后一切都按预期工作。

修改 我找到了here

  

不要明确设置a的最小,最大或首选高度   标题窗格,因为这可能会导致标题时的意外行为   窗格打开或关闭。

所以我的猜测是,当您将TitledPane设置为场景的根目录时,它会尝试设置首选高度,这会导致提到的“意外行为”。