我在加載到AnchorPane中的FXML的佈局時遇到了問題。如何在將一個FXML加載到AnchorPane時自動調整大小?
我有一個AnchorPane和一個按鈕的Scene.fxml。按下按鈕將Internal.fxml加載到AnchorPane中。 Internal.fxml具有一個將AnchorPane Constraints設置爲左側和右側的TextField。就是這樣。
雖然現在調整窗口大小時TextField不會調整大小。
爲什麼TextField不能自行調整大小?下面代碼中的錯誤在哪裏?
Main.java:
@Override
public final void start(final Stage firstStage) throws Exception {
Parent mask = FXMLLoader.load(getClass()
.getResource("/fxml/Scene.fxml"));
Scene scene = new Scene(mask);
scene.getStylesheets().add("/styles/Styles.css");
firstStage.setTitle("MyProgram");
firstStage.setScene(scene);
firstStage.show();
Main.stage = firstStage;
}
Scene.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane id="AnchorPane" fx:id="content" prefHeight="600.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.treasury.myprogram.controller.SceneController">
<children>
<Button layoutX="257.0" layoutY="227.0" mnemonicParsing="false" onAction="#loadInternal" text="Button" />
</children>
</AnchorPane>
Internal.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-1.0" maxWidth="-1.0" minHeight="400.0" minWidth="600.0" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.treasury.myprogram.controller.InternalController">
<children>
<TextField layoutY="14.0" prefWidth="-1.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0" />
</children>
</AnchorPane>
SceneController.java:
package com.treasury.myprogram.controller;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
public class SceneController implements Initializable {
@FXML
private AnchorPane content;
@FXML
private void loadInternal() {
try {
AnchorPane internalPane = FXMLLoader.load(getClass().getResource("/fxml/Internal.fxml"));
this.content.getChildren().setAll(internalPane);
} catch (IOException ex) {
System.out.println("Internal error: " + ex.getMessage());
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
編輯:
好吧,首先感謝很多,現在的代碼工作。那麼,其中的一半呢,但至少第一個問題已經消失了。我現在有一個FXML只能工作一半。它有一個GridPane,下面有幾個條目和一個TextField。他們都直接在AnchorPane上。現在,當我處於JavaFX Scene Builder 1.1的構建模式時,一切都很正常,但是當我將此Internal.fxml加載到Scene.fxml中時,只有TextField適應Parent大小,但GridPane不適用。我無法找到原因。 什麼問題?錯配在哪裏?
Internal.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<AnchorPane id="AnchorPane" maxHeight="-1.0" maxWidth="-1.0" minHeight="400.0" minWidth="600.0" prefHeight="-1.0" prefWidth="-1.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.treasury.myprogram.controller.InternalController">
<children>
<GridPane gridLinesVisible="true" prefHeight="-1.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="5.0">
<children>
<Label text="Program name:" GridPane.columnIndex="0" GridPane.rowIndex="0" />
<Label text="Version:" GridPane.columnIndex="0" GridPane.rowIndex="1" />
<Label text="Release date:" GridPane.columnIndex="0" GridPane.rowIndex="2" />
<Label text="myprogram" GridPane.columnIndex="1" GridPane.rowIndex="0" />
<Label text="1.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Label text="28.02.2014" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField prefWidth="200.0" GridPane.columnIndex="0" GridPane.rowIndex="3" />
<TextField prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
</children>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="294.0" minWidth="10.0" prefWidth="112.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="502.0" minWidth="10.0" prefWidth="478.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
<TextField layoutY="125.0" prefWidth="590.0" AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" />
</children>
</AnchorPane>
我發佈了一個編輯我的問題與不工作的FXML爲例。 – Socrates
意思是現在你的問題解決了? –
此解決方案適用於我。謝謝Rajeev – Menefee