在fxml文件之间切换 [英] Switch between fxml files

查看:161
本文介绍了在fxml文件之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在swing组件中使用jfxPanel创建了一个应用程序。我面临的问题是我无法更改fxml文件。当单击fxml的按钮时,我想处理该fxml并在那里加载另一个fxml文件。
这是我到目前为止所做的事情

I have created an application using jfxPanel inside swing component. The problem I am facing is I am not being able to change the fxml files. When a button of the fxml is clicked, I want to dispose that fxml and load another fxml file there. This is what I have done till now

public class NonResponsiveButtons extends JFrame {
    NonResponsiveButtons nrb;
    BottomPanelIncomingController bpic;
    JPanel panel; 
    JPanel bPanel;

    private int applicationWidth_600 = 600;
    private int applicationHeight_600 = 600;

    private int upperPanelHeight_535 = 535;
    private int bottomPanelHeight_65 = (applicationHeight_600-upperPanelHeight_535); 


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



    public NonResponsiveButtons(){
        final JFXPanel fxPanel = new JFXPanel();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
            // TODO Auto-generated method stub
                try{
                    new JFXPanel();
                    add(getJPanel(), BorderLayout.CENTER);
                    add(getJBottomPanel(), BorderLayout.PAGE_END);
                    bPanel.add(fxPanel, BorderLayout.CENTER);

                    Platform.runLater((new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        initFX(fxPanel);    
                    }
                })) ;

            }
                catch(Exception e){
                    System.out.println("Error in swing utilities thread :" + e.getMessage());
            }

        }
    });

    this.setSize(applicationWidth_600, applicationHeight_600);
    setLocationRelativeTo(null);
    BorderLayout borderLayout = new BorderLayout();
    setLayout(borderLayout);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    setVisible(true);
}


private void initFX(JFXPanel jfxPanel) {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("fxmlDesign.fxml"));
        Parent root = (Parent) fxmlLoader.load();
        Scene scene = new Scene(root, 600, 65);
        jfxPanel.setScene(scene);

        fxmlLoader.getController();
        bpic = new BottomPanelIncomingController();
        bpic.setNonResoinsiveButtons(this);

    } catch (IOException exc) {
        exc.printStackTrace();
        System.exit(1);
    }
}

public void loadSecondFxml(){
    System.out.println("loading second fxml");
}   

private JPanel getJPanel(){
    if(panel == null){
    panel = new JPanel();
    panel.setSize(applicationWidth_600,upperPanelHeight_535);
    panel.setBackground(Color.gray);
    }
    return panel;
}

private JPanel getJBottomPanel(){
    if(bPanel == null){
        bPanel = new JPanel();
        bPanel.setSize(applicationWidth_600, bottomPanelHeight_65);
        bPanel.setBackground(new Color(8, 16, 19));
    }
    return bPanel;
}   

}

BottomPanelIncommingController类。 fxml控制器

The BottomPanelIncommingController class. The fxml controller

public class BottomPanelIncomingController implements Initializable {
 NonResponsiveButtons nrb;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
    // TODO Auto-generated method stub
    System.out.println("HEllo");
    }

    public void setNonResoinsiveButtons(NonResponsiveButtons nrb){
    this.nrb = nrb;
    }

    @FXML
    public void callAccepted(ActionEvent event){
    System.out.println("From controller");
    nrb.loadSecondFxml();
    }
 }


推荐答案

你在使用FXML时,不应该使用 new 来实例化控制器,因为实例化控制器的作业是由FXMLLoader完成的。在实例化控制器时,它还会创建FXML中存在的节点实例并将它们注入控制器。

You should never instantiate the controller with new when you are using FXML because the job to instantiate the controller is done by the FXMLLoader. While instantiating the controller, it also creates instances of the nodes which are there in the FXML and inject them into the controller.

如果没有从控制器获取控制器实例FMXLLoader,控制器内所有用 @FXML 注释的节点都为空。因此,您必须始终将控制器从fxml中取出。

If you do not get the controller instance from the FMXLLoader, all your nodes inside the controller which are annotated with @FXML eill be null. Therefore, you must always get the Controller out of the fxml.

在您的情况下,您应该使用

In your case, you should use


bpic = fxmlLoader.getController();

bpic = fxmlLoader.getController();

而不是


bpic = new BottomPanelIncomingController();

bpic = new BottomPanelIncomingController();

更新

点击按钮更改FXML

让我们考虑点击按钮时调用以下方法

Let us consider the following method is called on Button click

@FXML
public void callAccepted(ActionEvent event){
    System.out.println("From controller");
    nrb.loadSecondFxml();
}

您可以加载FXML并将其设置在场景上然后再转到 JFXPanel

You can load the FXML and set it on a Scene and then to the JFXPanel

public void loadSecondFxml(){
    //Load new FXML and assign it to scene
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("newFXML.fxml"));
    Parent root = (Parent) fxmlLoader.load();
    Scene scene = new Scene(root, 600, 65);
    jfxPanel.setScene(scene);
} 

注意:我不确定你是什么试图在这里实现,考虑这只是点击一个按钮加载FXML并应用你的逻辑。

Note : I am not sure what you are trying to achieve here, consider this as an example just to load FXML on click of a button and apply your logic.

这篇关于在fxml文件之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