当前位置: 首页 > 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

http://www.lryc.cn/news/450661.html

相关文章:

  • 数据结构之——单循环链表和双向循环链表
  • Git Stash: 管理临时更改的利器
  • ELK--收集日志demo
  • Redis的主要特点及运用场景
  • 与我免费ai书童拆解《坚持》创作历程
  • 昇思MindSpore进阶教程--下沉模式
  • Hive SQL业务场景:连续5天涨幅超过5%股票
  • Java 如何从图片上提取文字
  • C#进阶-读写Excel常用框架及其使用方式
  • Python爬虫lxml模块安装导入和xpath基本语法
  • python魔法(python高级magic方法进阶)
  • 【论文笔记】Flamingo: a Visual Language Model for Few-Shot Learning
  • 问:JAVA阻塞队列实现类及最佳实践?
  • Springboot3 + MyBatis-Plus + MySql + Vue + ProTable + TS 实现后台管理商品分类(最新教程附源码)
  • 消费电子制造企业如何使用SAP系统提升运营效率与竞争力
  • 算法记录——树
  • 单片机在控制和自动化任务中的应用场景广泛
  • UEFI EDK2框架学习(三)——protocol
  • PostgreSQL的字段存储类型了解
  • CTFshow 命令执行 web29~web36(正则匹配绕过)
  • 【顺序表使用练习】发牌游戏
  • 1.7 编码与调制
  • 004集—— txt格式坐标写入cad(CAD—C#二次开发入门)
  • CSS中的font-variation-settings:探索字体的可变性
  • 组合优化与凸优化 学习笔记5 对偶拉格朗日函数
  • 监控易监测对象及指标之:Exchange邮件服务器监测
  • 【机器学习基础】Transformer学习
  • mysql如何不使用窗口函数,去统计出入库情况
  • uni-app canvas文本自动换行
  • 【设计模式-职责链】