手拉手JavaFX UI控件与springboot3+FX桌面开发(下)

简介: 手拉手JavaFX UI控件与springboot3+FX桌面开发

手拉手JavaFX UI控件与springboot3+FX桌面开发(中):https://developer.aliyun.com/article/1431715


drawImage绘制图片



drawImage(Image img,double sx,double sy,double sw,double sh,double dx,double dy,double dw,double dh)


img:要绘制的图片


sx,sy,sw,sh:源矩形(s,source)


dx,dy,sw,dh:目标矩形(d,destination)


样式单CSS


CSS,Cascading Style Sheets


用于定义界面的显示样式(背景,边框,字体等)


-fx-padding:填充、-fx-font-size:字体大小、-fx-text-fill:文件颜色


新建javafx项目时,已经默认创建了一个CSS文件:application.css


图片背景



CSS文件

#pane{
    -fx-background-image: url("中国风1.JPG");
    /*-fx-background-repeat: stretch;
    -fx-background-size: 900 506;
    -fx-background-position: center center;*/
    /*-fx-effect: dropshadow(three-pass-box, black, 30, 0.5, 0, 0);*/
}
.root{
    -fx-background-image: url("中国风1.JPG");
}
Java文件
public class CSSdemo extends Application {
    TextField textField =new TextField();
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("CSSdemo");
        BorderPane borderPane = new BorderPane();
        HBox hbox =new HBox(10);
        Button b =new Button("选择");
        Button c =new Button("提交");
        textField.getStyleClass().add("text");
        b.getStyleClass().add("button");
        hbox.getChildren().addAll(textField,b,c);
       // borderPane.setId("pane");
        borderPane.setTop(hbox);
        Scene scene =new Scene(borderPane,300,400);
        //scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
        //scene.getStylesheets().add(CSSdemo.class.getResource("style.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch();
    }
}


SceneBuilder可视化设计



Eclipse添加javafx组件


在Work with中添加:http://download.eclipse.org/efxclipse/updates-released/2.3.0/site/


Eclpise-jdk11-javaFx


https://openjfx.cn/

module Java11 {
   exports com;
   requires javafx.base;
   requires javafx.controls;
   requires javafx.fxml;
   requires javafx.graphics;
   requires javafx.media;
   requires javafx.swing;
   requires javafx.web;
   requires javafx-swt;
}


媒体播放器


Javafx自带媒体播放的支持


Media:表示一个媒体文件(URL)


MediaPlayer:表示一个播放器(播放控件)


MediaView:表示一个播放控件(显示)


Eclpise应用程序的发布



创建一个文件夹,将jdk里的jre拷贝过去


创建启动脚本


IEDA 应用程序的发布


右击项目或模块


open module settings


选择ArtiFacts->JAR->From modules with dependencies


可以选择包含测试类或者不包含


第一个仅导出目标jar包


第二个导出目标jar包和项目所依赖的jar包


选择Include in project build


Build -> Build Artifacts -> Build


构建结果如下:


创建一个文件夹,将jdk里的jre拷贝过去


创建启动脚本


start jre\bin\javaw.exe -jar ch01.jar


登入窗口+小知识点


