麒麟正青春

 

javafx中controller与fxml及参数传值-4

 来自于: https://www.bilibili.com/video/BV1tt4y1W7nY?spm_id_from=333.788.videopod.sections&vd_source=137b7db2f57c3cb63d8af247fefc66e0

 一、去掉controller及其应用场景

 二、controller有参构造器

    javafx中controller与fxml及参数传值-1-2

三、多controller,多stage通信与传值

   javafx中controller与fxml及参数传值-3

四、事件总线eventbus

创建javafx项目,通过maven管理

1、pom.xml中引入依赖,谷歌的事件总线插件guava 

<!-- 通过谷歌的事件总线插件guava,实现控制器间参数传递,发送者传递内容给事件总线,事件总线再传递内容给订阅者-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.4.0-jre</version>
</dependency>

2、定义事件总线为单例模式

public class EventBusUtil {
//单例模式
private final static EventBus EVENT_BUS =new EventBus();//创建一个EventBus事件总线对象
//获取EventBus对象
public static EventBus getDefault(){
return EVENT_BUS;
}
private EventBusUtil(){

}
}

3、消息封装类,Dir、MoveEvent、ResultEvent

// 消息封装类,矩形的移动方向的枚举类
public enum Dir {
// 矩形的移动方向
UP,
DOWN,
LEFT,
RIGHT
}

//消息封装类,移动方向和移动距离的类
public class MoveEvent {
private Dir dir;
private double distance;
public MoveEvent() {}
public MoveEvent(Dir dir, double distance) {
this.dir = dir;
this.distance = distance;
}

public Dir getDir() {
return dir;
}

public void setDir(Dir dir) {
this.dir = dir;
}

public double getDistance() {
return distance;
}

public void setDistance(double distance) {
this.distance = distance;
}
}

//消息封装类,处理结果的消息类
public class EventResult {
private String result;
public EventResult() {
}
public EventResult(String result) {
this.result = result;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}

4、模块引入类:module-info.java

odule com.wyrjgs.eventbus {
requires javafx.controls;
requires javafx.fxml;
requires com.google.common;


opens com.wyrjgs.eventbus to javafx.fxml;
exports com.wyrjgs.eventbus;
exports com.wyrjgs.eventbus.fxcontroller;
opens com.wyrjgs.eventbus.fxcontroller to javafx.fxml;
}

5、应用类

public class HelloApplication extends Application {
//通过谷歌的事件总线插件guava,实现控制器间参数传递,发送者传递内容给事件总线,事件总线再传递内容给订阅者
@Override
public void start(Stage stage) throws IOException {
// FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("/fxml/main-view.fxml"));
// Parent root = FXMLLoader.load(getClass().getResource("/fxml/main-view.fxml"));
// stage.setScene(new Scene(root));
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/main-view.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}

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

6、主控制器类

public class MainController {
@FXML
private Locale locale;
@FXML
ResourceBundle resourceBundle;
@FXML
void initialize(){}
// 使用构造函数,注册为订阅者
public MainController()
{
EventBusUtil.getDefault().register(this);
}
// 订阅者处理消息的方法,处理 EventResult消息
@Subscribe
public void resultHandler(EventResult eventResult)
{
System.out.println("移动处理结果"+eventResult.getResult());
}

}

//不建议使用实现Initializable接口
//public class MainController implements Initializable {
// @Override
// public void initialize(URL url, ResourceBundle resourceBundle) {
//
// }
// }

7、中心控制器类


public class CenterController{
@FXML
private Locale locale;
@FXML
ResourceBundle resourceBundle;
@FXML
Rectangle rect;
@FXML
void initialize(){}
@FXML
void onSendInfoAction() {
//1、发送者发送位置信息,发送者发送一个参数,接收者接收到一个参数,参数类型相同,
//如果多个参数可以封装到一个类中,也可以使用Map集合封装多个参数
EventBusUtil.getDefault().post("位置信息:"+rect.getLayoutX()+"\t"+rect.getLayoutY());//发送消息给事件总线,发送了一个 String 类型消息
}
//2、通过构造函数,注册为订阅者
public CenterController() {
EventBusUtil.getDefault().register(this);//注册为订阅者
}
//3、消息处理的方法,关于MoveEvent的消息,对消息处理
@Subscribe
public void moveRectHandler(MoveEvent moveEvent) {
//3、接收者接收到消息,关于MoveEvent的消息,对消息处理
// 接收者接收到一个参数,参数类型相同,如果多个参数可以封装到一个类中,也可以使用Map集合封装多个参数
System.out.println("移动");
Dir dir = moveEvent.getDir();
// switch (dir) {
// case UP:
// rect.setLayoutY(rect.getLayoutY() + moveEvent.getDistance());
// break;
// case DOWN:
// rect.setLayoutY(rect.getLayoutY() + moveEvent.getDistance());
// break;
// case LEFT:
// rect.setLayoutX(rect.getLayoutX() + moveEvent.getDistance());
// break;
// case RIGHT:
// rect.setLayoutX(rect.getLayoutX() + moveEvent.getDistance());
// }
if((dir==Dir.UP)||(dir==Dir.DOWN))
{
rect.setLayoutY(rect.getLayoutY() + moveEvent.getDistance());
}
else
{
rect.setLayoutX(rect.getLayoutX() + moveEvent.getDistance());
}
//4、对接收消息的处理结果,发送给事件总线,发送一个EventResult类型的消息。相当于接收消息后返回一个消息
EventBusUtil.getDefault().post(new EventResult("移动完成"));//发送消息给事件总线,发送了一个 EventResult 类型消息

}

}
//不建议使用实现Initializable接口
//public class CenterController implements Initializable {
// @Override
// public void initialize(URL url, ResourceBundle resourceBundle) {
//
// }
// }

8、底部控制器类


public class BottomController {
@FXML
private Locale locale;
@FXML
ResourceBundle resourceBundle;
@FXML
Label labelInfo;

@FXML
void initialize(){}
private MoveEvent moveEvent= new MoveEvent();
@FXML
void onMoveDown(ActionEvent event) {
moveEvent.setDir(Dir.DOWN);
moveEvent.setDistance(5);
EventBusUtil.getDefault().post(moveEvent);//发送消息给事件总线,发送了一个 MoveEvent 类型消息
}
@FXML
void onMoveLeft(ActionEvent event) {
moveEvent.setDir(Dir.LEFT);
moveEvent.setDistance(-5);
EventBusUtil.getDefault().post(moveEvent);//发送消息给事件总线,发送了一个
}
@FXML
void onMoveRight(ActionEvent event) {
moveEvent.setDir(Dir.RIGHT);
moveEvent.setDistance(5);
EventBusUtil.getDefault().post(moveEvent);//发送消息给事件总线,发送了一个 MoveEvent 类型消息
}
@FXML
void onMoveUp(ActionEvent event) {
moveEvent.setDir(Dir.UP);
moveEvent.setDistance(-5);
EventBusUtil.getDefault().post(moveEvent);//发送消息给事件总线,发送了一个 MoveEvent 类型消息
//发送者发送一个参数,接收者接收到一个参数,参数类型相同,
//如果多个参数可以封装到一个类中,也可以使用Map集合封装多个参数
}

//3、标识为订阅者消息处理方法,处理String类型的消息,通过注解 @Subscribe
@Subscribe
public void setInfo(String info)
{
//发送者发送一个参数,接收者接收到一个参数,参数类型相同,
//如果多个参数可以封装到一个类中,也可以使用Map集合封装多个参数
labelInfo.setText(info);
System.out.println("BottomController收到消息:"+info);
}

//2、通过构造函数,注册为订阅者
public BottomController() {
EventBusUtil.getDefault().register(this);
}
}

//不建议使用实现Initializable接口
//public class BottomController implements Initializable {
// @Override
// public void initialize(URL url, ResourceBundle resourceBundle) {
//
// }
// }

9、main-view.fxml


<BorderPane prefHeight="771.0" prefWidth="717.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.wyrjgs.eventbus.fxcontroller.MainController">
<center>
<fx:include source="center-view.fxml" />
</center>
<bottom>
<fx:include source="bottom-view.fxml" />
</bottom>
</BorderPane>

10、center-view.fxml

<Pane prefHeight="505.0" prefWidth="607.0" style="-fx-border-color: #808080; -fx-border-width: 3;" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.wyrjgs.eventbus.fxcontroller.CenterController">
<children>
<Rectangle fx:id="rect" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="52.0" layoutX="276.0" layoutY="227.0" stroke="BLACK" strokeType="INSIDE" width="55.0" />
<Button fx:id="sendBtn" layoutX="260.0" layoutY="461.0" mnemonicParsing="false" onAction="#onSendInfoAction" text="发送位置信息" />
</children>
</Pane>

11、bottom-view.fxml

<AnchorPane prefHeight="165.0" prefWidth="625.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.wyrjgs.eventbus.fxcontroller.BottomController">
<children>
<Button layoutX="345.0" layoutY="74.0" mnemonicParsing="false" onAction="#onMoveRight" text="右" />
<Button layoutX="228.0" layoutY="74.0" mnemonicParsing="false" onAction="#onMoveLeft" text="左" />
<Button layoutX="288.0" layoutY="108.0" mnemonicParsing="false" onAction="#onMoveDown" text="下" />
<Button layoutX="288.0" layoutY="32.0" mnemonicParsing="false" onAction="#onMoveUp" text="上" />
<Label fx:id="labelInfo" layoutX="10.0" layoutY="144.0" prefHeight="15.0" prefWidth="557.0" text="位置信息" />
</children>
</AnchorPane>

12、运行效果

 

posted on 2025-03-10 15:12  麒麟正青春  阅读(51)  评论(0)    收藏  举报

导航