JavaFx之场景交互(二十一)

有parent、son两个父子窗口,父窗口可以操作子窗口,父子可以相互调用对方的对象,下面我给出两种方案,我推荐使用第二种

一、构造传参

参数比较多的话代码不优雅、而且不太方便维护。

package top.oneit.jdownload.test;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; /**
* @author lingkang
*/
public class MyParent extends Application {
private MySonA sonA;
private Button button; @Override
public void start(Stage primaryStage) throws Exception {
button = new Button("open子窗口A");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (sonA==null){
sonA = new MySonA(primaryStage,button);
sonA.show();// 显示子窗口
}else{
sonA.show();
}
// 调用子接口对象
System.out.println(sonA.getButton().getText());
}
}); Button closeSon = new Button("关闭子窗口");
closeSon.setLayoutY(40);
closeSon.setOnAction(event -> {
sonA.close();
}); AnchorPane anchorPane = new AnchorPane(button, closeSon);
anchorPane.setPrefWidth(400);
anchorPane.setPrefHeight(300);
primaryStage.setScene(new Scene(anchorPane));
primaryStage.show();
} public static void main(String[] args) {
launch(args);
}
}

package top.oneit.jdownload.test;

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage; /**
* @author lingkang
*/
public class MySonA extends Stage {
private Stage parent;
private Button button; public MySonA(Stage parent,Button parentButton) {
this.parent = parent;
Label label = new Label("我是儿子A");
button = new Button("关闭父窗口");
button.setOnAction(event -> {
System.out.println(parent.getTitle());
parent.close();
});
button.setLayoutY(40); Button open = new Button("打开父窗口");
open.setLayoutY(80);
open.setOnAction(event -> {
parentButton.setText("子调用父的对象");// 父子传参,构造方法
parent.show();
}); Button exit = new Button("exit");
exit.setLayoutY(120);
exit.setOnAction(event -> {
System.exit(0);
}); AnchorPane pane = new AnchorPane(label, button, open, exit);
pane.setPrefWidth(300);
pane.setPrefHeight(200);
setScene(new Scene(pane));
} public Button getButton() {
return button;
} public void setButton(Button button) {
this.button = button;
}
}

效果:

二、继承公共类

灵活,注意,创建多个同样的窗口带来的问题,还有获取对应stage为空的问题!提前判空
启动入口

import javafx.application.Application;
import javafx.stage.Stage; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; /**
* @author lingkang
* @date 2021/11/18
*/
public class MyApp extends Application {
public static ConcurrentMap<String, Stage> stages = new ConcurrentHashMap<>(); @Override
public void start(Stage primaryStage) throws Exception {
new MyParent();//显示父窗口
} public static void main(String[] args) {
launch(args);
}
}

公共类

import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.stage.WindowEvent; /**
* @author lingkang
* @date 2021/11/18
*/
public class MyStageCommon extends Stage {
private String thisClassName=getClass().getName();//将当前类名单独初始化
public MyStageCommon() {
super();
// 创建窗口时加入
MyApp.stages.put(thisClassName, this);
// x 掉窗口时将它移除
setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
MyApp.stages.remove(thisClassName);
}
});
} @Override
public void close() {
super.close();
MyApp.stages.remove(thisClassName);
} /**
* 获取对象,,注意返回空值
*/
public <T> T getStage(Class<T> clazz) {
if (!MyApp.stages.containsKey(clazz.getName()))
return null;
return (T) MyApp.stages.get(clazz.getName());
}
}

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane; /**
* @author lingkang
*/
public class MyParent extends MyStageCommon {
public Button button; public MyParent() {
setTitle("父窗口!");
button = new Button("open子窗口A");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (getStage(MySonA.class) == null) {
new MySonA().show();// 显示子窗口
} else {
getStage(MySonA.class).show();
}
// 调用子接口对象
System.out.println(getStage(MySonA.class).button.getText());
}
}); Button closeSon = new Button("关闭子窗口");
closeSon.setLayoutY(40);
closeSon.setOnAction(event -> {
if (getStage(MySonA.class) != null)
getStage(MySonA.class).close();
}); AnchorPane anchorPane = new AnchorPane(button, closeSon);
anchorPane.setPrefWidth(400);
anchorPane.setPrefHeight(300);
setScene(new Scene(anchorPane));
show();
}
}

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane; import java.util.Date; /**
* @author lingkang
*/
public class MySonA extends MyStageCommon {
public Button button; public MySonA() {
setTitle("子窗口A");
MyParent parent = getStage(MyParent.class);
Label label = new Label("我是儿子A");
button = new Button("关闭父窗口");
button.setOnAction(event -> {
System.out.println(parent.getTitle());
parent.close();
});
button.setLayoutY(40); Button open = new Button("打开父窗口");
open.setLayoutY(80);
open.setOnAction(event -> {
if (parent.button != null)
parent.button.setText("子调用父的对象" + new Date());// 父子传参
parent.show();
}); Button exit = new Button("exit");
exit.setLayoutY(120);
exit.setOnAction(event -> {
System.exit(0);
}); AnchorPane pane = new AnchorPane(label, button, open, exit);
pane.setPrefWidth(300);
pane.setPrefHeight(200);
setScene(new Scene(pane));
}
}

效果:

