当前位置: 首页 > news >正文

java-快速将普通main类变为javafx类,并加载自定义fxml

java-快速将普通main类变为javafx类,并加载自定义fxml

    • 前提
    • 步骤
      • 1. 普通类继承Application
      • 2. 实现main方法
      • 3. 写一个controller
      • 4. 写一个fxml文件
      • 5. 写start方法加载fxml
      • 6. 具体代码
      • 7. 运行即可

前提

使用自带javafx的jdk,这里使用的是jdk1.834,当然你可以使用其他的可行版本。

步骤

1. 普通类继承Application

public class server extends Application {

2. 实现main方法

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

3. 写一个controller

普通类实现implements Initializable即可

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;import java.net.URL;
import java.util.ResourceBundle;public class controller implements Initializable {@FXMLprivate TextArea allStatus;@FXMLprivate TextArea pkRecord;@FXMLprivate Label mount;@FXMLprivate Label monitorStatus;@Overridepublic void initialize(URL location, ResourceBundle resources) {System.out.println(2222);}
}

4. 写一个fxml文件

将fxml文件放在项目的resources目录下,后面好引用。另外,重要的是在GridPane节点上增加和controller类的关系,使用fx:controller=“controller”

<?xml version="1.0" encoding="UTF-8"?><?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?><GridPane fx:controller="controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"><columnConstraints><ColumnConstraints hgrow="SOMETIMES" maxWidth="302.0" minWidth="10.0" prefWidth="158.0" /><ColumnConstraints hgrow="SOMETIMES" maxWidth="324.0" minWidth="10.0" prefWidth="285.0" /><ColumnConstraints hgrow="SOMETIMES" maxWidth="140.0" minWidth="10.0" prefWidth="140.0" /></columnConstraints><rowConstraints><RowConstraints maxHeight="128.0" minHeight="4.0" prefHeight="4.0" vgrow="SOMETIMES" /><RowConstraints maxHeight="303.0" minHeight="10.0" prefHeight="303.0" vgrow="SOMETIMES" /><RowConstraints maxHeight="124.0" minHeight="10.0" prefHeight="41.0" vgrow="SOMETIMES" /></rowConstraints><children><TextArea fx:id="allStatus" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1"><GridPane.margin><Insets right="10.0" /></GridPane.margin></TextArea><TextArea fx:id="pkRecord" prefHeight="200.0" prefWidth="200.0" GridPane.rowIndex="1"><GridPane.margin><Insets left="10.0" right="10.0" /></GridPane.margin></TextArea><VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="2" GridPane.rowIndex="1"><children><Label fx:id="mount" prefHeight="36.0" prefWidth="140.0" text="Label"><opaqueInsets><Insets /></opaqueInsets><VBox.margin><Insets bottom="20.0" /></VBox.margin></Label><Label fx:id="monitorStatus" prefHeight="39.0" prefWidth="140.0" text="Label" /></children></VBox></children>
</GridPane>

5. 写start方法加载fxml

  @Overridepublic void start(Stage primaryStage) throws IOException {Parent root = FXMLLoader.load(getClass().getResource("/monitor.fxml"));primaryStage.setTitle("KeDD");primaryStage.setResizable(false);primaryStage.setScene(new Scene(root, 800, 500));primaryStage.show();}

6. 具体代码

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.omg.CORBA.portable.ApplicationException;
import java.io.IOException;public class server extends Application {public static void main(String[] args) {launch();}@Overridepublic void start(Stage primaryStage) throws IOException {Parent root = FXMLLoader.load(getClass().getResource("/monitor.fxml"));primaryStage.setTitle("KeDD");primaryStage.setResizable(false);primaryStage.setScene(new Scene(root, 800, 500));primaryStage.show();}
}

7. 运行即可

运行后会弹出窗口,然后打印出上面的2222

相关文章:

  • go 安装三方库
  • Unity开发绘画板——01.前言
  • C++之String类(下)
  • TypeScript 算法手册【插入排序】
  • 五、CAN总线
  • 《NoSQL》非关系型数据库MongoDB 学习笔记!
  • 2024年3分钟手把手教你激活Guitar Pro 8破解版
  • 工业现场干扰问题及处理方法
  • 移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——15.红黑树
  • Eclipse 快捷键:提高开发效率的利器
  • 【C语言】指针详解(一)
  • 在 Kali Linux 中安装 Impacket
  • pytorch张量基础
  • 物联网将如何影响全球商业?
  • Java基础——十二、容器
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Android交互
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • Linux编程学习笔记 | Linux IO学习[1] - 文件IO
  • Object.assign方法不能实现深复制
  • TCP拥塞控制
  • TypeScript迭代器
  • 高性能JavaScript阅读简记(三)
  • 驱动程序原理
  • 通信类
  • 我这样减少了26.5M Java内存!
  • 项目实战-Api的解决方案
  • hi-nginx-1.3.4编译安装
  • 大数据全解:定义、价值及挑战
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #Linux(权限管理)
  • #图像处理
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (3)(3.5) 遥测无线电区域条例
  • (3)选择元素——(17)练习(Exercises)
  • (6)设计一个TimeMap
  • (7)摄像机和云台
  • (SERIES12)DM性能优化
  • (阿里云在线播放)基于SpringBoot+Vue前后端分离的在线教育平台项目
  • (纯JS)图片裁剪
  • (二)fiber的基本认识
  • (七)glDrawArry绘制
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • .NET 4.0中的泛型协变和反变
  • .Net Core中的内存缓存实现——Redis及MemoryCache(2个可选)方案的实现
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .NET/C# 的字符串暂存池
  • .net项目IIS、VS 附加进程调试
  • ??myeclipse+tomcat
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
  • [ 蓝桥杯Web真题 ]-布局切换
  • [AIGC] 广度优先搜索(Breadth-First Search,BFS)详解
  • [AX]AX2012 AIF(四):文档服务应用实例