Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据内容自动调整窗口大小_Java_Javafx - Fatal编程技术网

Java 根据内容自动调整窗口大小

Java 根据内容自动调整窗口大小,java,javafx,Java,Javafx,给定以下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?&

给定以下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>

有没有办法根据扩展的标题窗格的数量自动调整窗口大小?

我在Kotlin提出了以下解决方案。请记住,这只是展示了你如何做到这一点-我只是在摆弄
5*14
以找到合适的数量

导入javafx.application.application
导入javafx.geometry.Insets
导入javafx.scene.scene
导入javafx.scene.control.TextArea
导入javafx.scene.control.TitledPane
导入javafx.scene.layout.VBox
导入javafx.stage.stage
趣味主线(args:Array){
launch(TestApp::class.java,*args)
}
类TestApp:Application(){
覆盖乐趣开始(初级阶段:阶段){
val t1:标题窗格
val t2:标题窗格
val t3:标题窗格
VBox().apply{
有趣的标题窗格。creatTextArea(){
content=TextArea()。应用{
最小高度=125.0
高度=125.0
预行计数=1
isWrapText=true
}
}
填充=插图(14.0)
孩子们(
TitledPane().apply{
t1=这个
text=“标题1”
创建文本区域()
},
TitledPane().apply{
t2=这个
text=“标题2”
isExpanded=false
创建文本区域()
},
TitledPane().apply{
t3=这个
text=“标题3”
isExpanded=false
创建文本区域()
}
)
}.让{
场景(it)
}.让{
初级阶段{
场景=它
this.minHeightProperty().bind(this.maxHeightProperty())
this.maxHeightProperty().bind(t1.heightProperty().add(t2.heightProperty()).add(t3.heightProperty()).add(5*14))
}
}.show()
}
}

倾听标题窗格的状态,并根据需要调整窗口大小。。注意:从用户体验的角度来看,通常不建议频繁更改窗口大小(太多的视觉噪音)。一个想法是找到一些表示场景/根的大小更改的属性,听它并调用stage.sizeToScene。找不到任何常规内容(布局在需要调整大小时有自己的规则)-f.i.StackPane将其内容调整为pref,因此可以将真实内容包装到StackPane中,将其用作场景根,并侦听真实内容的大小更改。。可能遗漏了一些明显的东西,感觉很复杂;)@kleopatr如果我用
滚动窗格
包装
标题窗格
s,可能会更好。这是否回答了您的问题?Sedrick-不尝试:这是否也尊重标题窗格的动画?我想不会吧?剩下的我必须同意@kleopatra关于用户体验的三个问题。。。
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);
    }
}