首先,javafx有点像前端,我们可以把他理解为这样的结构

分别为
窗口:就是显示的窗口
场景:相当于页面,整个可操作的大小,估计可以切换不同场景
节点:就是内部各个模块,自己一个个设置
代码如下

package cc.caiguang.hello;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;public class Main extends Application {public static void main(String[] args) {Application.launch(args);/*Application类还能获取主机网址* */}@Overridepublic void start(Stage stage) throws Exception {Label label = new Label("你好javaFX");/*标签放到布局里*/BorderPane pane = new BorderPane(label);/*布局放到场景里,设置高度宽度*/Scene scene = new Scene(pane,300,300);/*场景放到窗口里*/stage.setScene(scene);stage.setTitle("窗口");stage.show();/** Stage 窗口 下面是* Scene 场景 下面是* parent  根节点 下面是* 节点 note label* */System.out.println("调用主程序...");}@Overridepublic void init() throws Exception{super.init();System.out.println("开始...");/*进行初始化工作,比如建立数据库链接* 新建线程与窗口同步进行 * */}@Overridepublic void stop() throws Exception{System.out.println("结束...");/*进行清理数据库链接工作,* 清理资源链接** */}}

显示如下,我们需要注意的是其实就是一个套一个,不同节点(控件)传入场景,不同场景传入窗口

而调用时会调用另外两个父类方法,我们给他重写,发现一个是启动方法,一个是结束方法
一个运行在主程序开始之前,一个运行在主程序结束

用途就可以多样化比如
启动方法:进行初始化工作,比如建立数据库链接
新建线程与窗口同步进行
结束方法:进行清理数据库链接工作,
清理资源链接

Application类还能获取主机网址

package cc.caiguang.hello;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {public static void main(String[] args) {Application.launch(args);/*Application类还能获取主机网址* */}@Overridepublic void start(Stage stage) throws Exception {Button button = new Button("按钮");BorderPane pane = new BorderPane(button);button.setOnAction(e ->{getHostServices().showDocument("www.bilibili.com");});/*布局放到场景里,设置高度宽度*/Scene scene = new Scene(pane,300,300);/*场景放到窗口里*/stage.setScene(scene);stage.setTitle("窗口");stage.show();System.out.println("调用主程序...");}}

运行结果是

我们可以知道显示什么内容(比如这里的按钮)只需要传入不同对象

        Label label = new Label("你好javaFX");BorderPane pane = new BorderPane(label);Button button = new Button("按钮");BorderPane pane = new BorderPane(button);

我们还利用了这个类的调用网址的功能
调用了小破站

        button.setOnAction(e ->{getHostServices().showDocument("www.bilibili.com");})

Stage类
Titel:设置窗口名称
icon:设置窗口图标
resiziable:设置窗口是否能调节大小
x,y,width,height:设置原始宽高
StageStyle:设置窗口样式,4种

在添加图标时

放入图片

import javafx.stage.Stage;
public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.setTitle(" Hello");primaryStage.getIcons().add(new Image("image/icon.ico"));primaryStage.show();}}

视频中可以不过不知道为什么报错

Exception in Application start method

网上找方法后发现这样可以

package cc.caiguang.hello;import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;import java.io.File;public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {String path = "image/img.png";File fileIcon = new File(path);Image applicationIcon = new Image(fileIcon.toURI().toString());primaryStage.getIcons().add(applicationIcon);primaryStage.setTitle(" Hello");Label label = new Label("添加图标");BorderPane pane = new BorderPane(label);Scene scene = new Scene(pane,300,200);primaryStage.setScene(scene);primaryStage.show();}}
        String path = "image/img.png";File fileIcon = new File(path);Image applicationIcon = new Image(fileIcon.toURI().toString());primaryStage.getIcons().add(applicationIcon);

看上去像是没法直接通过路径识别图片文件,进行了转换

primaryStage.getIcons().add(new Image("image/icon.ico"));

那我们可以改成

package cc.caiguang.hello;import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;import java.io.File;public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));primaryStage.setTitle(" Hello");Label label = new Label("添加图标");BorderPane pane = new BorderPane(label);Scene scene = new Scene(pane,300,200);primaryStage.setScene(scene);primaryStage.show();}}

效果如下

4种窗口样式

StageStyle.DECORATED

默认,和不设置一样

StageStyle.UNDECORATED

无装饰,上面那一栏没有

StageStyle.TRANSPARENT

透明窗口

StageStyle.UTILITY

简单装饰
没啥太大区别,一般使用默认

下面是点击跳转另一个页面

