JavaFx之不通过全局静态变量进行窗体通信

时间:2022-02-23 14:19:17

百度了n多窗体通信,,,总是通过定义全局静态变量进行传值通信。。我个人不喜欢一个controller里写满所有的布局(这样显得臃肿,但是组件传值方便)。有没有另外的办法进行模块化并且可以传值呢。。

肯定是有的。。。

1.定义一个泛型类接收Controller对象与Stage对象

 public class StageAndController<C, S> {
private C controller;
private S stage; public StageAndController(C controller, S stage) {
this.controller = controller;
this.stage = stage;
}
public StageAndController(C controller) {
this.controller = controller;
} public C getController() {
return controller;
} public void setController(C controller) {
this.controller = controller;
} public S getStage() {
return stage;
} public void setStage(S stage) {
this.stage = stage;
}
}

这里之所以返回两个对象,,首先每个controller都对应一个打开的布局窗体,controller用来传值、赋值、初始化操作,stage本身需要调用show()才显示,,所以定义此类

2.封装 打开一个窗体或者动态加载一个Node

 public static StageAndController addMenu(final Pane pane, final String fxmlName, Object controller) {
URL location = AddOperation.class.getResource("/fxml/" + fxmlName);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
try {
Node node = fxmlLoader.load();//返回Node对象 Node是布局*父类,再之上就是Object,所有此处可以加载一个布局添加到父级中
pane.getChildren().add(node);//Node添加到父级布局
controller = fxmlLoader.getController();//获取加载布局的Contrller对象
} catch (IOException e) {
e.printStackTrace();
}
return new StageAndController(controller);//返回controller实例
}
//pane->需要添加node的父级Pane及Pane子类对象  fxmlName-->自定义的布局文件   Object-->需要实例化的Controller

这里我的用法是动态增加一排菜单栏,使用了到了controller传值作用,用法如下:

StageAndController controller = AddOperation.addMenu(operation, "read_item.fxml", ReadCardController.class);
readCardController = (ReadCardController) controller.getController();

向父级窗体暴露了一个Controller实例对象,就可以进行父窗体向子组件传值,初始化。

-------------------------------------------------------------------------------------------------------

  public static StageAndController newDialog(String fxmlName, String title, Object controller) {
URL location = OpenDialog.class.getResource("/fxml/" + fxmlName);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Stage stage = new Stage();
try {
Pane dialogPane = fxmlLoader.load();
stage.setScene(new Scene(dialogPane));
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.DECORATED);
stage.setResizable(false);
if (title != null) {
stage.setTitle(title);
}
controller = fxmlLoader.getController(); } catch (IOException e) {
e.printStackTrace();
}
return new StageAndController(controller, stage);

此处同理,加载的是一个窗体布局,同时设置模态化(本身未关闭,父级窗体不可点击)、动态标题,,,,,返回一个Controller与Stage对象,,,用法如下:

 StageAndController stageAndController = OpenDialog.newDialog("connectDevice.fxml", "连接", ConnectDevice.class);
ConnectDevice controller = (ConnectDevice) stageAndController.getController();
Stage stage = (Stage) stageAndController.getStage();
controller.str= str;
controller.init();
stage.show();

窗体显示之前父窗体向子窗体传值或者做一些初始化。 这样模块化布局传值也变得简单好维护。

-------------------------------------------------------------------------------------------------------

嫌弃自带窗体风格太丑,,可以往下看。。

自定义Stage窗体布局(State设置透明时):

public static StageAndController customDialog(String fxmlName, Object controller, Pane pane, String css) {
URL location = OpenDialog.class.getResource("/fxml/" + fxmlName);
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
Stage stage = new Stage();
try {
pane = fxmlLoader.load();
Background background = new Background(new BackgroundFill(Paint.valueOf("#EFEFEF"), new CornerRadii(15), new Insets(0)));
pane.setBackground(background);
pane.setBorder(new Border(new BorderStroke(Color.valueOf("#0096C9"), BorderStrokeStyle.SOLID, new CornerRadii(15), new BorderWidths(2))));
Scene scene = new Scene(pane);
if (css != null) {
scene.getStylesheets().add(OpenDialog.class.getResource("/css/" + css).toExternalForm());
}
scene.setFill(Paint.valueOf("#FFFFFF00"));//设置场景透明 stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setResizable(false);
stage.setScene(scene);
controller = fxmlLoader.getController(); } catch (IOException e) {
e.printStackTrace();
}
return new StageAndController(controller, stage);
}

用法差不多,加了一个自定义加载css样式,可以按需更改,,不过自定义时,,窗体头部拖动就不可用,下面贴出解决方案:

 public class DragListener implements EventHandler<MouseEvent> {

     private double xOffset = 0;
private double yOffset = 0;
private final Stage stage; public DragListener(Stage stage) {
this.stage = stage;
} @Override
public void handle(MouseEvent event) {
event.consume();
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
xOffset = event.getSceneX();
yOffset = event.getSceneY();
} else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
stage.setX(event.getScreenX() - xOffset);
if(event.getScreenY() - yOffset < 0) {
stage.setY(0);
}else {
stage.setY(event.getScreenY() - yOffset);
}
}
} public void enableDrag(Node node) {
node.setOnMousePressed(this);
node.setOnMouseDragged(this);
}
}

用法如下:

   StageAndController stageAndController = OpenDialog.customDialog("connectDevice_cus.fxml",ConnectDevice.class,new Pane(),null);
ConnectDevice controller = (ConnectDevice) stageAndController.getController();
Stage stage = (Stage) stageAndController.getStage();
new DragListener(stage).enableDrag(controller.title);//自定义窗口时设置拖动监听 此处controller.title是我自定义窗体标题栏的HBox对象
controller.stage = stage;
controller.init();
stage.show();

这样就可以愉快的使用自定义弹窗了。。。。

-------------------------------------------------------------------------------------------------------

不喜勿喷!!!! 欢迎进群学习交流(927465926)