如何在将FXML加载到AnchorPane时自动调整大小? [英] How to resize automatically when loading an FXML into an AnchorPane?

查看:604
本文介绍了如何在将FXML加载到AnchorPane时自动调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble with the layout of an FXML I load into an AnchorPane.

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

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.

虽然现在调整窗口大小时TextField不会调整大小。

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?

Main.java:

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:

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:

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:

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不适合。我无法找到原因。
有什么问题?错误配置在哪里?

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?

Internal.fxml:

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>


推荐答案

尝试使用BorderPane而不是锚定窗格并设置孩子fxml的对象进入中心。
还要检查父fxml是否是场景构建器中的atuo resizabel。

Try to use a BorderPane rather than anchor pane and set the object of child fxml into centre. Also check whether parent fxml is atuo resizabel in scene builder.

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

这篇关于如何在将FXML加载到AnchorPane时自动调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