在使用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>


ContainerController.java代码:

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


更多相关文章

  1. java 画图注意 改变窗口大小不会消失
  2. Java XML - 具有相同名称的嵌套元素
  3. Javascript 无提示框关闭IE窗口

随机推荐

  1. mysql恢复报ERROR 2006 (HY000) at line
  2. Linux Centos 下使用yum 命令安装mysql
  3. 从具有外部约束的多个表中删除
  4. Mysql异常_01_ 誓死登进mysql_Can&#39;t
  5. qt连接mysql运行时出现QSqlQuery::exec:d
  6. 【Qt】Qt5.12连接MySQl5.7(亲自测试成功)
  7. MySQL DELETE语句和TRUNCATE TABLE语句的
  8. 计算mysql中每天的发生次数
  9. 为获得快速结果,我的查询有什么好的索引?
  10. 读《SQL优化核心思想》:你不知道的优化技