Java: JavaFX桌面GUI开发

简介: Java: JavaFX桌面GUI开发

1、基本概念

窗口          Stage
  -场景       Scene
    -布局     stackPane
      -控件   Button

2、最小框架代码

创建命令行应用

package com.company;


import javafx.application.Application;
import javafx.stage.Stage;


public class HelloWorld extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

对就是啥都没有,空白的窗体

d22.3.png


3、控件布局

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
// 实例化按钮
Button button = new Button("这是按钮上的文字");

// 创建布局控件
StackPane stackPane = new StackPane();

// 将button添加到布局
stackPane.getChildren().add(button);

// 创建场景 宽=400 高=400
Scene scene = new Scene(stackPane, 400, 400);

// 将场景添加到窗口
primaryStage.setScene(scene);

// 显示窗口
primaryStage.show();
}
}

d22.4.png


4、事件添加

Main.java


package com.company;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application implements EventHandler<MouseEvent> {
private Button button;

public static void main(String[] args) {
// write your code here
// System.out.println("你好");
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
// 实例化按钮
button = new Button("这是按钮");

// 1、添加按钮点击事件, this.handle 处理事件
// button.setOnMouseClicked(this);

// 2、使用单独实现的类 事件监听
// button.setOnMouseClicked(new MyMouseEvent());

// 3、使用匿名类添加事件监听
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("鼠标点击按钮了");
}
});

// 4、jdk 8 使用简写执行一条输出
button.setOnMouseClicked(e -> System.out.println("简写的监听事件"));

// 5、同时输出多条
button.setOnMouseClicked(e -> {
System.out.println("简写的监听事件1");
System.out.println("简写的监听事件2");
});

// 创建布局控件
StackPane stackPane = new StackPane();

// 将button添加到布局
stackPane.getChildren().add(button);

// 创建场景
Scene scene = new Scene(stackPane, 400, 400);

// 给场景添加事件处理的对象
// scene.setOnMousePressed(this);
scene.setOnMousePressed(new MySceneMouseEvent());

// 将场景添加到窗口
primaryStage.setScene(scene);

// 显示窗口
primaryStage.show();
}

@Override
public void handle(MouseEvent event) {

// event.getSource() 获取事件对象
if (event.getSource() == button) {
System.out.println("点击了按钮");
} else {
System.out.println("点击了场景");
}
}
}

MyMouseEvent.java 处理鼠标点击事件的类

package com.company;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

public class MyMouseEvent implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent event) {
System.out.println("MyMouseEvent click");
}
}

MySceneMouseEvent.java 处理场景点击事件的类

package com.company;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

public class MySceneMouseEvent implements EventHandler<MouseEvent> {
@Override
public void handle(MouseEvent event) {
System.out.println("场景鼠标点击");
}
}

5、场景切换

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class SceneChange extends Application {
Scene scene1, scene2;

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
// 场景1
Button button1 = new Button("场景1 的button");

// 事件监听 点击后切换到场景2
button1.setOnMouseClicked(e -> {
primaryStage.setScene(scene2);
});

VBox vBox = new VBox();
vBox.getChildren().add(button1);
scene1 = new Scene(vBox, 400, 400);

// 场景2
Button button2 = new Button("场景2 的button");

// 事件监听 点击后切换到场景1
button2.setOnMouseClicked(e -> {
primaryStage.setScene(scene1);
});

StackPane stackPane = new StackPane();
stackPane.getChildren().add(button2);
scene2 = new Scene(stackPane, 400, 400);

primaryStage.setScene(scene1);
primaryStage.show();
}
}

d22.5.png


d22.6.png


6、窗体切换

Main.java

package com.company;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Main extends Application {
private Stage stage;

@Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;

// 窗口点击叉号关闭询问
stage.setOnCloseRequest(event -> {
event.consume(); // 消除默认事件
handleClose();
});

// 布局
Button button = new Button("关闭窗口");

// 鼠标点击关闭窗口
button.setOnMouseClicked(event -> handleClose());

VBox vBox = new VBox();
vBox.getChildren().add(button);
Scene scene = new Scene(vBox, 400, 400);

stage.setScene(scene);
stage.show();
}

public void handleClose() {
// 接收窗体返回值
boolean ret = WindowAlert.display("关闭窗口", "是否关闭窗口?");
System.out.println(ret);
if (ret) {
stage.close();
}

}

