2016-05-12 79 views
0

當我啓動應用程序時,TitledPane不顯示我添加的GridPane。令人驚訝的是,當我增加/減少窗口寬度時,它是可見的。我在哪裏失蹤?GridPane不可見,除非我調整窗口大小javaFx

下面是完整的代碼:

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

回答

0

我不知道確切的原因,但似乎TitledPane如果用作根爲Scene不能調整大小。

我想是這樣的:

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

然後一切正常。

編輯: 我發現這個here

不明確設置的最小,最大或 名爲窗格的首選高度,因爲這可能會導致意外的行爲,當標題 窗格打開或關閉。

所以我的猜測是,當您將TitledPane設置爲場景根目錄時,它會嘗試設置首選高度,導致提到的「意外行爲」。

+0

我想你是對的。如果我不提供尺寸參數到場景,它的效果很好。我的最終目標是將這個數據庫網格添加爲可觀察對象之一。所以我的工作就完成了!感謝DVarga。 – Ajeetkumar