JavaFx 打开一个新窗口和窗口交互(四)

JavaFX 从入门入门到入土系列
前面我演示的demo都是单个窗口,那么如何实现多个窗口呢?使用Stage secondStage = new Stage();然后secondStage.show();展示即可。

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.stage.Stage; /**
* @author lingkang
* @date 2021/9/17 22:29
* @description
*/
public class WindowsDemo extends Application {
public void start(Stage stage) throws Exception {
stage.setTitle("标题");
stage.getIcons().add(new Image("img/avatar.jpg"));
stage.setHeight(200);
stage.setWidth(300); Button button = new Button("打开窗口");
// 添加一个点击事件
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// 创建新的stage
Stage newStage = new Stage();
newStage.setWidth(300);
newStage.setHeight(200);
Label label = new Label("新窗口");
newStage.setScene(new Scene(label));
// 显示
newStage.show();
}
});
// 直接添加样式
button.setStyle("-fx-background-color: red;-fx-text-fill: blue;");
stage.setScene(new Scene(button));
stage.show();
} public static void main(String[] args) {
launch(args);
}
}

效果如下

那么他们怎么交互呢?

简单关闭第一个窗口可以在显示第二个窗口时调用 stage.close();关闭第一个窗口第一个窗口

Button button = new Button("打开窗口");
// 添加一个点击事件
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// 创建新的stage
Stage newStage = new Stage();
newStage.setWidth(300);
newStage.setHeight(200);
Label label = new Label("新窗口");
newStage.setScene(new Scene(label));
// 显示
newStage.show();
// 关闭第一个窗口
stage.close();
}
});

窗口间交互

窗口间交互通过公共静态map进行,后面的文章我会用RxJava异步事件实现。

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.VBox;
import javafx.stage.Stage; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; /**
* @author lingkang
* @date 2021/9/17 22:29
* @description
*/
public class WindowsDemo extends Application { public static ConcurrentMap<String, Stage> stageManage = new ConcurrentHashMap<>(); public void start(Stage stage) throws Exception {
stage.setTitle("标题");
stage.getIcons().add(new Image("img/avatar.jpg"));
stage.setHeight(200);
stage.setWidth(300); Button button = new Button("打开窗口");
// 添加一个点击事件
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// 创建新的stage
Stage newStage = new Stage();
newStage.setWidth(300);
newStage.setHeight(200);
Label label = new Label("新窗口");
newStage.setScene(new Scene(label));
// 显示
newStage.show();
// 关闭第一个窗口
//stage.close(); // 放到静态map中共用,从而达到状态管理
stageManage.put("second", newStage);
}
}); Button closeNewStage = new Button("关闭第二个窗口");
closeNewStage.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
// 通过共用的map,关闭第二个窗口
stageManage.get("second").close();
}
}); // 直接添加样式
button.setStyle("-fx-background-color: red;-fx-text-fill: blue;");
VBox vBox = new VBox();
vBox.getChildren().add(button);
vBox.getChildren().add(closeNewStage);
stage.setScene(new Scene(vBox));
stage.show();
// 放到静态map中共用,从而达到状态管理
stageManage.put("first", stage);
} public static void main(String[] args) {
launch(args);
}
}

效果

