JavaFX 加载 fxml 文件

这篇具有很好参考价值的文章主要介绍了JavaFX 加载 fxml 文件。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

20231230更新:Java文件在加载FXML文件时,注意文件路径。

JavaFX 加载 fxml 文件主要有两种方式,第一种方式通过 FXMLLoader 类直接加载 fxml 文件,简单直接,但是有些控件目前还不知道该如何获取,所以只能显示,目前无法处理。第二种方式较为复杂,但是可以使用与 fxml 文件对应的 ***Controller 类可以操作 fxml 文件中的所有控件。现将两种方式介绍如下:

方式一:

  1. 创建 fxml 的 UI 文件
  2. 将 fxml 文件放置到对应位置
  3. FXMLLoader 加载文件显示

fxml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button fx:id="ownedNone" alignment="CENTER" mnemonicParsing="false" text="Owned None" />
      <Button fx:id="nonownedNone" mnemonicParsing="false" text="Non-owned None" />
      <Button fx:id="ownedWindowModal" mnemonicParsing="false" text="Owned Window Modal" />
      <Button mnemonicParsing="false" text="Button" />
      <Button mnemonicParsing="false" text="Button" />
   </children>
</VBox>

UI 显示:

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx

java加载 fxml 文件:

package ch04;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Cursor;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;

/**
 * @author qiaowei
 * @version 1.0
 * @package ch04
 * @date 2020/5/24
 * @description
 */
