【问题标题】:Auto-resizing a window based on the content根据内容自动调整窗口大小
【发布时间】:2021-03-13 11:30:37
【问题描述】:

鉴于以下 FXML:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.VBox?>


<VBox spacing="7.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TitledPane text="Title 1">
         <content>
            <TextArea minHeight="-Infinity" prefHeight="125.0" prefRowCount="1" wrapText="true" />
         </content>
      </TitledPane>
      <TitledPane expanded="false" text="Title 2">
         <content>
            <TextArea minHeight="-Infinity" prefHeight="125.0" prefRowCount="1" wrapText="true" />
         </content>
      </TitledPane>
      <TitledPane expanded="false" text="Title 3">
         <content>
            <TextArea minHeight="-Infinity" prefHeight="125.0" prefRowCount="1" wrapText="true" />
         </content>
      </TitledPane>
   </children>
   <padding>
      <Insets bottom="14.0" left="14.0" right="14.0" top="14.0" />
   </padding>
</VBox>

以及以下 Java 代码:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        final Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

有没有办法根据展开的TitledPanes 自动调整窗口大小?

【问题讨论】:

  • 监听 titledPanes 的状态并根据需要调整窗口大小.. 注意:从用户体验的角度来看,通常不建议频繁更改窗口大小(视觉噪音太大)
  • 一个想法是找到一些表示场景/根的大小变化的属性,监听它并调用stage.sizeToScene。找不到任何通用的东西(当/如果他们真的想要调整大小时,布局有自己的规则) - f.i. StackPane 将其内容的大小调整为 pref,因此它可以将真实内容包装到 stackPane 中,将其用作场景根并听取真实内容的大小变化.. 可能会丢失一些明显的东西,感觉很复杂;)
  • @kleopatr 如果我用ScrollPane 包裹TitledPanes 会更好。
  • 这能回答你的问题吗? JavaFX Stage/Scene automatic resize
  • Sedrick - 无需尝试:这是否也尊重 TitledPane 的动画?我觉得不会吧?其余的我必须同意@kleopatra 关于用户体验的三个问题......

标签: java javafx


【解决方案1】:

我想出了以下在 Kotlin 中一起破解的解决方案。请记住,这仅展示了您如何做到这一点 - 5 * 14 只是我摆弄着寻找合适的数量......

import javafx.application.Application
import javafx.geometry.Insets
import javafx.scene.Scene
import javafx.scene.control.TextArea
import javafx.scene.control.TitledPane
import javafx.scene.layout.VBox
import javafx.stage.Stage

fun main(args: Array<String>) {
    Application.launch(TestApp::class.java, *args)
}

class TestApp: Application() {
    override fun start(primaryStage: Stage) {
        val t1: TitledPane
        val t2: TitledPane
        val t3: TitledPane
        VBox().apply {
            fun TitledPane.creatTextArea() {
                content = TextArea().apply {
                    minHeight = 125.0
                    prefHeight = 125.0
                    prefRowCount = 1
                    isWrapText = true
                }
            }
            padding = Insets(14.0)
            children.addAll(
                TitledPane().apply {
                    t1 = this
                    text = "Title 1"
                    creatTextArea()
                },
                TitledPane().apply {
                    t2 = this
                    text = "Title 2"
                    isExpanded = false
                    creatTextArea()
                },
                TitledPane().apply {
                    t3 = this
                    text = "Title 3"
                    isExpanded = false
                    creatTextArea()
                }
            )
        }.let {
            Scene(it)
        }.let {
            primaryStage.apply {
                scene = it
                this.minHeightProperty().bind(this.maxHeightProperty())
                this.maxHeightProperty().bind(t1.heightProperty().add(t2.heightProperty()).add(t3.heightProperty()).add(5  * 14))
            }
        }.show()
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多