javaFX写写公司内部程序还行;

父类传递子类 设置信息,调用方法

常见的第一种 代码调用

new FXMLLoader.getController子窗口传值

@FXML
private void closee(ActionEvent event){controller.closeWindow2();}
//时间传值按钮
    @FXML
    private void  timeTO(ActionEvent event){
        controller.kkk.setText(LocalTime.now().toString());//调用子窗口方法
    }
    Test2 controller;//子窗口类
    //Button 按钮
    @FXML
    private  void click(ActionEvent event) throws IOException {
    //主要 初始化子类
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/test2.fxml"));
        Pane load1 = fxmlLoader.load();
        // 加载器实例中获取控制器 Test2 类
         controller = fxmlLoader.getController();
        controller.setMsg("hello word");//调用子窗口 方法
        Scene scene = new Scene(load1);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("我是子窗口");
        stage.show();
    }

!!!注意不要在FXMLLoader上使用静态加载功能,否则您将无法从加载器实例中获取控制器.
**
在这里插入图片描述

第二种 fxml形式调用

** fxml 页面 父调用子类 父类fxml内引用子FXML 例如

   <fx:include source="test2.fxml" fx:id="test_window"></fx:include>

!!!注意 fx:id 必须的 ; 注意id 名称 test_window 与下方关联
父级页面 引用

@FXML
Test2 test_windowController;  //test2.fxml 指定的控制器 Test2 类 

注意 成员名称 分为两部分组成 test_window 与 Controller
test_window 就是FX:id 名 ; 后缀 必须加上 Controller字段名
最后就是可以用 test_windowController 调用 引入窗口的方法,传参

在这里插入图片描述

子类 调用 父类-设置信息,方法

父类继承的Initializable 类 在重写方法中 调用 子类的公有方法 传递 父类

//**父类方法**
@FXML
Test2 test_windowController; //<fx:include source="test2.fxml" fx:id="test_window"></fx:include> // 父类FXML引入的子类 __ 父类 声明子类 test_windowController由 java fxml内部识别自动注入;
@Override
    public void initialize(URL location, ResourceBundle resources) {
		 //调用 子类方法 传递 父类;
		 test_windowController.setMainController(this);
    }
 -----------------------------------------------------
    
//**class Test2  类 方法**
    private AppController appController; //声明父类
   // 子类方法接收 父类、、
    void setMainController(AppController appController){
     this.appController=appController;
     }
AppController appController 就可以调用父类成员方法,成员属性

FXml 如何关闭自身
fxml中 找到顶级标签 例如

@FXML
AnchorPane  root2;

 Scene scene = root2.getScene();
    Stage window = (Stage) scene.getWindow();
    window.close();

B站视频讲解的UP主 LeeWyatt

B站视频基础讲解的 Aimls

参考网址
javafx中文网 https://openjfx.cn/
那些遇到过的问题 https://qa.1r1g.com/sf/ask/tagged/fxml/
csdn博客 https://blog.csdn.net/harcrance/article/details/82351697
易百教程javafx教程

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