public class LoadFXMLTest extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage)  {
        try {
            useFXMLLoader(primaryStage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    /**
      @class      LoadFXMLTest
      @date       2020/5/24
      @author     qiaowei
      @version    1.0
      @brief      使用FXMLLoader加载fxml文件并生成相应的java类
      @param      primaryStage Application创建的Stage实例
     */
    public void useFXMLLoader(Stage primaryStage) throws Exception {
        Parent root =
                FXMLLoader.load(getClass().getResource("/sources/StageModalityWindow.fxml"));

        // 根据控件在窗体控件的添加顺序编号获取
        // Button ownedNone = (Button) ((VBox) root).getChildren().get(0);
        Button ownedNone = (Button) root.lookup("#ownedNone");
        ownedNone.setOnAction(e -> resetWindowTitle(primaryStage, "hello ownedNone"));

        Button ownedWindowModal = (Button) root.lookup("#ownedWindowModal");
        ownedWindowModal.setOnAction(e -> resetWindowTitle(primaryStage, "ownedWindowModal"));

        int width = ((int) ((VBox) root).getPrefWidth());
        int height = ((int) ((VBox) root).getPrefHeight());
//
        Scene scene = new Scene(root, width, height);
//        Scene scene = new Scene(root);
//        Button ownedNoneButton = ((VBox) root).getChildren().get(1);

        primaryStage.setTitle("StageModality");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    /**
     @class        StageModalityApp
     @date         2020/3/6
     @author       qiaowei
     @version      1.0
     @description  根据窗口拥有者和模式设置窗口状态
     @param        owner 窗口的父控件
     @param        modality 窗口的模式
     */
    private void showDialog(Window owner, Modality modality) {
        // Create a new stage with specified owner and modality
        Stage stage = new Stage();
        stage.initOwner(owner);
        stage.initModality(modality);
        Label modalityLabel = new Label(modality.toString());
        Button closeButton = new Button("Close");
        closeButton.setOnAction(e -> stage.close());

        VBox root = new VBox();
        root.getChildren().addAll(modalityLabel, closeButton);
        Scene scene = new Scene(root, 200, 100);

        // 设置鼠标在scene的显示模式
        scene.setCursor(Cursor.HAND);

        stage.setScene(scene);
        stage.setTitle("A Dialog Box");
        stage.show();
    }

    private void resetWindowTitle(Stage stage, String title) {
        stage.setTitle(title);
    }
}

根据 fxml 中控件的 “fx:id” 属性获取控件,并添加触发事件。通过 button 修改窗体的 title

注意两点:

  1. fxml 的存放路径,不同位置加载 String 不同。
  2. fxml 加载后返回的是 root 实例,需要放置到 sence 实例中再显示。
  3. 在通过 fxml 中控件的 id 返回控件时,id 前要加 #字符。

第二种方式

  1. 创建 fxml 文件,在 fxml 文件的顶层控件设置 “fx:controller”,属性值 =“完整的包路径.***Controller”

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx

在完整的包路径下创建 ***Controller 类,在 fxml 文件中定义的控件和方法前都加 “@FXML”,注意两个文件中的对应控件、方法名称必须保持一致。

注意:***Controller 类在这里只继承 Objec,这样其中的控件都自动绑定到 fxml 中的控件,不会出现控件为 null 的情况。退出程序按钮。不添加 "@FXML" 注解,系统认为时类自己添加的控件,不会与 fxml 文件中同名控件绑定,系统不会自动初始化,值为 null;添加 "@FXML" 注解,系统会自动绑定到 fxml 文件中的同名控件,会自动给初始化为 MenuItem 的实例。注意字段与方法的区别,如果在 fxml 中定义方法,在 java 文件中必须有同名的方法,而且方法前要加 "@FXML" 注释。

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx

示例如下:创建一个有 menuBar、menuItem 的窗体,在 fxml 中定义 menuItem 的 id 和 Action,在 ***Controller 中操作控件 menuItem 和 Action。

fmxl 文件,其中 openItem 没有设置 onAction,closeItem 设置 onAction,注意之后的两种处理方式。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="400.0" 
            prefWidth="600.0" 
            xmlns="http://javafx.com/javafx/10.0.2-internal" 
            xmlns:fx="http://javafx.com/fxml/1" 
            fx:controller="ch06.VHFFrameController">
   <children>
      <MenuBar fx:id="menuBar" layoutY="2.0" AnchorPane.topAnchor="2.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
                <MenuItem fx:id="openItem" mnemonicParsing="false" text="Open" />
                <MenuItem fx:id="closeItem" mnemonicParsing="false" onAction="#exitApp" text="Exit" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <Separator prefWidth="200.0" />
   </children>
</AnchorPane>

 对应的 ***Controller 类,openItem 通过在 setStage 方法中绑定 setOnAction(不能在构造函数中进行绑定,只能通过其它方法绑定!!!),closeItem 通过 fxml 文件中的设置直接绑定了 exitApp 方法(注:两个 MenuItem 控件通过不同的方法进行动作绑定,一个在 fxml 文件中绑定,一个在类文件中绑定)

注意:fxml 文件中设置的控件、方法在 ***Controller 类中要通过 “@FXML” 标识方法、字段在绑定,且方法、字段名称完全一致。

package ch06;

import javafx.fxml.FXML;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;

/**
  @package      ch06 
  @file         VHFFrameController.java
  @date         2020/5/27
  @author       qiaowei
  @version      1.0
  @description  与VHFFFrame.fxml文件对应的Controller类
 */
public class VHFFrameController {
    
    public VHFFrameController() {
        operator = Operator.instance();
    }
    
    /**
      @class      VHFFrameController
      @date       2020/5/27
      @author     qiaowei
      @version    1.0
      @brief      打开文件选择窗口
     */
//    @FXML
    public void openChooser() {
        if (isStage()) {
            operator.openFile(primaryStage);       
        }
    }
    
    /**
      @class      VHFFrameController
      @date       2020/5/27
      @author     qiaowei
      @version    1.0
      @brief      设置primaryStage字段实例,当字段已经设置过,跳过设置步骤
      @param      primaryStage 从主程序传来的主primaryStage实例
     */
    public void setStage(Stage primaryStage) {
        if ( !isStage()) {
            this.primaryStage = primaryStage;
        }

        openItem.setOnAction(e -> openChooser());
    }
    
    /**
      @class      VHFFrameController
      @date       2020/5/27
      @author     qiaowei
      @version    1.0
      @brief      退出程序
     */
    @FXML
    private void exitApp() {
        operator.exitApp();
    }
    
    /**
      @class      VHFFrameController
      @date       2020/5/27
      @author     qiaowei
      @version    1.0
      @brief      判断primaryStage实例是否为null,为null时,返回false;反之返回true;
      @return     true primaryStage不为null;反之返回false
     */
    private boolean isStage() {
        boolean flag = false;
        if (null != primaryStage) {
            flag = true;
        } else {
            flag = false;
        }
        
        return flag;
    }
    
    @FXML
    private MenuItem openItem;
    
    @FXML
    private MenuItem closeItem;
    
    @FXML
    private MenuBar menuBar;

    private static Operator operator;
    
    private Stage primaryStage;
}

 具体操作类 Operator,实现退出程序,打开 FileChooser 窗体两个功能:

package ch06;

import javafx.application.Platform;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;

/**
 * @author qiaowei
 * @version 1.0
 * @package ch06
 * @date 2020/5/27
 * @description
 */
public class Operator {
    
    private Operator() {
        
    }
    
    public static Operator instance() {
        if (null == OPERATOR) {
            OPERATOR = new Operator();
        }
        
        return OPERATOR;
    }
    
    public void openFile(Stage stage) {
        FileChooser chooser = new FileChooser();

        File file = chooser.showOpenDialog(stage);
    }
    
    public void exitApp() {
        Platform.exit();
    }
    
    private static Operator OPERATOR = null;
}

JavaFX 运行类:

package ch06;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @author qiaowei
 * @version 1.0
 * @package ch06
 * @date 2020/5/27
 * @description
 */
public class VHFApp extends Application {

    public static void main(String[] args) {
        try {
            Application.launch(VHFApp.class, args);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader fxmlLoader = 
                new FXMLLoader(getClass().getResource("/sources/VHFFrame.fxml"));
        Parent root = fxmlLoader.load();
//        Parent root = 
//                FXMLLoader.load(getClass().getResource("/sources/VHFFrame.fxml"));

        int width = ((int) ((AnchorPane) root).getPrefWidth());
        int height = ((int) ((AnchorPane) root).getPrefHeight());

        Scene scene = new Scene(root, width, height);

        primaryStage.setTitle("VHFApplication");
        primaryStage.setScene(scene);
        
        VHFFrameController controller = fxmlLoader.getController();
        controller.setStage(primaryStage);

        primaryStage.show();
    }
    
//    private static VHFFrameController controller = new VHFFrameController();
}

实现如下:

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx

示例 2:

文件树图:

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx

 fxml 文件:指定对应的 Controller 文件,对控件 exitItem 制定了对应的方法 exitApplication。

<?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 without
  modification, are permitted provided that the following conditions
  are met:

  - Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the distribution.
  - Neither the name of Oracle Corporation nor the names of its
    contributors may be used to endorse or promote products derived
    from 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 NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.MainWindowController">
  <children>
    <MenuBar VBox.vgrow="NEVER">
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="New" />
            <MenuItem fx:id="openItem" 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="exitItem" mnemonicParsing="false" onAction="#exitApplication" 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>
    <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
         <children>
            <TextArea layoutX="178.0" layoutY="73.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
         </children>
    </AnchorPane>
  </children>
</VBox>

对应的 Controller:在退出程序过程中,对 openItem 按钮是否为 null 进行测试,有 @FXML 注解时,openItem 被自动绑定实例,不为 null;当注释调 @FXML 注解后,openItem 没有自动绑定实例,为 null。如果要在 Controller 类中对控件进行操作,可以实现 initialize 方法和 @FXML 注解。这样保证控件已经绑定实例,可以对比 initialize 和 setupAttributes 方法,两者中 openItem 控件的情况。FXMLLoader 首先调用默认构造函数,然后调用 initialize 方法,从而创建相应控制器的实例。首先调用构造函数,然后填充所有 @FXML 带注释的字段,最后调用 initialize ()。因此,构造函数无法访问引用在 fxml 文件中定义的组件的 @FXML 注解字段,而 initialize 方法可以访问这些注解字段。

package ui;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;

import java.net.URL;
import java.util.ResourceBundle;


/**************************************************************************************************
 * @copyright 2003-2022
 * @package   ui
 * @file      MainWindowController.java
 * @date      2022/5/2 22:54
 * @author    qiao wei
 * @version   1.0
 * @brief     MainWindow.fxml的绑定类。
 * @history
**************************************************************************************************/
public class MainWindowController {
    
    public MainWindowController() {
        setupAttributes();
    }

    @FXML
    public void initialize() {
        if (null != openItem) {
            openItem.setOnAction(e -> exitApplication());
        }
    }

    public void setupAttributes() {
        if (null != openItem) {
            openItem.setOnAction(e -> openFile());
        }
        
    }
    
    private void openFile() {
        
    }
    
    @FXML
    private void exitApplication() {
        if (null != openItem) {
            System.out.println("测试@FXML注释作用");
        }
        
        Platform.exit();
    }
    
    @FXML
    private MenuItem openItem;
    
    /**********************************************************************************************
     * @date   2022/5/2 22:48
     * @author qiao wei
     * @brief  退出程序按钮。不添加"@FXML"注解,系统认为时类自己添加的控件,不会与fxml文件中同名控件绑定,系统
     *         不会自动初始化,值为null;添加"@FXML"注解,系统会自动绑定到fxml文件中的同名控件,会自动给初始化
     *         为MenuItem的实例。注意字段与方法的区别,如果在fxml中定义方法,在java文件中必须有同名的方法,而且
     *         方法前要加"@FXML"注释。
    **********************************************************************************************/
    @FXML
    private MenuItem exitItem;
}

启动类:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import ui.MainWindowController;


/**************************************************************************************************
 @Copyright 2003-2022
 @package PACKAGE_NAME
 @date 2022/5/2
 @author qiao wei
 @version 1.0
 @brief TODO
 @history
 *************************************************************************************************/
public class Main extends Application {

    public static void main(String[] args) {
        try {
            Application.launch(Main.class, args);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader fxmlLoader =
                new FXMLLoader(getClass().getResource("/fxml/MainWindow.fxml"));

        Parent root = fxmlLoader.load();
//        MainWindowController controller = fxmlLoader.getController();

        Rectangle2D rectangle2D = Screen.getPrimary().getBounds();
        
        // 获取窗体左上角初始坐标
        double xPosition = rectangle2D.getWidth() / 5;
        double yPosition = rectangle2D.getHeight() / 5;

        // 获取窗体尺寸
        int width = (int) rectangle2D.getWidth() * 2 / 3;
        int height = (int) rectangle2D.getHeight() * 2 / 3;

        // 设置窗体起始坐标、尺寸
        primaryStage.setScene(new Scene(root, width, height));
        primaryStage.setX(xPosition);
        primaryStage.setY(yPosition);
        primaryStage.setTitle("Radar Track Application");

        primaryStage.show();
    }
}

在这里注意。FXMLLoader加载的文件路径是“/fxml/MainWindow.fxml”,文件是从从工程的根目录加载,在这里IDEA的根目录是src。

运行结果:

JavaFX 加载 fxml 文件,JavaFX,Java,java,开发语言,javafx文章来源地址https://www.toymoban.com/news/detail-683919.html

到了这里,关于JavaFX 加载 fxml 文件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Java 17 中找不到 JavaFX 的解决方法

    最近在学习 Paul Deitel 和 Harvey Deitel 编写的 Java 编程入门教材《Java: How to Program - Early Objects 11th Edition》 本书豆瓣页面链接 其中第 1 章第 1.10 节给出一个简易画板示例程序,是一个 JavaFX GUI 程序 其目录结构如下 按书上给的命令 javac *.java 编译时出现如下错误,编译失败 以上错

    2024年02月09日
    浏览(42)
  • 详解Java实现2048小游戏(JavaFX,完整源码+注释说明)

    刚刚完成的一个小游戏,写文章记录一下,如果有任何错误或者可以改进的代码请提出 另一方面也是方便自己几个月或几年后忘记时,来这里翻一翻回顾思路 目录 基本界面: 类的组织: _CardPane: _CardMatrixPane: _CardColor: _GameMenuBar: _2048Demo: 基本思路: 卡片: 卡片矩阵:

    2024年02月03日
    浏览(40)
  • GraalVM详细安装及打包springboot、java、javafx使用教程(环境安装篇)

    下一篇:GraalVM详细安装及打包springboot、java、javafx使用教程(打包普通JAVA项目篇) GraalVM是一款由Oracle公司开发的一款具有高效性能、降低基础设施成本、支持Java发展、与其他编程语言无缝集成、创建本机镜像等优点的跨平台虚拟机。它支持多种编程语言,如Java、JavaScript、Rub

    2024年04月26日
    浏览(29)
  • 使用javafx实现的基于udp、tcp的聊天GUI应用,可用作Java大作业、计网课程设计+视频演示

    点击这里下载源码 项目使用javafx作为ui开发平台,基于udp和tcp实现聊天功能。 支持同一网络环境内多设备聊天(测试了多台windows、mac设备通过有线和无线连接到一个局域网的情况)。 通信的流程可以简单描述为: 每个用户登录时会生成一个带uuid的用户信息 使用udp向局域网

    2024年02月04日
    浏览(35)
  • SpringBoot整合JavaFX进行桌面应用开发

    目录 一、创建一个spring boot工程 二、导入JavaFX依赖 三、创建fxml文件以及controller类文件 3.1 创建一个fxml文件 3.2 创建对应的controller文件 四、Scene Builder的使用 4.1 下载安装 4.2 运行 4.3 界面编辑 五、 修改主启动类 六、测试运行 七、Jfoenix 学习参考: 第一步,我们直接使用Sp

    2024年02月06日
    浏览(36)
  • 【JavaFx】1.JavaFx实现闹钟小程序

    专栏目录 1.JavaFx实现闹钟小程序 2.银行账户管理员 3.大数字 4.购物车 5.文本编辑器 6.乌龟图 JavaFx菜鸟教程 JavaFx哔哩哔哩教程 JavaFx是什么 JavaFx是java实现图形界面的一种方式,其他还有java的awt、swing,但是逐渐被淘汰。 awt -- swing -- JavaFx javafx可以实现逻辑和样式的分离,可以

    2024年02月20日
    浏览(38)
  • JavaFX环境配置(配jdk版本和javafx版本对应)

    因为jdk11之后就没有内置javafx了所以要自行下载配置到lib包 下载地址:JavaFX - Gluon (gluonhq.com)(要记住下载的路径)     方法1.在idea中新建JavaFx项目 创建好后发现爆红,是因为没在lib中导入javafx的jar包   找到对应路径选中其中的这几项然后导入即可,如果还有爆红则可能还需

    2024年02月04日
    浏览(30)
  • 安装部署JavaFX和IDEA添加JavaFX的详细步骤

    JavaFX是Java平台的一个图形用户界面(GUI)工具包,用于创建丰富、交互式和可视化的应用程序。 JavaFX提供了一整套各种UI组件、图形绘制、动画效果、布局管理和事件处理等功能,使开发人员能够轻松地构建具有吸引力和易用性的用户界面。 JavaFX具有以下特点: 跨平台性:

    2024年02月11日
    浏览(27)
  • javafx应用程序线程异常Exception in thread “JavaFx Application Thread“

    前几天用javafx做小桌面应用程序出现了一个问题: 反复检查,最终确定报错的原因是UI刷新频率过快导致的 javafx提供了Platform.runLater用于解决该问题:  总结:需要高频调用方法使用Platform.runLater

    2024年02月10日
    浏览(41)
  • javafx各种控件

    (1)基础 RadioButton Controls经常以组的形式出现,它可以让用户进行选择。 radiobutton控件在radiobutton类中,需要导 javafx.scene.control 包。 注意,我们在RadioButton旁边显示的字符串作为参数传递给RadioButton 类的构造函数的参数。你可以选择省略字符串参数来创建一个没有文本的Ra

    2024年02月09日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包