手拉手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


目录
相关文章
|
3月前
|
IDE Java 开发工具
Spring Boot:加速Java后端开发的现代化利器
在当今快速迭代的软件开发环境中,Spring Boot 已成为Java后端开发领域的首选框架。作为Spring家族的一员,它以“约定优于配置”的设计理念,极大地简化了传统Spring应用的配置和部署过程,让开发者能够更加专注于业务逻辑的实现。本文将探讨Spring Boot的核心优势,并通过一个简单的示例展示如何快速启动一个基于Spring Boot的Java Web应用。
79 1
|
20天前
|
C# 开发者 Windows
一款基于Fluent设计风格、现代化的WPF UI控件库
一款基于Fluent设计风格、现代化的WPF UI控件库
|
2天前
|
前端开发 Java 开发者
Spring Boot DevTools 热部署神器,助你在开发浪潮中乘风破浪,一骑绝尘!
【8月更文挑战第29天】在快速迭代的软件开发领域,高效开发至关重要。Spring Boot DevTools 作为一款优秀的热部署工具,可自动重新加载代码修改,无需手动重启应用,大幅节省时间,即时预览修改效果,简化开发流程。通过简单示例,展示了其自动刷新静态资源和模板文件的功能,有效提升了开发效率,使开发者更专注于功能实现。它就像是开发者的得力助手,显著减少等待时间,带来更高效、流畅的开发体验。
|
2月前
|
存储 SQL 测试技术
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍+lw)(2)
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍+lw)
99 2
|
2月前
|
JavaScript Java 关系型数据库
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍+lw)(1)
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍+lw)
95 1
|
2月前
|
JavaScript Java 关系型数据库
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍)(1)
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍)
22 0
基于SpringBoot+Vue交通管理在线服务系统的开发(源码+部署说明+演示视频+源码介绍)(1)
|
2月前
|
文字识别 Java
文本,文字识别07,SpringBoot服务开发-入参和返回值,编写接口的时候,要注意识别的文字返回的是多行,因此必须是List集合,Bean层,及实体类的搭建
文本,文字识别07,SpringBoot服务开发-入参和返回值,编写接口的时候,要注意识别的文字返回的是多行,因此必须是List集合,Bean层,及实体类的搭建
|
2月前
|
文字识别 Java Spring
文本,文字识别,SpringBoot服务开发,SpringBoot如何提供上传服务,接口的设计,它做了将Base64重新转为图片,SpringBoot的应用实例,项目基础搭建
文本,文字识别,SpringBoot服务开发,SpringBoot如何提供上传服务,接口的设计,它做了将Base64重新转为图片,SpringBoot的应用实例,项目基础搭建
|
3月前
|
前端开发 Java 微服务
Spring Boot与微前端架构的集成开发
Spring Boot与微前端架构的集成开发
|
3月前
|
JSON 前端开发 Java
Springboot mvc开发之Rest风格及RESTful简化开发案例
Springboot mvc开发之Rest风格及RESTful简化开发案例
38 2
下一篇
云函数