JavaFx 打开一个新窗口和窗口交互(四)的更多相关文章

  1. windbg学习---.browse打开一个新的command 窗口

    .browse r eax .browse <command>将会显示新的命令浏览窗口和运行给出的命令

  2. Window.open 实现导航与打开窗口,导航到一个特定链接地址,也可以打开一个新的浏览器窗体

    语法 window.open(strUrl,strWindowName,strWindowFeatures ,replace) strUrl: 打开资源的地址 strWindowName: 表示窗体名 ...

  3. JS 点击元素发ajax请求 打开一个新窗口

    JS 点击元素发ajax请求 打开一个新窗口 经常在项目中会碰到这样的需求,点击某个元素后,需要发ajax请求,请求成功以后,开发需要把链接传给前端(或者说请求成功后打开新窗口),前端需要通过新窗口打 ...

  4. vue中使用router打开一个新的窗口

    一个单页应用打开一个新的窗口不是很好控制,比如权限的处理,因为原先的页面不会自动刷新,方法很简单: let routeData = this.$router.resolve({ name: " ...

  5. 使用javascript打开一个新页而不被浏览器屏蔽

    使用javascript打开一个新页面可以有几种方式,但各有利弊,以下做下分析 1.window.open(url) 这是新手最常用的方法,好处是简单易用,坏处,很简单,会被很多浏览器拦截而导致功能失 ...

  6. 在做爬虫或者自动化测试时新打开一个新标签页,必须使用windows切换

    在做爬虫或者自动化测试时,有时会打开一个新的标签页或者新的窗口,直接使用xpath定位元素会发现找不到元素,在firefox中定位了元素还是找不到, 经过多次发现,在眼睛视野内看到这个窗口是在最前面, ...

  7. Virtualbox中不能为虚拟机打开一个新任务的原因及解决方法

    VirtualBox新建虚拟机时报错,不能为虚拟机打开一个新任务的原因 解决办法如下 1.保证bios里的virtualization technology的选项开启,不同电脑BIOS设置可能会不一样 ...

  8. Android课程---Oracle VM VirtualBox出现不能为虚拟机打开一个新任务

    因工作需要在Win7下增添了Win7虚拟系统,随着VirtualBox 4.326的版本更新,用户们也开始升级.一用户在升级后发现原来创建的虚拟机无法打开,提示信息为:不能为虚拟电脑win7打开一个新 ...

  9. VirtualBox不能为虚拟电脑 Ubuntu 打开一个新任务

    今天在用Vbox中的Ubuntu系统准备测试Python代码时,Vbox报了一个错误:"不能为虚拟电脑 Ubuntu 打开一个新任务".因为之前用的时候还好好的,也不知道是不是最近 ...

  10. 不能为虚拟电脑 ubuntu 打开一个新任务.

    使用virtualbox报错: 不能为虚拟电脑 ubuntu3 打开一个新任务. The virtual machine 'ubuntu3' has terminated unexpectedly d ...

随机推荐

  1. warning in ./src/router/index.js (Emitted value instead of an instance of Error) Error compiling template: Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'jsoninfo'

    目录 warning in ./src/router/index.js (Emitted value instead of an instance of Error) Error compiling ...

  2. div 让a内容居中方法

    <div>标签是HTML中的一个重要标签,它代表了一个文档中的一个分割区块或一个部分.在<div>标签中,我们可以放置各种内容,包括文本.图像.链接等等.有时候,我们需要将其中 ...

  3. JS逆向实战23 某市wss URL加密+请求头+ws收发

    声明 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 本文首发链接为: http ...

  4. 11g编译bbed

    报错如下: make -f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed Linking BBED utility (bbed) rm -f /u01/app/or ...

  5. idea修改默认maven配置

    idea修改默认maven配置 方法一 (不推荐) 打开project.default.xml文件,在其中加入如下几行配置. 代码如下 保存修改之后新建一个maven项目查看效果 方法二 新增Proj ...

  6. 记一次服务器Cuda驱动崩溃修复过程

    基本过程 今天实验室师兄在服务器运行深度学习训练时候得到报错CUDA initialization: Unexpected error from cudaGetDeviceCount()疑似Cuda与 ...

  7. 关于长链剖分的数组实现 | CF1009F Dominant Indices

    请容许我不理解一下为什么这题题解几乎全都是指针实现/kk 其实长链剖分是可以直接用数组来写的. 考虑朴素 DP.设 \(f_{u,i}\) 表示以点 \(u\) 为根的子树中与点 \(u\) 距离为 ...

  8. 云端golang开发,无需本地配置,能上网就能开发和运行

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 需求 学习golang的时候,需要一个IDE,还需要一 ...

  9. codeforce 827div4

    第一次在codeforce上打题,补一下题记录成长 D题 分析:求数组中两个互质的数的最大下标和: 思路:观察到数据范围n是2e5暴力做n^2会超时,再观察数据a[i]最大为1000,所以这2e5个数 ...

  10. ResNet详解:网络结构解读与PyTorch实现教程

    本文深入探讨了深度残差网络(ResNet)的核心概念和架构组成.我们从深度学习和梯度消失问题入手,逐一解析了残差块.初始卷积层.残差块组.全局平均池化和全连接层的作用和优点.文章还包含使用PyTorc ...