package cc.caiguang.hello;import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;import java.io.File;public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));primaryStage.setTitle(" Hello");Button button1 = new Button("按钮1");Button button2 = new Button("按钮2");button1.setLayoutX(200);button1.setLayoutY(200);button2.setLayoutX(200);button2.setLayoutY(250);button1.setOnAction(e ->{getHostServices().showDocument("www.bilibili.com");});button2.setOnAction(event ->{Stage stage = new Stage();stage.setHeight(200);stage.setWidth(600);stage.initModality(Modality.NONE);stage.show();});AnchorPane pane = new AnchorPane();pane.getChildren().addAll(button2,button1);Scene scene = new Scene(pane,500,500);primaryStage.setScene(scene);primaryStage.initStyle(StageStyle.DECORATED);primaryStage.show();}}

效果如下

无修

Modality.NONE

这时候我们发现,Stage不就是窗口吗,那我们可以在新的窗口中继续操作,于是我们就可以
无限套娃

package cc.caiguang.hello;import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;import java.io.File;public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));primaryStage.setTitle(" Hello");Button button1 = new Button("按钮1");Button button2 = new Button("按钮2");button1.setLayoutX(200);button1.setLayoutY(200);button2.setLayoutX(200);button2.setLayoutY(250);button1.setOnAction(e ->{getHostServices().showDocument("www.bilibili.com");});button2.setOnAction(event ->{Button button3 = new Button("按钮1");Button button4 = new Button("按钮2");button3.setLayoutX(200);button3.setLayoutY(200);button4.setLayoutX(200);button4.setLayoutY(250);AnchorPane pane = new AnchorPane();pane.getChildren().addAll(button3,button4);Scene scene = new Scene(pane,500,500);Stage stage = new Stage();stage.setHeight(400);stage.setWidth(400);stage.initModality(Modality.NONE);//修饰格式,Modality.NONE表示无修饰stage.setScene(scene);stage.setTitle("窗口");stage.show();});AnchorPane pane = new AnchorPane();pane.getChildren().addAll(button2,button1);Scene scene = new Scene(pane,500,500);primaryStage.setScene(scene);primaryStage.initStyle(StageStyle.DECORATED);primaryStage.show();}}

这里发现button名字不能重复,我们要换成不同的名字

设置

stage.initOwner(primaryStage);

再设置
WINDOW_MODAL
设置这个时,副窗口激活,主窗口不能用,其他都可以
APPLICATION_MODAL
设置这个时,副窗口激活,其他窗口不能用

package cc.caiguang.hello;import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;import java.io.File;public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));primaryStage.setTitle(" Hello");Button button1 = new Button("按钮1");Button button2 = new Button("按钮2");button1.setLayoutX(200);button1.setLayoutY(200);button2.setLayoutX(200);button2.setLayoutY(250);button2.setOnAction(event ->{Button button3 = new Button("按钮1");Button button4 = new Button("按钮2");button3.setLayoutX(200);button3.setLayoutY(200);button4.setLayoutX(200);button4.setLayoutY(250);AnchorPane pane = new AnchorPane();pane.getChildren().addAll(button3,button4);Scene scene = new Scene(pane,500,500);Stage stage = new Stage();stage.setHeight(400);stage.setWidth(400);stage.initModality(Modality.APPLICATION_MODAL);//修饰格式,Modality.NONE表示无修饰//Modality.APPLICATION_MODAL 全局应用模态,其他窗口借用//Modality.WINDOW_MODAL 要有副窗口stage.setScene(scene);stage.initOwner(primaryStage);stage.setTitle("窗口");stage.show();});button1.setOnAction(event ->{Stage stage = new Stage();stage.setHeight(400);stage.setWidth(400);stage.setTitle("窗口");stage.show();});AnchorPane pane = new AnchorPane();pane.getChildren().addAll(button2,button1);Scene scene = new Scene(pane,500,500);primaryStage.setScene(scene);primaryStage.initStyle(StageStyle.DECORATED);primaryStage.show();}}

关闭窗口事件,其他大同小异

package cc.caiguang.hello;import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;import java.io.File;
import java.util.Optional;public class Main extends Application {public static void main(String[] args) {Application.launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.getIcons().add(new Image((new File("image/img.png")).toURI().toString()));primaryStage.setTitle(" Hello");Button button1 = new Button("按钮1");Button button2 = new Button("按钮2");button1.setLayoutX(200);button1.setLayoutY(200);button2.setLayoutX(200);button2.setLayoutY(250);AnchorPane pane = new AnchorPane();pane.getChildren().addAll(button2,button1);Scene scene = new Scene(pane,500,500);Platform.setImplicitExit(false);//取消操作系统默认退出动作primaryStage.setOnCloseRequest(e ->{e.consume();//取消关闭窗口动作Alert alert = new Alert(Alert.AlertType.CONFIRMATION);alert.setTitle("退出程序");alert.setHeaderText(null);alert.setContentText("是否退出");//弹出框,是否退出Optional<ButtonType> result = alert.showAndWait();if (result.get() ==ButtonType.OK){Platform.exit();//系统关闭应用}});primaryStage.setScene(scene);primaryStage.initStyle(StageStyle.DECORATED);primaryStage.show();}}

展示效果

这个是关闭窗口,结束运行程序
还有


Platform.exit();
//系统关闭应用
primaryStage.close();
//仅关闭窗口,不结束程序

以上就是今天学习内容,有人知道那个图片的为啥直接写报错吗

JAVAFX学习笔记相关推荐

  1. JavaFX 学习笔记——窗口与控件

    前言 如今比较流行的桌面gui框架有WPF.WinForm.Qt.javafx等.其中WPF和WinForm目前还只能在运行Winsows上.Qt(widget)是一个很强大的跨平台C++框架(不只是 ...

  2. javaFX学习笔记之 散布图(Scatter Chart)

    (Scatter Chart),它是一种用一组点来表示数据的双轴图表. 每个点通过X和Y值来定义.跟其他双轴图表类似,你可以创建一组或者多组数据.图展示了一个带有三组数据的Scatter Chart. ...

  3. JavaFX学习笔记(最全,最详细)

    文章目录 Java: JavaFX桌面GUI开发 1.基本概念 2.最小框架代码 3.控件布局 4,初步认识stage窗口 5,stage窗口模式(StageStyle) 6,screen类的使用 7 ...

  4. JavaFX+Jfoenix 学习笔记(序)--引言And软件截图

    文章目录(更新中): JavaFX+Jfoenix 学习笔记(序)--引言And软件截图 JavaFX+Jfoenix 学习笔记(一)--环境搭建及多款Hello Word演示 JavaFX+Jfoe ...

  5. JavaFX+Jfoenix 学习笔记(五)--ContextMenu右键菜单

    1.右键菜单,如图 2.实例-1:最简单的右键菜单 下面我们演示一个通过右键菜单来改变背景和字体颜色的例子. package zkh.javafx.learn.contextmenu;import j ...

  6. 我的Android进阶之旅------gt;Android中编解码学习笔记

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  7. 安全学习笔记(一):基础概念

    域名 什么是域名 域名(英语:Domain Name),又称网域,是由一串用点分隔的名字组成的Internet上某一台计算机或计算机组的名称,用于在数据传输时对计算机的定位标识(有时也指地理位置) 由 ...

  8. 编解码学习笔记(基础)

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放license收费等等 ...

  9. PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 call

    您的位置 首页 PyTorch 学习笔记系列 PyTorch 学习笔记(六):PyTorch hook 和关于 PyTorch backward 过程的理解 发布: 2017年8月4日 7,195阅读 ...

最新文章

  1. Flutter开发Flutter与原生OC、Java的交互通信-2(48)
  2. CYPRESS USB芯片win10驱动
  3. 【原】postman常用设置全局变量的js片段
  4. oracle client 11.2.0.3 32位,oracle client 32位/64位下载(Oracl数据库)
  5. [易学易懂系列|golang语言|零基础|快速入门|(一)]
  6. 青岛自然人税收管理系统服务器地址,青岛市自然人税收管理系统扣缴客户端
  7. 一条windows指令合并ts文件
  8. 未转变者3.16进不去服务器,未转变者3.16.0.1
  9. 钉钉日志范文100篇_钉钉怎么添加日志模板 几步轻松添加
  10. 全国计算机二级12月福建报名时间,2020年12月福建计算机二级考试报名时间安排...
  11. hosts文件位置在哪里
  12. 计算机应用职业的外部环境,完整的职业生涯规划书范文
  13. Linux下的打包(tar)、压缩(gzip / bzip2)
  14. office 2010 ppt 添加音乐等相关问题
  15. java吸血鬼_4位吸血鬼数字的java实现思路与实例讲解
  16. 团队问卷调查结果报告
  17. 积米:化妆找吉米,逛街找积米
  18. 各航空公司的网址和电话
  19. 正式进入Hadoop学习 不会再有比我还全面的大数据学习(三) Hadoop生态圈
  20. typescript (TS)进阶篇 --- 内置高阶泛型工具类型(Utility Type)

热门文章

  1. Android开发学习资源之(三)
  2. 【python实现生成隐藏的一句话木马】
  3. 流媒体文件应用常见问题解答
  4. 百度知道推出企业问答平台
  5. Java如何获取MD5值
  6. sql语句查询最近七天 三十天 数据
  7. tengine升级h2的问题
  8. 辨别盗版windows 7
  9. 单品购买商城源码/单个商品详情页购买源码下载
  10. Dreamweaver8加载站点缓存时卡死,解决办法: