JavaFX窗口自适应
16lz
2021-01-22
在使用JavaFX开发PC客户端的时候,会遇到一个问题,当主界面窗口拉伸变大之后,希望子窗口能够自适应的变化,下面给出解决方案。
假设主窗口的界面文件是MainUI.fxml,对应的是MainUIController.java,在MainUI之上再嵌套一层,嵌套的界面文件定义为Container.fxml,对应的是ContainerController.java,就可以达到效果:
Container.fxml布局文件
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import javafx.scene.layout.*?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.StackPane?> <AnchorPane fx:id="rootPane" minHeight="600.0" minWidth="800.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.nii.desktop.controller.ContainerController"> <children> <StackPane fx:id="containerPane" prefHeight="594.0" prefWidth="868.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/> </children> </AnchorPane>
package com.nii.desktop.controller; import com.nii.desktop.util.ui.ResourceLoader; import com.nii.desktop.util.ui.UIManager; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.control.SplitPane; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; import javafx.scene.layout.StackPane; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; /** * Created by wzj on 2018/2/7. */ public class ContainerController implements Initializable { /** * 日志 */ private final static Logger LOGGER = LoggerFactory.getLogger(ContainerController.class); /** * container pane */ @FXML private StackPane containerPane; /** * Called to initialize a controller after its root element has been * completely processed. * * @param location The location used to resolve relative paths for the root object, or * <tt>null</tt> if the location is not known. * @param resources The resources used to localize the root object, or <tt>null</tt> if */ @Override public void initialize(URL location, ResourceBundle resources) { SplitPane pane = null; try { pane = FXMLLoader.load(ResourceLoader.getFxmlResource("MainUI.fxml")); } catch (IOException e) { LOGGER.error("Switch main UI failed",e); } containerPane.getChildren().addAll(pane); } }
测试窗口代码:
package com.nii.desktop.test; import com.nii.desktop.util.ui.ResourceLoader; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import java.io.IOException; /** * Created by wzj on 2018/2/7. */ public class AutoSizeApplication extends Application { @Override public void start(Stage primaryStage) throws Exception { AnchorPane pane = null; try { pane = FXMLLoader.load(ResourceLoader.getFxmlResource("Container.fxml")); } catch (IOException e) { e.printStackTrace(); } // 将pane加入到Scen中 Scene scene = new Scene(pane, 500, 500); // 设置stage的scen,然后显示我们的stage primaryStage.setScene(scene); primaryStage.setTitle("Hello World"); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
把MainUI的边框设置为红色,启动之后,会发现,随着主窗口的变动,MainUI的大小也在随着改变。
代码下载链接:https://github.com/HelloKittyNII/Code/tree/master/client/desktop/src/main/java/com/nii/desktop/test