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

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

    • 前提
    • 步骤
      • [1. 普通类继承Application](#1. 普通类继承Application)
      • [2. 实现main方法](#2. 实现main方法)
      • [3. 写一个controller](#3. 写一个controller)
      • [4. 写一个fxml文件](#4. 写一个fxml文件)
      • [5. 写start方法加载fxml](#5. 写start方法加载fxml)
      • [6. 具体代码](#6. 具体代码)
      • [7. 运行即可](#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 {

    @FXML
    private TextArea allStatus;

    @FXML
    private TextArea pkRecord;

    @FXML
    private Label mount;

    @FXML
    private Label monitorStatus;


    @Override
    public 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 复制代码
  @Override
  public 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();
    }
    @Override
    public 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

相关推荐
郝学胜-神的一滴2 分钟前
深入探索 C++ 元组:从基础到高级应用
开发语言·c++·stl·软件工程
I'm a winner3 分钟前
第七章:AI进阶之------输入与输出函数(一)
开发语言·人工智能·python·深度学习·神经网络·microsoft·机器学习
木心操作11 分钟前
js生成excel表格进阶版
开发语言·javascript·ecmascript
mask哥12 分钟前
详解mcp以及agen架构设计与实现
java·微服务·flink·大模型·ai agent·springai·mcp
听情歌落俗24 分钟前
MATLAB3-1变量-台大郭彦甫
开发语言·笔记·算法·matlab·矩阵
Propeller31 分钟前
【Android】View 交互的事件处理机制
android·java
杨杨杨大侠34 分钟前
Atlas Mapper 教程系列 (5/10):集合映射与嵌套对象处理
java·开源·github
ERP老兵_冷溪虎山35 分钟前
Python/JS/Go/Java同步学习(第十三篇)四语言“字符串转码解码“对照表: 财务“小南“纸式转码术处理凭证乱码崩溃(附源码/截图/参数表/避坑指南)
java·后端·python
是2的10次方啊36 分钟前
如何设计10万QPS秒杀系统?缓存+消息队列+分布式锁架构实战
java
心灵宝贝37 分钟前
Tomcat Connectors 1.2.37 源码编译安装教程(mod_jk 详细步骤)
java·tomcat