gpt4 book ai didi

java - 将 FXML 加载到 AnchorPane 时如何自动调整大小?

转载 作者:行者123 更新时间:2023-12-01 17:16:05 27 4
gpt4 key购买 nike

我在加载到 AnchorPane 中的 FXML 布局时遇到问题。

我有一个 Scene.fxml,上面有一个 AnchorPane 和一个按钮。按下按钮将 Internal.fxml 加载到 AnchorPane 中。 Internal.fxml 有一个 TextField,其 AnchorPane 约束设置为左侧和右侧。就是这样。

尽管现在调整窗口大小时,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;
}

场景.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>

内部.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 适应父级大小,但 GridPane 不会。我很难找出原因。有什么问题?哪里配置错误?

内部.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>

最佳答案

尝试使用 BorderPane 而不是锚定 Pane ,并将子 fxml 的对象设置为中心。还要检查父 fxml 是否在场景生成器中自动调整大小。

@FXML private BorderPane bPaneHomeChild;
FXMLLoader loader = new FXMLLoader(getClass.getResource("/fxml/Internal.fxml"));
Pane cmdPane = (Pane) loader.load();
bPaneHomeChild.setCenter(cmdPane);

关于java - 将 FXML 加载到 AnchorPane 时如何自动调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22066555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com