【问题标题】:Why won't my textfield stretch on resize?为什么我的文本字段不会在调整大小时拉伸?
【发布时间】:2016-02-24 00:54:10
【问题描述】:

我无法真正理解这种拉伸在 JavaFX 中是如何工作的。对于 HBox 和 TextField,我的 prefWidth 为 Infinity,所以如果我们调整框架的大小,TextField 应该更大。请帮助我,我在这里缺少什么。谢谢。

<GridPane fx:id="first" hgap="5" vgap="5"  xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" >

<columnConstraints>
    <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" />
</columnConstraints>

<VBox.margin>
    <Insets left="10.0" top="15"/>
</VBox.margin>
<HBox GridPane.columnIndex="0" GridPane.rowIndex="0" prefWidth="Infinity"  maxWidth="Infinity">

    <fx:define>
        <ToggleGroup fx:id="myToggleGroup"/>
    </fx:define>
    <children>
        <RadioButton text="System" toggleGroup="$myToggleGroup">
        </RadioButton>
        <RadioButton text="Document" toggleGroup="$myToggleGroup">
            <HBox.margin>
                <Insets left="200.0"/>
            </HBox.margin>
        </RadioButton>
    </children>
</HBox>
<HBox GridPane.rowIndex="1" GridPane.columnIndex="0"  >
    <Label text="Name:" minWidth="50">
    </Label>

    <TextField maxWidth="Infinity" minWidth="450" prefWidth="Infinity">
        <HBox.margin>
            <Insets left="20.0"/>
        </HBox.margin>
    </TextField>
</HBox>

【问题讨论】:

  • 看看stackoverflow.com/questions/35438104/…(不同,但对网格窗格中的布局如何工作有完整的解释)是否有帮助。
  • 另外我只能在这个项目中使用fxml。
  • 没关系,该问题中的所有内容也可以使用FXML完成。 (虽然,“only 允许使用 FXML”???不能只用 FXML 编写应用程序,控制器中需要 Java。)
  • 是的,我只指 GUI。

标签: javafx


【解决方案1】:

默认情况下,HBox 将分配控件的首选大小。我不确定他们将如何处理"Infinity" 的首选大小(我从布局计算中得到一些错误):我将删除所有prefWidth="Infinity" 并告诉HBox 允许文本字段成长:

<TextField HBox.hgrow="ALWAYS" maxWidth="Infinity">

这是一个完整的例子(稍作修改以适应 SSCCE):

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.geometry.Insets?>

<GridPane fx:id="first" hgap="5" vgap="5"
    xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1">

    <columnConstraints>
        <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" />
    </columnConstraints>

    <HBox GridPane.columnIndex="0" GridPane.rowIndex="0">

        <fx:define>
            <ToggleGroup fx:id="myToggleGroup" />
        </fx:define>
        <children>
            <RadioButton text="System" toggleGroup="$myToggleGroup">
            </RadioButton>
            <RadioButton text="Document" toggleGroup="$myToggleGroup">
                <HBox.margin>
                    <Insets left="200.0" />
                </HBox.margin>
            </RadioButton>
        </children>
    </HBox>
    <HBox GridPane.rowIndex="1" GridPane.columnIndex="0">
        <Label text="Name:" minWidth="50">
        </Label>

        <TextField HBox.hgrow="ALWAYS" maxWidth="Infinity">
            <HBox.margin>
                <Insets left="20.0" />
            </HBox.margin>
        </TextField>
    </HBox>
</GridPane>

以及运行它的代码:

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {
        primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("layout.fxml"))));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 在旁注中,如果我拉伸,我如何实现我的第二个单选按钮始终保持在中间?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
相关资源
最近更新 更多