public class LoginDemo extends Application{
   public static void main(String[] args) {
     launch(args);
   }
   @Override
   public void start(Stage stage) throws Exception {
     stage.setTitle("Login");
     Label name =new Label("账号:");
     Label password = new Label("密码:");
     name.setFont(Font.font("宋体",20));
     TextField inname =new TextField();
     inname.setUserData("hello");
     PasswordField inPasswordField =new PasswordField();
     inPasswordField.setUserData("123456");
     Button login =new Button("登录");
     Button clear =new Button("清理");
     GridPane gr =new GridPane();
     gr.setStyle("-fx-background-color:#FFF5EE");
             //列,行
     gr.add(name, 0, 0);
     gr.add(inname, 1, 0);
     gr.add(password, 0, 1);
     gr.add(inPasswordField, 1, 1);
     gr.add(login, 1, 2);
     gr.add(clear, 0, 2);
     gr.setHgap(10);
     gr.setVgap(10);
     GridPane.setMargin(login, new Insets(0, 0, 0, 150));
     gr.setAlignment(Pos.CENTER);
     //清理事件
     clear.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
           inname.setText("");
           inPasswordField.setText("");
        }
     });
     //登录事件
     login.setOnAction((e)->{
        String name1 =inname.getText();
        String password1 =inPasswordField.getText();
        if (inname.getUserData().equals(name1) && inPasswordField.getUserData().equals(password1)) {
           loginWindos lonWindos = new loginWindos(name1);
           stage.close();
        }else {
           Alert waring = new Alert(Alert.AlertType.WARNING);
               waring.setHeaderText("用户名/密码出错");
               waring.setContentText("请重新输入!");
               //showAndWait();当输入的焦点在对话框里,后面的界面无法输入(不往下走,知道对话框关闭)
               waring.showAndWait();
               //呼吸灯
               FadeTransition fade = new FadeTransition();
               fade.setDuration(javafx.util.Duration.seconds(2));
               fade.setNode(gr);
               fade.setFromValue(0);
               fade.setToValue(1);
               fade.play();
        }
     });
     Scene scene =new Scene(gr);
     stage.setScene(scene);
     stage.setWidth(400);
     stage.setHeight(500);
     stage.setResizable(false);
     stage.show();
   }
   class  loginWindos
   {
     private final Stage stage = new Stage();
     public loginWindos(String name) {
        stage.setTitle("qgs");
         Alert waring = new Alert(Alert.AlertType.INFORMATION);
               waring.setHeaderText("欢迎"+name+"来到qgs操作界面");
               waring.showAndWait();
        stage.show();
     }
   }
}


Springboot3+JavaFX


Pom.xml加入依赖

<dependency>
   <groupId>org.openjfx</groupId>
   <artifactId>javafx-controls</artifactId>
   <version>17.0.2</version>
</dependency>

package com.example.fx;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class fxmain extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("SceneDemo");
        BorderPane borderPane =new BorderPane();
        TextArea textArea =new TextArea();
        HBox hbox =new HBox();
        Button openbaidu =new Button("打开百度");
        Button getScreenvalue = new Button("获取屏幕属性值");
        hbox.getChildren().addAll(openbaidu,getScreenvalue);
        openbaidu.setOnAction((e)->{
            HostServices hostServices = getHostServices();
            hostServices.showDocument("https://www.baidu.com/");
        });
        getScreenvalue.setOnAction((e)->{
            Screen primary = Screen.getPrimary();
            double dpi = primary.getDpi();
            System.out.println("当前屏幕dpi:"+dpi);
            Rectangle2D rec1 = primary.getBounds();
            Rectangle2D rec2 = primary.getVisualBounds();
            textArea.appendText("\n----全部屏幕--------");
//            textArea.appendText("\n左上角x:"+rec1.getMinX()+"左上角y"+rec1.getMinY());
//            textArea.appendText("\n右下角x--"+ rec1.getMaxX()+"右下角y--"+ rec1.getMaxY());
            textArea.appendText("\n宽度:"+rec1.getWidth()+"高度"+rec1.getHeight());
            textArea.appendText("\n----可以看到的屏幕--------");
//            textArea.appendText("\n左上角x:"+rec2.getMinX()+"左上角y"+rec2.getMinY());
//            textArea.appendText("\n右下角x--"+ rec2.getMaxX()+"右下角y--"+ rec2.getMaxY());
            textArea.appendText("\n宽度:"+rec2.getWidth()+"高度"+rec2.getHeight());
        });
        borderPane.setTop(hbox);
        borderPane.setCenter(textArea);
        Scene scene =new Scene(borderPane,400,400);
        scene.setCursor(Cursor.CLOSED_HAND);//手
        /**
         * scene.setCursor(Cursor.HAND);//手,箭头啥的
         * Cursor CROSSHAIR  光标十字光标
         * Cursor . DEFAULT 光标默认值
         * Cursor DISAPPEAR   光标消失
         * Cursor CLOSED_HAND 光标闭合手
         * Contextmenudemo 上下文菜单演示
         * Cursor E _ RESIZE 光标E _ RESIZE
         */
        stage.setScene(scene);
        stage.show();
    }
}

