JavaFX:扩展 TextArea、Listview(Vgrow/Hgrow = 'ALWAYS' 不起作用)

The*_*end 0 java listview textarea javafx

我有一个简单的 JavaFX 应用程序,一个带有 AnchorPane 的 BorderLayout,在两个窗格中,一个包含一个 HBox 和一个带有 TextArea 和 ListView 的 VBox。当我增加窗口尺寸时,我希望它们的宽度也增加。我已经在控件上尝试了 Vgrow/Hgrow = 'ALWAYS' 和最大宽度/最大高度 = 'MAX_VALUE',但没有任何效果。-(我使用场景生成器创建 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="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" BorderPane.alignment="CENTER">
         <children>
            <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0" />
            <Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="200.0">
               <children>
                  <HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0">
                     <children>
                        <VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" HBox.hgrow="ALWAYS">
                           <children>
                              <TextArea maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" VBox.vgrow="ALWAYS" />
                              <ListView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" VBox.vgrow="ALWAYS" />
                           </children>
                        </VBox>
                     </children>
                  </HBox>
               </children>
            </Pane>
         </children>
      </AnchorPane>
   </center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

我的java应用程序类(HelloApplication):

package com.example.demo;

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

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:我删除了不必要的 AnchorPane 和其他窗格,只保留一个 VBox 并将其放置在另一个 BorderLayout 中,看一下,现在看起来很完美:)

在此输入图像描述

Jam*_*s_D 6

不要对尺寸进行硬编码,并且不要使用依赖于硬编码尺寸的布局窗格,例如Pane和 ,除非在极少数情况下。AnchorPane

(有时您可能需要将最大大小设置为Double.MAX_VALUE允许控件超出其首选大小,但据我所知,在本例中即使这样做也是不必要的。)

您的 FXML 有一个空Pane的且至少有两个冗余的包装器容器,它们仅包装一个节点(另一个节点PaneAnchorPane)。删除所有这些和硬编码值,它将起作用:

<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
    <center>
        <HBox>
            <children>
                <VBox HBox.hgrow="ALWAYS">
                    <children>
                        <TextArea VBox.vgrow="ALWAYS" />
                        <ListView VBox.vgrow="ALWAYS" />
                    </children>
                </VBox>
            </children>
        </HBox>
    </center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)

根据您的实际用例,您可能不需要HBox

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

<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.*?>
<BorderPane xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1">
    <center>
            <VBox>
                <children>
                    <TextArea VBox.vgrow="ALWAYS" />
                    <ListView VBox.vgrow="ALWAYS" />
                </children>
            </VBox>
    </center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)