JavaFx introductory tutorial (2)

I have been busy recently. My colleague introduced JavaFx and studied it, and wrote a demo of the basic class of the component interface (javafx interface components use css style)
github source address: https://github.com/rain86/JavaFxBaseDemo

When I first learned JavaFx, I encountered how to implement interface jumping,
and there are many interface modules that are reused. How to realize the random combination of different modules in the interface. The following code directly solves the problem.

There are actually only two interfaces in the demo below,
one is the login interface LoginController.java, and the
other is the main interface MainController.java. (Other modules are randomly combined to form different main interfaces)

Hospital.java is the entry point of the program. It inherits Application and implements the main method.
Inheriting Application will implement the start method, which is to create a new open interface.

/**
 *
 * 程序入口
 * @author mile
 */
public class Hospital extends Application {

    private Stage stage;
    private MainController main;


    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        LoginController login = (LoginController) replaceSceneContent(Constants.LOGIN_PAGE);
        this.stage.show();
        login.setApp(this);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(Hospital.class, (java.lang.String[])null);
    }


    /**
     * 替换界面并返回相应的控制类
     */
    private Initializable replaceSceneContent(String fxml) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = Hospital.class.getResourceAsStream(fxml);
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(Hospital.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, 1000, 500);
        System.out.print("replaceSceneContent");
        stage.setScene(scene);
        stage.setTitle("Hospital");
        stage.sizeToScene();
        return (Initializable) loader.getController();
    }

    /**
     * 登陆成功后进入主页面
     */
    public void gotoMain(String[] strs) {
        try {
            main = (MainController) replaceSceneContent(Constants.MAIN_PAGE);//替换界面
            main.setApp(this);//给controller传入application
            main.loadLayout(strs);//给主界面中加载需要模块 strs 是模块集合
        } catch (Exception ex) {
            Logger.getLogger(Hospital.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 重绘主界面
     */
    public void reMain(String[] strs){
        main.loadLayout(strs);
    }

The main interface has different modules and various combinations of modules. So in MainController.java, we implement the loadLayout method to display the interface of different module combinations according to the parameters passed.

/**
 * FXML Controller class
 * 主界面:登陆成功后进入该界面,
 *
 * @author mile
 */
public class MainController implements Initializable,setApp {

    private Hospital application;
    @FXML
    VBox main_border;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @Override
    public void setApp(Hospital application) {
        this.application = application;
    }

    /**
     * 根据传进来的模块重新绘制主界面:
     * 拿到main_border清空子元素,然后根据数据挨个把模块添加进来并重新绘制
     */
    void loadLayout(String[] strs) {
        main_border.getChildren().clear();
        for (String str : strs) {
            URL layout = this.application.getClass().getResource(str);
            if (layout != null) {
                try {
                    FXMLLoader loader = new FXMLLoader();
                    InputStream in = Hospital.class.getResourceAsStream(str);
                    loader.setBuilderFactory(new JavaFXBuilderFactory());
                    loader.setLocation(Hospital.class.getResource(str));
                    Node page;
                    try {
                        page = (Node) loader.load(in);
                    } finally {
                        in.close();
                    }
                    main_border.getChildren().add(page);
                    setApp setapp = (setApp)loader.getController();
                    setapp.setApp(this.application);
                } catch (IOException ex) {
                    Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

All control classes must implement this interface, you can use the hospital's redraw main interface method and the application method

package hospital;

/**
 *
 * @author mile
 * 每个控制类都要实现这个接口,让他有application的引用,从而控制其他地方
 */
public interface setApp {
    public void setApp(Hospital application);
}

For example, the control class of the menu module below

/**
 * FXML Controller class
 * 头部的总菜单
 * @author mile
 */
public class TopMenuController implements Initializable ,setApp{

    private Hospital application;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @Override
    public void setApp(Hospital application) {
        this.application = application;
    }

    @FXML
    private void gotoDelete(ActionEvent event) {
        try {
            String[] strs = new String[1];
            strs[0] = Constants.TOP_MENU;
            application.gotoMain(strs);
        } catch (Exception ex) {
            Logger.getLogger(QueryMenuController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @FXML
    private void gotoConsultingManagement(ActionEvent event) {
        try {
            String[] strs = new String[3];
            strs[0] = Constants.TOP_MENU;
            strs[1] = Constants.QUERY_MENU;
            strs[2] = Constants.CONSULTING_MANAGEMENT_BODY;
            application.reMain(strs);
        } catch (Exception ex) {
            Logger.getLogger(QueryMenuController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    @FXML
    private void gotoexit(ActionEvent event) {
        System.exit(0);
    }

}

You can directly download the source code and run it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325713608&siteId=291194637