JavaFX Scene Builder在将组件添加到附加的AnchorPane时在运行时扩展ScrollPane区域

问题描述 投票:0回答:1

我无法找到有关此的信息,但是我已经在Scene Builder中创建了一个布局,并且已经将AnchorPane放置在一个空的ScrollPane中,并在行中添加了文本,滑块和标签,然后添加了一个按钮,供用户添加上面的新条目。

基本上是典型的偏好诱导UI,用户还可以在其中添加新条目并指定自己的偏好值。

[为了测试目的,按下该按钮会创建一个新标签,将其添加到AnchorPane,然后将其重新定位到AnchorPane之外的Y位置,然后调整AnchorPane的大小,以便包括新标签。我遇到的问题是ScrollPane无法调整和扩展可滚动区域以适应新的AnchorPane高度,因此我无法向下滚动到新标签可见的位置。另一方面,在Scene Builder中,如果我调整AnchorPane的大小以使其大于ScrollPane,则后者会动态调整可滚动区域,因此我不确定问题出在哪里。

感谢您的任何帮助。

编辑:根据要求,下面是该项目的最低可复制版本。

加载FXML并创建场景的类

package main;
import java.io.IOException;

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

public class Registration_Page extends Application {
    private Stage primaryStage;
    private TitledPane mainLayout;

    @Override
    public void start(Stage primaryStage) throws IOException {
        this.primaryStage = primaryStage;
        showMainView();
    }

    private void showMainView() throws IOException {
        FXMLLoader loader = new FXMLLoader(Registration_Page.class.getResource("resources/Registration_Page.fxml"));
        mainLayout = loader.load();
        Scene scene = new Scene(mainLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

}

使用FXML定义的组件充当按钮控制器的类。

package main;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;


public class ButtonController { 

    @FXML
    private Button plus;    

    @FXML
    private AnchorPane prefValuesAnchorPane;

    @FXML
    private ScrollPane scrollPane;

    @FXML
    protected void plusAction(ActionEvent event) {
        Label lbl1 = new Label("Hello");
        prefValuesAnchorPane.getChildren().add(lbl1);
        lbl1.relocate(18, 250);
        System.out.println(prefValuesAnchorPane.getHeight());
        System.out.println(lbl1.getLayoutY());
        if (lbl1.getLayoutY() >= prefValuesAnchorPane.getHeight())
        {
            prefValuesAnchorPane.resize(prefValuesAnchorPane.getWidth(), lbl1.getLayoutY() + 3);

        }
    }
}

FXML文档

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="User Registration" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.ButtonController">
  <content>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
         <children>
            <TextField layoutX="52.0" layoutY="79.0" />
            <Text layoutX="14.0" layoutY="96.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Name" />
            <Text layoutX="14.0" layoutY="130.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Preference Values">
               <font>
                  <Font size="14.0" />
               </font>
            </Text>
            <ScrollPane fx:id="scrollPane" layoutX="14.0" layoutY="137.0" prefHeight="223.0" prefWidth="332.0">
               <content>
                  <AnchorPane fx:id="prefValuesAnchorPane" minHeight="0.0" minWidth="0.0" prefHeight="217.0" prefWidth="329.0">
                     <children>
                        <Text layoutX="15.0" layoutY="30.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Val1:" />
                        <Slider blockIncrement="1.0" layoutX="115.0" layoutY="15.0" majorTickUnit="1.0" max="10.0" minorTickCount="0" prefWidth="140.0" showTickLabels="true" snapToPixel="false" snapToTicks="true" />
                        <Label layoutX="280.0" layoutY="12.0" text="0">
                           <font>
                              <Font name="System Bold" size="14.0" />
                           </font>
                        </Label>
                        <Text layoutX="15.0" layoutY="62.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Val2:" />
                        <Slider blockIncrement="1.0" layoutX="115.0" layoutY="50.0" majorTickUnit="1.0" max="10.0" minorTickCount="0" prefWidth="140.0" showTickLabels="true" snapToTicks="true" />
                        <Label layoutX="280.0" layoutY="48.0" text="0">
                           <font>
                              <Font name="System Bold" size="14.0" />
                           </font>
                        </Label>
                        <Text layoutX="15.0" layoutY="97.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Val3:" />
                        <Slider blockIncrement="1.0" layoutX="115.0" layoutY="85.0" majorTickUnit="1.0" max="10.0" minorTickCount="0" prefWidth="140.0" showTickLabels="true" snapToTicks="true" />
                        <Label layoutX="280.0" layoutY="82.0" text="0">
                           <font>
                              <Font name="System Bold" size="14.0" />
                           </font>
                        </Label>
                        <Text layoutX="15.0" layoutY="133.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Val4:" />
                        <Slider blockIncrement="1.0" layoutX="115.0" layoutY="120.0" majorTickUnit="1.0" max="10.0" minorTickCount="0" prefWidth="140.0" showTickLabels="true" snapToTicks="true" />
                        <Label layoutX="280.0" layoutY="118.0" text="0">
                           <font>
                              <Font name="System Bold" size="14.0" />
                           </font>
                        </Label>
                        <Button fx:id="plus" graphicTextGap="1.0" layoutX="289.0" layoutY="153.0" maxHeight="-Infinity" maxWidth="-Infinity" mnemonicParsing="false" onAction="#plusAction" prefHeight="0.0" prefWidth="30.0" text="+" textAlignment="CENTER" textOverrun="CENTER_ELLIPSIS" AnchorPane.topAnchor="153.0">
                           <font>
                              <Font name="System Bold" size="14.0" />
                           </font>
                        </Button>
                     </children>
                  </AnchorPane>
               </content>
            </ScrollPane>
            <Button layoutX="532.0" layoutY="335.0" mnemonicParsing="false" text="Next" />
         </children></AnchorPane>
  </content>
</TitledPane>
user-interface javafx scenebuilder
1个回答
0
投票

解决方案是改为使用一个包含AnchorPane的ScrollPane(原样),并为每个添加的条目创建一个AnchorPane或BorderPane或任何其他容器窗格(afaik),然后对其添加任何内容,而不是添加组件,例如按钮或标签,直接粘贴到与SP相连的原始AP上。

© www.soinside.com 2019 - 2024. All rights reserved.