package com.example.fx;
import com.example.domain.Corrdinate;
import com.example.util.EasyExcelUtil;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.geometry.Rectangle2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.FileChooser;
import javafx.stage.Screen;
import javafx.stage.Stage;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class fxmain extends Application {
    private static String filePath =null;
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("坐标验证");
        BorderPane borderPane =new BorderPane();
        TextArea textArea =new TextArea();
        HBox hbox =new HBox();
        Button openbaidu =new Button("打开百度");
        Button upFile =new Button("上传文件");
        Button executeFile =new Button("执行");
        Button getScreenvalue = new Button("获取屏幕属性值");
        hbox.getChildren().addAll(upFile,executeFile);
        openbaidu.setOnAction((e)->{
            HostServices hostServices = getHostServices();
            hostServices.showDocument("https://www.baidu.com/");
        });
        //上传文件
        upFile.setOnAction((e)->{
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择Excel文件");
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
            //过滤
            fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("All Excel", "*.xlsx"),
                    new FileChooser.ExtensionFilter("XLS", "*.xls"), new FileChooser.ExtensionFilter("XLSX", "*.xlsx"));
            File file = fileChooser.showOpenDialog(stage);
            try {
                System.out.println("路径:"+file.getCanonicalPath());
                textArea.setText("路径:"+file.getCanonicalPath());
                filePath=file.getCanonicalPath();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            if (file != null) {
            }
        });
        //执行
        executeFile.setOnAction((e)->{
            List<Corrdinate> list = EasyExcelUtil.importExcel(filePath);
            textArea.setText("生成文件路径C:\\Users\\Administrator\\Desktop\\demofile");
            for (Corrdinate corrdinate : list) {
                textArea.appendText("\n"+corrdinate.toString());
            }
        });
        getScreenvalue.setOnAction((e)->{
            Screen primary = Screen.getPrimary();
            double dpi = primary.getDpi();
            System.out.println("当前屏幕dpi:"+dpi);
            Rectangle2D rec1 = primary.getBounds();
            Rectangle2D rec2 = primary.getVisualBounds();
            textArea.appendText("\n----全部屏幕--------");
//            textArea.appendText("\n左上角x:"+rec1.getMinX()+"左上角y"+rec1.getMinY());
//            textArea.appendText("\n右下角x--"+ rec1.getMaxX()+"右下角y--"+ rec1.getMaxY());
            textArea.appendText("\n宽度:"+rec1.getWidth()+"高度"+rec1.getHeight());
            textArea.appendText("\n----可以看到的屏幕--------");
//            textArea.appendText("\n左上角x:"+rec2.getMinX()+"左上角y"+rec2.getMinY());
//            textArea.appendText("\n右下角x--"+ rec2.getMaxX()+"右下角y--"+ rec2.getMaxY());
            textArea.appendText("\n宽度:"+rec2.getWidth()+"高度"+rec2.getHeight());
        });
        borderPane.setTop(hbox);
        borderPane.setCenter(textArea);
        Scene scene =new Scene(borderPane,400,400);
        scene.setCursor(Cursor.CLOSED_HAND);//手
        /**
         * scene.setCursor(Cursor.HAND);//手,箭头啥的
         * Cursor CROSSHAIR  光标十字光标
         * Cursor . DEFAULT 光标默认值
         * Cursor DISAPPEAR   光标消失
         * Cursor CLOSED_HAND 光标闭合手
         * Contextmenudemo 上下文菜单演示
         * Cursor E _ RESIZE 光标E _ RESIZE
         */
        stage.setScene(scene);
        stage.show();
    }
}


jdk1.8以上的版本(jdk9,jdk11,jdk17等)没有jre的问题


D:\Program Files\Java\jdk-17目录下执行


bin\jlink.exe --module-path jmods --add-modules java.desktop --output jre


