You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX:TextArea与ListView无法随窗口扩展(Vgrow/Hgrow设置无效)

JavaFX布局适配问题:TextArea与ListView无法随窗口同步加宽的解决方法

问题场景

开发了一个简单JavaFX应用,初始布局采用BorderPane嵌套AnchorPane,内部包含两个Pane,其中一个嵌套HBox和VBox,VBox内有TextArea和ListView。希望窗口放大时这两个控件能同步加宽,已经设置了Vgrow/Hgrow为ALWAYS、Max Width/Max Height为MAX_VALUE,但没有效果。使用Scene Builder创建FXML。

原问题代码

FXML代码

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

<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <Pane layoutX="14.0" layoutY="14.0" prefHeight="372.0" prefWidth="572.0">
               <children>
                  <HBox layoutX="14.0" layoutY="14.0" prefHeight="344.0" prefWidth="544.0">
                     <children>
                        <Pane prefHeight="200.0" prefWidth="200.0" />
                        <VBox prefHeight="344.0" prefWidth="340.0">
                           <children>
                              <TextArea maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
                              <ListView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
                           </children>
                        </VBox>
                     </children>
                  </HBox>
               </children>
            </Pane>
         </children>
      </AnchorPane>
   </center>
</BorderPane>

Java主类代码

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

import java.io.IOException;

public class Main extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(Main.class.getResource("sample.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 600, 400);
        stage.setTitle("JavaFX Layout Test");
        stage.setScene(scene);
        stage.show();
    }

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

问题根源

冗余的AnchorPane和Pane嵌套打断了布局的尺寸传递逻辑:AnchorPane默认不会自动拉伸内部子节点(未设置锚点约束时),外层Pane也没有配置生长属性,导致VBox无法获取窗口放大后的可用空间,最终让内部的TextArea和ListView无法同步加宽。

解决方法

移除多余的布局容器,简化层级结构,仅保留必要的BorderPane和VBox(或HBox),让尺寸传递链保持通畅。

修改后的FXML代码

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

<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <HBox prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <VBox HBox.hgrow="ALWAYS">
               <children>
                  <TextArea maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
                  <ListView maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS" />
               </children>
            </VBox>
         </children>
      </HBox>
   </center>
</BorderPane>

关键调整说明

  • 移除冗余的AnchorPane和Pane,减少不必要的布局嵌套
  • 为VBox添加HBox.hgrow="ALWAYS",确保它能在HBox中占据所有可用宽度
  • 保留TextArea和ListView的VBox.vgrow="ALWAYS"与maxWidth="MAX_VALUE",保证它们随VBox同步调整尺寸

内容的提问来源于stack exchange,提问作者Thend

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 22:40:41