当窗口最大化时,如何自动调整JavaFX容器的大小?
基本上,我有以下文件:当窗口最大化时,如何自动调整JavaFX容器的大小?,javafx,javafx-8,fxml,scenebuilder,Javafx,Javafx 8,Fxml,Scenebuilder,基本上,我有以下文件: <?xml version="1.0" encoding="UTF-8"?> <?import com.jfoenix.controls.JFXButton?> <?import com.jfoenix.controls.JFXTabPane?> <?import com.jfoenix.controls.JFXTreeView?> <?import javafx.geometry.Insets?> <?
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTabPane?>
<?import com.jfoenix.controls.JFXTreeView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="566.0" prefWidth="753.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gui.controller">
<center>
<JFXTabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" BorderPane.alignment="CENTER">
<tabs>
<Tab text="TAB">
<content>
<AnchorPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="495.0" prefWidth="753.0">
<children>
<StackPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="150.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" nodeOrientation="LEFT_TO_RIGHT" prefHeight="100.0" prefWidth="200.0">
<children>
<JFXTreeView fx:id="treeView" />
<TableView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="277.0" prefWidth="753.0">
<columns>
<TableColumn maxWidth="-1.0" text="COL" />
<TableColumn text="COL" />
<TableColumn text="COL" />
<TableColumn text="COL" />
<TableColumn minWidth="0.0" text="COL" />
<TableColumn text="COL" />
</columns>
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
</TableView>
</children>
</HBox>
</children>
</StackPane>
</children>
</VBox>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</JFXTabPane>
</center>
</BorderPane>
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane parent = (BorderPane) FXMLLoader.load(getClass().getResource("document.fxml"));
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.show();
}
}
当我最大化窗口时,表和其他容器不会相应地调整大小。我试图在SceneBuilder中将所有容器的Max Width
和Max Height
设置为Max_VALUE
,但实际上什么也没发生是否有一种方法可以自动扩展容器,以便在窗口最大化时可以使用所有空间?这里是一个开始:一个将随场景增长的边框窗格:
public void start(Stage stage) {
BorderPane root = new BorderPane();
root.setStyle("-fx-background-color: yellow");
root.setCenter(new Text("center"));
root.setTop(new Text("top"));
root.setBottom(new Text("bottom"));
Scene scene = new Scene(root);
root.prefHeightProperty().bind(scene.heightProperty());
root.prefWidthProperty().bind(scene.widthProperty());
stage.setScene(scene);
stage.show();
}
在花了一些时间调查这个问题之后。纠正此问题的步骤有:
1-将所有锚定窗格约束设置为0(在VBox标记中)将允许子容器占用父容器提供的所有空间
<VBox AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
2-将TabeView的Hgrow属性设置为Always将允许表在找到空间时水平增长。3-对于其余的容器,保持默认设置,并将最大高度和最大宽度设置为使用计算大小即可。您可以添加一个复制该问题的容器吗?@ItachiUchiha done。发布的代码不是@C0,因为我从原始FXML文件中删除了所有不必要和不相关的部分(已删除菜单、项目和容器)。我只保留了重现问题所需的部分。我没有包括将加载FXML文件、设置场景并显示它的主类,因为这无助于理解问题和找到解决方案。这是不相关的,只会用不必要的代码使文章过度拥挤。它当然应该最小,但它也应该是可验证的:我们应该能够复制粘贴并运行它。我正在使用带有控制器的FXML文档编写UI,但我没有访问场景对象实例的权限。我知道我可以在加载FXML时插入引用(使用返回的控制器引用)但它看起来像是一个肮脏的黑客。有没有其他方法可以仅使用FXML实现它?我已经包括了FXML文档。