目录
相关文章
|
前端开发 Java 关系型数据库
基于Java+Springboot+Vue开发的鲜花商城管理系统源码+运行
基于Java+Springboot+Vue开发的鲜花商城管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的鲜花商城管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。技术学习共同进步
911 7
|
人工智能 Java 数据库
飞算 JavaAI:革新电商订单系统 Spring Boot 微服务开发
在电商订单系统开发中,传统方式耗时约30天,需应对复杂代码、调试与测试。飞算JavaAI作为一款AI代码生成工具,专注于简化Spring Boot微服务开发。它能根据业务需求自动生成RESTful API、数据库交互及事务管理代码,将开发时间缩短至1小时,效率提升80%。通过减少样板代码编写,提供规范且准确的代码,飞算JavaAI显著降低了开发成本,为软件开发带来革新动力。
|
缓存 NoSQL Java
基于SpringBoot的Redis开发实战教程
Redis在Spring Boot中的应用非常广泛,其高性能和灵活性使其成为构建高效分布式系统的理想选择。通过深入理解本文的内容,您可以更好地利用Redis的特性,为应用程序提供高效的缓存和消息处理能力。
1634 79
|
供应链 JavaScript BI
ERP系统源码,基于SpringBoot+Vue+ElementUI+UniAPP开发
这是一款专为小微企业打造的 SaaS ERP 管理系统,基于 SpringBoot+Vue+ElementUI+UniAPP 技术栈开发,帮助企业轻松上云。系统覆盖进销存、采购、销售、生产、财务、品质、OA 办公及 CRM 等核心功能,业务流程清晰且操作简便。支持二次开发与商用,提供自定义界面、审批流配置及灵活报表设计,助力企业高效管理与数字化转型。
1007 2
ERP系统源码,基于SpringBoot+Vue+ElementUI+UniAPP开发
|
人工智能 自然语言处理 API
用自然语言控制电脑,字节跳动开源 UI-TARS 的桌面版应用!内附详细的安装和配置教程
UI-TARS Desktop 是一款基于视觉语言模型的 GUI 代理应用,支持通过自然语言控制电脑操作,提供跨平台支持、实时反馈和精准的鼠标键盘控制。
7157 17
用自然语言控制电脑,字节跳动开源 UI-TARS 的桌面版应用!内附详细的安装和配置教程
|
Java API 微服务
Java 21 与 Spring Boot 3.2 微服务开发从入门到精通实操指南
《Java 21与Spring Boot 3.2微服务开发实践》摘要: 本文基于Java 21和Spring Boot 3.2最新特性,通过完整代码示例展示了微服务开发全流程。主要内容包括:1) 使用Spring Initializr初始化项目,集成Web、JPA、H2等组件;2) 配置虚拟线程支持高并发;3) 采用记录类优化DTO设计;4) 实现JPA Repository与Stream API数据访问;5) 服务层整合虚拟线程异步处理和结构化并发;6) 构建RESTful API并使用Springdoc生成文档。文中特别演示了虚拟线程配置(@Async)和StructuredTaskSco
1331 0
|
人工智能 自然语言处理 前端开发
20分钟上手DeepSeek开发:SpringBoot + Vue2快速构建AI对话系统
本文介绍如何使用Spring Boot3与Vue2快速构建基于DeepSeek的AI对话系统。系统具备实时流式交互、Markdown内容渲染、前端安全防护等功能,采用响应式架构提升性能。后端以Spring Boot为核心,结合WebFlux和Lombok开发;前端使用Vue2配合WebSocket实现双向通信,并通过DOMPurify保障安全性。项目支持中文语义优化,API延迟低,成本可控,适合个人及企业应用。跟随教程,轻松开启AI应用开发之旅!
|
数据安全/隐私保护 开发者
产品经理-桌面端UI名词
AxureMost 提供了一套完整的桌面端 UI 组件库,涵盖通用、布局、导航、数据录入、数据展示、反馈及其他组件。每个组件都具备详细的设计规范和资源,帮助设计师和开发者快速构建功能丰富的用户界面。组件库包括按钮、表单、表格、对话框等,全面支持各类应用场景。
369 8
产品经理-桌面端UI名词
|
移动开发 前端开发 Java
Java最新图形化界面开发技术——JavaFx教程(含UI控件用法介绍、属性绑定、事件监听、FXML)
JavaFX是Java的下一代图形用户界面工具包。JavaFX是一组图形和媒体API,我们可以用它们来创建和部署富客户端应用程序。 JavaFX允许开发人员快速构建丰富的跨平台应用程序,允许开发人员在单个编程接口中组合图形,动画和UI控件。本文详细介绍了JavaFx的常见用法,相信读完本教程你一定有所收获!
16536 5
Java最新图形化界面开发技术——JavaFx教程(含UI控件用法介绍、属性绑定、事件监听、FXML)
|
监控 Java 应用服务中间件
SpringBoot是如何简化Spring开发的,以及SpringBoot的特性以及源码分析
Spring Boot 通过简化配置、自动配置和嵌入式服务器等特性,大大简化了 Spring 应用的开发过程。它通过提供一系列 `starter` 依赖和开箱即用的默认配置,使开发者能够更专注于业务逻辑而非繁琐的配置。Spring Boot 的自动配置机制和强大的 Actuator 功能进一步提升了开发效率和应用的可维护性。通过对其源码的分析,可以更深入地理解其内部工作机制,从而更好地利用其特性进行开发。
690 6