Initializable接口,JavaFX加载fxml文件的控件,

JavaFX的窗体文件是fxml格式的xml文件。JavaFX通过FXMLLoader加载fxml文件,并将其进行显示。

fxml文件:





















  
  
    
      
        
          
            
            
            
            
            
            
            
            
            
            
            
            
          
        
        
        
          
            
            
            
            
            
            
            
            
            
            
          
        
        
        
          
            
          
        
      
    
      
        
          
          
                
               
                  
                     

绑定的的Java文件:

package form;


import java.net.URL;
import java.util.ResourceBundle;

import handle.MainFrameHandle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Spinner;
import javafx.scene.control.Tab;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;


/**
 * @copyright 2003-2023
 * @package   form
 * @file      MainFrameController.java
 * @date      2023-12-16
 * @author    qiao wei
 * @version   1.0
 * @brief     MainWindow.fxml文件对应的Java类。
 * @history
 */
public class MainFrameController implements Initializable {
    
    public MainFrameController() {
        mainFrameHandle = MainFrameHandle.getInstance();
    }

    /**
     * @class   MainFrameController
     * @date    2023-12-18
     * @author  qiao wei
     * @version 1.0
     * @brief   初始化,实现Initializable接口。MainFrameController.java与MainWindow.fxml绑定,当fxml文件被FXMLLoader
     *          加载(加载fxml文件时,绑定的控件已经进行初始化)后就自动调用该方法,对已经创建的控件可以进行二次设置。
     * @param   
     * @return  
     * @throws
     */
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        setControlProperty();
    }
    
    private void setControlProperty() {
        openFileItem.setOnAction(event -> mainFrameHandle.chooseFiles());
        quitItem.setOnAction(event -> mainFrameHandle.exitApplication(event));
        
        WebEngine webEngine = trackMapWebView.getEngine();
        webEngine.load("https://toutiao.com/");
         
//        StackPane stackPane = new StackPane();
//        stackPane.getChildren().addAll(webView);
//
//        HBox hBox = new HBox();
//        HBox.setHgrow(webView, Priority.ALWAYS);
//        VBox.setVgrow(webView, Priority.ALWAYS);
//        hBox.getChildren().addAll(webView);
        
//        trackDataMapTab.setContent(hBox);
    }
    
    @FXML
    private Tab trackDataViewTab;
    
    @FXML
    private Tab trackDataMapTab;
    
    @FXML
    private MenuItem openFileItem;
    
    @FXML
    private MenuItem quitItem;
    
    @FXML
    private Spinner minAltitudeSpinner;

    @FXML
    private Spinner maxAltitudeSpinner;
    
    @FXML
    private WebView trackMapWebView;
    
    private MainFrameHandle mainFrameHandle;
}

Initializable接口简介:

Controller initialization interface.

NOTE This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible.

Since:

fxml文件中的JavaFX控件在Java文件中绑定,在Java类的构造函数中还没有初始化,所有的控件还都是null,但是Java类继承了Initializable接口,则在构造函数之后自动调用Initializable接口的方法,此时JavaFX控件已经初始化,可以进行设置了。

你可能感兴趣的:(Java,JavaFX,开发语言,JavaFX,Java)