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

ops/2024/10/21 6:47:37/

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方法

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

3. 写一个controller

普通类实现implements Initializable即可

java">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”

java"><?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

java">  @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. 具体代码

java">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


http://www.ppmy.cn/ops/120038.html

相关文章

关系型数据库和非关系型数据库的区别

1.常见的主流数据库 关系型数据库&#xff1a; MySql 、达梦 、PostgreSQL 、Oracle 、Sql Server 、Sqlite非关系型数据库&#xff1a; Redis 、MongoDB 、HBase 、 Neo4J 、 CouchDB 2.介绍 关系型数据库最典型的数据结构是表&#xff0c;由二维表及其之间的联系…

聊聊JIT是如何影响JVM性能的!

文章内容收录到个人网站&#xff0c;方便阅读&#xff1a;http://hardyfish.top/ 文章内容收录到个人网站&#xff0c;方便阅读&#xff1a;http://hardyfish.top/ 文章内容收录到个人网站&#xff0c;方便阅读&#xff1a;http://hardyfish.top/ 我们知道Java虚拟机栈是线程…

在线代码编辑器

在线代码编辑器 文章说明前台核心代码后台核心代码效果展示源码下载 文章说明 采用Java结合vue3设计实现的在线代码编辑功能&#xff0c;支持在线编辑代码、运行代码&#xff0c;同时支持导入文件&#xff0c;支持图片识别&#xff0c;支持复制代码&#xff0c;可将代码导出为图…

在线PDF怎么转换成JPG图片?分享14种转换操作!

作为一名社畜&#xff0c;俺也经常要将PDF转换为图片格式&#xff01; 如何进行快速转换&#xff0c;包括电脑端、在线端和手机端&#xff0c;今天俺就测评了50款工具&#xff0c;给你得出了下面这些渠道&#xff0c;不少也是免费的&#xff0c;相信对你有帮助哦&#xff01; …

TypeScript 封装 Axios 1.7.7

随着Axios版本的不同&#xff0c;类型也在改变&#xff0c;以后怎么写类型&#xff1f; 1. 封装Axios 将Axios封装成一个类&#xff0c;同时重新封装request方法 重新封装request有几个好处&#xff1a; 所有的请求将从我们定义的requet请求中发送&#xff0c;这样以后更换…

发现一款适合所有用户小巧且强大的编辑器(完美替换Windows记事本)

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 编辑器 📒📝 功能亮点📝 适用场景📝 安装使用📝 替换Windows记事本🎈 获取方式 🎈⚓️ 相关链接 ⚓️📖 介绍 📖 今天,发现一款小巧(仅1.26M)且功能强大的编辑器,适用于文本编辑,编程开发等,应该说是适…

自己写string类

我们写string类不是为了和先人比较谁写得更好&#xff0c;而是去了解string最底层是怎么实现的&#xff0c;我们将通过这篇文章来自己实现string的一些简单的功能。大家和我一起动手实现一下吧。 我先将string.h附在下面&#xff0c;上面写的所有函数都是我们要在string.cpp里…

试用Foxit PDF: 在网页中单页展示PDF

产品需求 在网页中展示PDF文件&#xff0c;每次展示一页&#xff0c;通过按钮进行翻页。 思路分析 之前网站功能分别使用vue-pdf和pdfjs-dist实现过&#xff0c;但渲染速度都特别慢&#xff0c;所以想试试foxit pdf插件。网站只是用插件单页展示PDF文件&#xff0c;功能简单&a…