JavaFx之场景交互(二十一)的更多相关文章

  1. WCF技术剖析之二十一:WCF基本异常处理模式[中篇]

    原文:WCF技术剖析之二十一:WCF基本异常处理模式[中篇] 通过WCF基本的异常处理模式[上篇], 我们知道了:在默认的情况下,服务端在执行某个服务操作时抛出的异常(在这里指非FaultExcept ...

  2. 中介者模式 调停者 Mediator 行为型 设计模式(二十一)

      中介者模式(Mediator)   调度.调停   意图 用一个中介对象(中介者)来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散 而且可以独立地改变它们之间的交互. ...

  3. WCF技术剖析之二十一:WCF基本异常处理模式[下篇]

    原文:WCF技术剖析之二十一:WCF基本异常处理模式[下篇] 从FaultContractAttribute的定义我们可以看出,该特性可以在同一个目标对象上面多次应用(AllowMultiple = ...

  4. WCF技术剖析之二十一: WCF基本的异常处理模式[上篇]

    原文:WCF技术剖析之二十一: WCF基本的异常处理模式[上篇] 由于WCF采用.NET托管语言(C#和NET)作为其主要的编程语言,注定以了基于WCF的编程方式不可能很复杂.同时,WCF设计的一个目 ...

  5. iOS 11开发教程(二十一)iOS11应用视图美化按钮之实现按钮的响应(1)

    iOS 11开发教程(二十一)iOS11应用视图美化按钮之实现按钮的响应(1) 按钮主要是实现用户交互的,即实现响应.按钮实现响应的方式可以根据添加按钮的不同分为两种:一种是编辑界面添加按钮实现的响应 ...

  6. Senparc.Weixin.MP SDK 微信公众平台开发教程(二十一):在小程序中使用 WebSocket (.NET Core)

    本文将介绍如何在 .NET Core 环境下,借助 SignalR 在小程序内使用 WebSocket.关于 WebSocket 和 SignalR 的基础理论知识不在这里展开,已经有足够的参考资料, ...

  7. 学习笔记:CentOS7学习之二十一: 条件测试语句和if流程控制语句的使用

    目录 学习笔记:CentOS7学习之二十一: 条件测试语句和if流程控制语句的使用 21.1 read命令键盘读取变量的值 21.1.1 read常用见用法及参数 21.2 流程控制语句if 21.2 ...

  8. Django笔记二十一之使用原生SQL查询数据库

    本文首发于公众号:Hunter后端 原文链接:Django笔记二十一之使用原生SQL查询数据库 Django 提供了两种方式来执行原生 SQL 代码. 一种是使用 raw() 函数,一种是 使用 co ...

  9. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

  10. Bootstrap <基础二十一>徽章(Badges)

    Bootstrap 徽章(Badges).徽章与标签相似,主要的区别在于徽章的边角更加圆滑. 徽章(Badges)主要用于突出显示新的或未读的项.如需使用徽章,只需要把 <span class= ...

随机推荐

  1. Hosts映射

    Hosts映射 思考:如何通过主机名能够找到(ping)某个linux系统? 前言 Hosts:是一个文本文件,用来记录IP和Hostname的映射关系 DNS:是互联网上作为域名和IP地址映射的一个 ...

  2. 文心一言 VS 讯飞星火 VS chatgpt (106)-- 算法导论10.1 4题

    四.用go语言,重写 ENQUEUE 和 DEQUEUE 的代码,使之能处理队列的下溢和上溢. 文心一言: 在 Go 语言中,我们可以使用切片来实现队列.下面是如何实现 ENQUEUE 和 DEQUE ...

  3. MPL协议原文

    MPL协议原文 原文参考链接 中文翻译有一个,但是只翻译了两条 原文 Mozilla Public License Version 2.0 1. Definitions 1.1. "Cont ...

  4. JS中的caller属性

    JS中的caller属性: 这个属性会返回一个调用该函数对象的外层函数引用.也就是说,如果我们是在函数B()中调用函数A()的,那么只要在A()中调用A.caller,结果就会返回B()functio ...

  5. 【matplotlib 实战】--雷达图

    雷达图(Radar Chart),也被称为蛛网图或星型图,是一种用于可视化多个变量之间关系的图表形式.雷达图是一种显示多变量数据的图形方法.通常从同一中心点开始等角度间隔地射出三个以上的轴,每个轴代表 ...

  6. PTA1030完美数列二分法解决超时

    #include"bits/stdc++.h" using namespace std; const int N=100010; long long ans,n,p; long l ...

  7. Servlet--HttpServlet实现doGet和doPost请求的原理(转)

    Servlet(Server Applet):全称Java Servlet.是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. 狭义的Servlet是指 Jav ...

  8. 基于C# Socket实现的简单的Redis客户端

    前言 Redis是一款强大的高性能键值存储数据库,也是目前NOSQL中最流行比较流行的一款数据库,它在广泛的应用场景中扮演着至关重要的角色,包括但不限于缓存.消息队列.会话存储等.在本文中,我们将介绍 ...

  9. vue-router钩子执行顺序

    Vue的路由在执行跳转时,根据源码可知,调用了router中定义的navigate函数 function push(to: RouteLocationRaw) { return pushWithRed ...

  10. offscreenCanvas+worker+IndexedDB实现无感大量图片缓存

    一个有必要实现的需求 因为项目中需要使用canvasTexture(一个threejs3d引擎中的材质类型),绘制大量的图片,每次使用都会请求大量的oss图片资源,虽然重复请求会有磁盘缓存但毕竟这个磁 ...