我有一個只有一個FXML文件的JavaFX應用程序。在這個文件中我有一個AnchorPane,裏面有一個StackPane。下面是截圖:JavaFX面板內部面板自動調整大小
當我啓動這個應用程序,我想與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%;
}
你能接受這個答案。將讓我們知道這個問題有一個解決方案。良好的研究結果,正在尋找這個。 – blo0p3r 2013-03-05 13:42:15
@ blo0p3r我會但現在系統說:「你可以在2天內接受你自己的答案」;)希望有更多的其他建議。 – 2013-03-05 14:32:26
嗨。作爲一個按鈕被添加。例如。 \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