public static void main(String[] args) {
launch(args);
}
}

WindowAlert.java

package com.company;

import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;


public class WindowAlert {
public static boolean answer;

/**
* @param title 标题
* @param msg 消息
*/
public static boolean display(String title, String msg) {
// 创建舞台
Stage stage = new Stage();

// 设置显示模式
stage.initModality(Modality.APPLICATION_MODAL);
stage.setTitle(title);

// 创建控件
Button buttonYes = new Button("是");
buttonYes.setOnMouseClicked(event -> {
answer = true;
stage.close();
});

Button buttonNo = new Button("否");
buttonNo.setOnMouseClicked(event -> {
answer = false;
stage.close();
});

Label label = new Label(msg);

// 创建布局
VBox vBox = new VBox();
vBox.getChildren().addAll(label, buttonYes, buttonNo);
vBox.setAlignment(Pos.CENTER); // 布局居中显示

// 创建场景
Scene scene = new Scene(vBox, 200, 200);

// 显示舞台
stage.setScene(scene);
// stage.show();
stage.showAndWait(); // 等待窗体关闭才继续

// 窗体返回值
return answer;
}
}

d22.7.png

            </div>
目录
相关文章
|
IDE 开发工具
adobe2023全家桶和谐版本全部更新下载教程
盆友们~盆友们~激动的心,颤抖的手Adobe一年一度的重大更新Adobe2023版全新上线重磅更新,强势来袭激不激动!adobe2023系列软件已经正式更新,为您整理了所有adobe2023系列软件合集,包含ps2023、pr2023、ae2023等一系列软件,所有软件都是已经了的,安装完成打开即可免费使用,需要的朋友可以来下载!
1880 0
阿里云商标注册申请官方入口(附商标自助申请流程)
阿里云商标分为商标智能注册申请、商标安心注册申请和商标顾问注册申请
32807 4
阿里云商标注册申请官方入口(附商标自助申请流程)
|
DataWorks 大数据 数据库
云上一指禅:大数据产品DataWorks最佳实践
每天,阿里巴巴集团数万名数据/算法开发工程师正在使用DataWorks,承载阿里巴巴集团99%数据业务构建。
11884 0
云上一指禅:大数据产品DataWorks最佳实践
|
4月前
|
缓存 Java 关系型数据库
Java 面试经验总结与最新 BAT 面试资料整理含核心考点的 Java 面试经验及最新 BAT 面试资料
本文汇总了Java面试经验与BAT等大厂常见面试考点,涵盖心态准备、简历优化、面试技巧及Java基础、多线程、JVM、数据库、框架等核心技术点,并附实际代码示例,助力高效备战Java面试。
173 0
|
11月前
|
人工智能
《AI助力生物学:基因编辑与蛋白质结构解析的加速引擎》
在生物学研究中,AI正发挥重要作用,特别是在基因编辑和蛋白质结构解析方面。AI通过设计新型基因编辑工具(如OpenCRISPR™)、提高编辑效率与精准度(如EVOLVEpro),以及优化整个编辑过程,显著加速了基因编辑的研究进展。在蛋白质结构解析领域,AI技术如AlphaFold实现了精准预测蛋白质三维结构,加速了蛋白质设计与改造,并解析蛋白质相互作用网络。这不仅推动了医学和农业领域的发展,也带来了伦理和法律等挑战,需要确保其健康、可持续发展。
418 11
|
人工智能
【豆包】——猜三国人物——调试过程
【豆包】——猜三国人物——调试过程
788 0
【豆包】——猜三国人物——调试过程
|
存储 缓存 网络架构
计算机网络——三种交换方式(电路交换、分组交换、报文交换以及优缺点)
计算机网络——三种交换方式(电路交换、分组交换、报文交换以及优缺点)
1292 0
|
图形学 Python
使用Python进行曲线拟合:利用贝塞尔曲线
使用Python进行曲线拟合:利用贝塞尔曲线
709 1
|
存储
面试题:计算机内部如何存储负数和浮点数?
面试题:计算机内部如何存储负数和浮点数?
312 0
|
存储 SoC
深入理解AMBA总线(十一)AXI协议导论
深入理解AMBA总线(十一)AXI协议导论
2594 0