Java 将 FXML 加载到 AnchorPane 时如何自动调整大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22066555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize automatically when loading an FXML into an AnchorPane?
提问by Socrates
I am having trouble with the layout of an FXML I load into an AnchorPane.
我在加载到 AnchorPane 的 FXML 布局方面遇到问题。
I have a Scene.fxml with an AnchorPane and a button on it. Pressing the button loads the Internal.fxml into the AnchorPane. The Internal.fxml has a TextField with the AnchorPane Constraints set to left and right. That's about it.
我有一个带有 AnchorPane 和按钮的 Scene.fxml。按下按钮将 Internal.fxml 加载到 AnchorPane 中。Internal.fxml 有一个 TextField,其中 AnchorPane 约束设置为左右。就是这样。
Though now when resizing the window the TextField does not resize.
虽然现在在调整窗口大小时 TextField 不会调整大小。
Why doesn't the TextField resize itself? Where is the error in the code below?
为什么 TextField 不调整自身大小?下面代码中的错误在哪里?
Main.java:
主.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:
场景.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:
内部.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:
场景控制器.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) {
}
}
Edit:
编辑:
Ok, first off thanks a lot, now the code works. Well, half of it does, but at least the first issue is gone. I now have an FXML that works only half. It has a GridPane with a couple of entries and a TextField beneath. Both of them are directly on the AnchorPane. Now, when I am in the construction mode in JavaFX Scene Builder 1.1 everything looks fine, but when I load this Internal.fxml into the Scene.fxml only the TextField adapts to the Parent size, but the GridPane doesn't. I am having trouble finding out why. What's the problem? Where is the missconfiguration?
好的,首先非常感谢,现在代码可以工作了。嗯,有一半是这样,但至少第一个问题已经过去了。我现在有一个只能工作一半的 FXML。它有一个 GridPane,下面有几个条目和一个 TextField。它们都直接在 AnchorPane 上。现在,当我在 JavaFX Scene Builder 1.1 中处于构造模式时,一切看起来都很好,但是当我将此 Internal.fxml 加载到 Scene.fxml 中时,只有 TextField 适应父级大小,而 GridPane 不适应。我很难找出原因。有什么问题?错误配置在哪里?
Internal.fxml:
内部.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>
采纳答案by Rajeev Gupta
Try to use a BorderPane rather than anchor pane and set the object of child fxml into center. Also check whether parent fxml is auto resizable in Scene Builder.
尝试使用 BorderPane 而不是锚窗格并将子 fxml 的对象设置为中心。还要检查父 fxml 是否可以在 Scene Builder 中自动调整大小。
@FXML private BorderPane bPaneHomeChild;
FXMLLoader loader = new FXMLLoader(getClass.getResource("/fxml/Internal.fxml"));
Pane cmdPane = (Pane) loader.load();
bPaneHomeChild.setCenter(cmdPane);
回答by Sudip Saha
Try like this:
像这样尝试:
AnchorPane.setTopAnchor(<child node>,0.0);
AnchorPane.setBottomAnchor(<child node>,0.0);
AnchorPane.setLeftAnchor(<child node>,0.0);
AnchorPane.setRightAnchor(<child node>,0.0);
回答by David Wong
回答by RTZ ZZ
There are really good reasons to never use any of this. JavaFX and JavaFXML are bad rehashes of someone else's work namely Microsoft. The tools that Oracle provide look like they came straight out of 1975. You are struggling with this product and framework because it is poorly thought out and even more poorly executed. Live up to your potential, join the web revolution, Angular, REACT, MVC whatever. Just don't hang yourself with someones idea of punishing his fellow programmer. Java had it's moment in 2000 to 2005. That time is over, the rest are just hanging on. Don't hang up, upgrade yourself.
有充分的理由永远不要使用任何这些。JavaFX 和 JavaFXML 是其他人工作的糟糕翻版,即微软。Oracle 提供的工具看起来像是 1975 年直接出现的。您正在为这个产品和框架而苦苦挣扎,因为它没有经过深思熟虑,而且执行起来更加糟糕。发挥你的潜力,加入网络革命,Angular、REACT、MVC 等等。只是不要因为某人惩罚他的程序员同事的想法而上吊。Java 在 2000 年到 2005 年间迎来了它的时刻。那段时间已经过去了,其余的都还在坚持。别挂了,升级自己。