2013-03-05 779 views
41

我有一個只有一個FXML文件的JavaFX應用程序。在這個文件中我有一個AnchorPane,裏面有一個StackPane。下面是截圖:JavaFX面板內部面板自動調整大小

my panel inside panel application

當我啓動這個應用程序,我想與AnchorPane自動調整StackPane。從而; StackPane會自動獲得當前的可用寬度和高度。在調整應用程序的大小時,AnchorPane會自動調整大小,但StackPane保持其固定大小。

如何自動調整StackPane的大小並使其在其父面板中完全拉伸?

我的代碼

Main.java

package app; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

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

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("Main.fxml")); 
     Scene scene = new Scene(root,800,600); 
     scene.getStylesheets().add(this.getClass().getResource("/app/style1.css").toExternalForm()); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 

MainController.java

package app; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.StackPane; 

public class MainController implements Initializable { 

    @FXML 
    private AnchorPane anchorPane; 
    @FXML 
    private StackPane stackPane; 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
    stackPane.setPrefSize(anchorPane.getPrefWidth(), anchorPane.getPrefHeight()); //didn't work 
    }  
} 

Main.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController"> 
    <StackPane fx:id="stackPane" ></StackPane> 
</AnchorPane> 

style1.css

#anchorPane { 
    -fx-border-width: 2px; 
    -fx-border-color: chartreuse; 
} 
#stackPane { 
    -fx-border-width: 2px; 
    -fx-border-color: red; 

    /* didn't work */ 
    -fx-hgap: 100%; 
    -fx-vgap: 100%; 
} 

回答

62

經過搜索和測試的時間終於得到了它只是張貼問題了!

可以使用 「AnchorPane.topAnchor,AnchorPane.bottomAnchor,AnchorPane.leftAnchor,AnchorPane.rightAnchor」 具有值 「0.0」 FXML命令,以適應/拉伸/對齊AnchorPane中的子元素。所以,這些命令告訴子元素在調整大小時跟隨其父元素。

我更新的代碼Main.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 

<AnchorPane fx:id="anchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="app.MainController"> 
    <!--<StackPane fx:id="stackPane" ></StackPane>--> <!-- replace with the following --> 
    <StackPane fx:id="stackPane" AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" ></StackPane> 
</AnchorPane> 

海爾結果如下:

enter image description here

對於API文檔:http://docs.oracle.com/javafx/2/api/javafx/scene/layout/AnchorPane.html

+2

你能接受這個答案。將讓我們知道這個問題有一個解決方案。良好的研究結果,正在尋找這個。 – blo0p3r 2013-03-05 13:42:15

+2

@ blo0p3r我會但現在系統說:「你可以在2天內接受你自己的答案」;)希望有更多的其他建議。 – 2013-03-05 14:32:26

+0

嗨。作爲一個按鈕被添加。例如。 \t \t \t按鈕fx:id =「button」layoutX =「172.0」layoutY =「195.0」onAction =「#ingresarLogin」prefHeight =「35.0」prefWidth =「87.0」text =「Ingresar」/> \t 這是正確的? thanksssss – 2015-02-09 14:28:45

12

我在設計一個SceneBuilder GUI,試圖使主容器適應任何窗口大小。它應該始終是100%寬。

在這裏,您可以在SceneBuilder設置這些值:

AnchorPane Constraints in SceneBuilder

撥動點/紅線實際上只是添加/刪除Korki張貼在他的解決方案的屬性(AnchorPane.topAnchor等。 )。

+0

這會產生什麼FXML代碼?我想看看它。 – 2016-03-22 15:52:10

+0

那麼你是如何開始SceneBuilder並親眼看到的......它的完成時間不到20秒... – specializt 2016-05-11 10:04:14

6

另見here

@FXML 
private void mnuUserLevel_onClick(ActionEvent event) { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("DBedit.fxml")); 
    loader.setController(new DBeditEntityUserlevel()); 

    try { 
      Node n = (Node)loader.load(); 
      AnchorPane.setTopAnchor(n, 0.0); 
      AnchorPane.setRightAnchor(n, 0.0); 
      AnchorPane.setLeftAnchor(n, 0.0); 
      AnchorPane.setBottomAnchor(n, 0.0); 
      mainContent.getChildren().setAll(n); 
    } catch (IOException e){ 
      System.out.println(e.getMessage()); 
    } 
} 

的情況是孩子FXML加載到父AnchorPane。 爲了使孩子在符合其父母使用AnChorPane.setxxxAnchor命令拉伸。

0

這很簡單,因爲您正在使用FXMLBuilder

只要遵循這些簡單的步驟:

  1. 打開FXML文件到FXMLBuilder。
  2. 選擇堆棧窗格。
  3. 打開佈局選項卡[FXMLBuilder的左側選項卡]。
  4. 在舞臺調整大小(如TOP,LEFT,RIGHT,BOTTOM)期間,設置希望計算窗格大小的邊值。
0

如果您使用Scene Builder,您將在右側看到一個通常有三個選項(「屬性」,「佈局」和「代碼」)的手風琴面板。在第二個(「佈局」)中,您將看到一個名爲「[parent layout] Constraints」的選項(在您的情況下爲「AnchorPane Constrainsts」)。

您應該將「0」放在表示父佈局的元素的四邊。

0

無需放棄。

只是選擇窗格右擊然後選擇適合於母公司

它會自動將窗格大小調整爲錨定窗格大小。