Initializable接口,JavaFX加载fxml文件的控件,

2023-12-20 10:01

本文主要是介绍Initializable接口,JavaFX加载fxml文件的控件,,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JavaFX的窗体文件是fxml格式的xml文件。JavaFX通过FXMLLoader加载fxml文件,并将其进行显示。

fxml文件:

<?xml version="1.0" encoding="UTF-8"?><!--Copyright (c) 2015, 2019, Gluon and/or its affiliates.All rights reserved. Use is subject to license terms.This file is available and licensed under the following license:Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer inthe documentation and/or other materials provided with the distribution.- Neither the name of Oracle Corporation nor the names of itscontributors may be used to endorse or promote products derivedfrom this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTOWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--><?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.web.WebView?><VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1" fx:controller="form.MainFrameController"><children><MenuBar VBox.vgrow="NEVER"><menus><Menu mnemonicParsing="false" text="File"><items><MenuItem mnemonicParsing="false" text="New" /><MenuItem fx:id="openFileItem" mnemonicParsing="false" text="Open…" /><Menu mnemonicParsing="false" text="Open Recent" /><SeparatorMenuItem mnemonicParsing="false" /><MenuItem mnemonicParsing="false" text="Close" /><MenuItem mnemonicParsing="false" text="Save" /><MenuItem mnemonicParsing="false" text="Save As…" /><MenuItem mnemonicParsing="false" text="Revert" /><SeparatorMenuItem mnemonicParsing="false" /><MenuItem mnemonicParsing="false" text="Preferences…" /><SeparatorMenuItem mnemonicParsing="false" /><MenuItem fx:id="quitItem" mnemonicParsing="false" text="Quit" /></items></Menu><Menu mnemonicParsing="false" text="Edit"><items><MenuItem mnemonicParsing="false" text="Undo" /><MenuItem mnemonicParsing="false" text="Redo" /><SeparatorMenuItem mnemonicParsing="false" /><MenuItem mnemonicParsing="false" text="Cut" /><MenuItem mnemonicParsing="false" text="Copy" /><MenuItem mnemonicParsing="false" text="Paste" /><MenuItem mnemonicParsing="false" text="Delete" /><SeparatorMenuItem mnemonicParsing="false" /><MenuItem mnemonicParsing="false" text="Select All" /><MenuItem mnemonicParsing="false" text="Unselect All" /></items></Menu><Menu mnemonicParsing="false" text="Help"><items><MenuItem mnemonicParsing="false" text="About MyHelloApp" /></items></Menu></menus></MenuBar><TabPane prefHeight="375.0" prefWidth="642.0" tabClosingPolicy="UNAVAILABLE" VBox.vgrow="ALWAYS"><tabs><Tab fx:id="trackDataViewTab" text="雷达数据" /><Tab fx:id="trackDataMapTab" text="雷达数据地图"><content><BorderPane prefHeight="200.0" prefWidth="200.0"><center><WebView fx:id="trackMapWebView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" /></center><left><VBox prefHeight="200.0" prefWidth="150.0" spacing="2.0" BorderPane.alignment="CENTER"><children><Label text="最底高度范围:0~15000米" /><Spinner fx:id="minAltitudeSpinner" editable="true" /><Label text="最高高度范围:0~15000米" /><Spinner fx:id="maxAltitudeSpinner" editable="true" /><Button mnemonicParsing="false" prefHeight="23.0" prefWidth="150.0" text="高 度 过 滤"><font><Font size="16.0" /></font></Button><Separator orientation="VERTICAL" prefHeight="200.0" /></children></VBox></left></BorderPane></content></Tab></tabs></TabPane></children>
</VBox>

绑定的的Java文件:

package form;import java.net.URL;
import java.util.ResourceBundle;import handle.MainFrameHandle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Spinner;
import javafx.scene.control.Tab;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;/*** @copyright 2003-2023* @package   form* @file      MainFrameController.java* @date      2023-12-16* @author    qiao wei* @version   1.0* @brief     MainWindow.fxml文件对应的Java类。* @history*/
public class MainFrameController implements Initializable {public MainFrameController() {mainFrameHandle = MainFrameHandle.getInstance();}/*** @class   MainFrameController* @date    2023-12-18* @author  qiao wei* @version 1.0* @brief   初始化,实现Initializable接口。MainFrameController.java与MainWindow.fxml绑定,当fxml文件被FXMLLoader*          加载(加载fxml文件时,绑定的控件已经进行初始化)后就自动调用该方法,对已经创建的控件可以进行二次设置。* @param   * @return  * @throws*/@Overridepublic void initialize(URL url, ResourceBundle resourceBundle) {setControlProperty();}private void setControlProperty() {openFileItem.setOnAction(event -> mainFrameHandle.chooseFiles());quitItem.setOnAction(event -> mainFrameHandle.exitApplication(event));WebEngine webEngine = trackMapWebView.getEngine();webEngine.load("https://toutiao.com/");//        StackPane stackPane = new StackPane();
//        stackPane.getChildren().addAll(webView);
//
//        HBox hBox = new HBox();
//        HBox.setHgrow(webView, Priority.ALWAYS);
//        VBox.setVgrow(webView, Priority.ALWAYS);
//        hBox.getChildren().addAll(webView);//        trackDataMapTab.setContent(hBox);}@FXMLprivate Tab trackDataViewTab;@FXMLprivate Tab trackDataMapTab;@FXMLprivate MenuItem openFileItem;@FXMLprivate MenuItem quitItem;@FXMLprivate Spinner<Integer> minAltitudeSpinner;@FXMLprivate Spinner<Integer> maxAltitudeSpinner;@FXMLprivate WebView trackMapWebView;private MainFrameHandle mainFrameHandle;
}

Initializable接口简介:

Controller initialization interface.

NOTE This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible.

Since:

fxml文件中的JavaFX控件在Java文件中绑定,在Java类的构造函数中还没有初始化,所有的控件还都是null,但是Java类继承了Initializable接口,则在构造函数之后自动调用Initializable接口的方法,此时JavaFX控件已经初始化,可以进行设置了。

这篇关于Initializable接口,JavaFX加载fxml文件的控件,的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/515614

相关文章

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构