前阶段的Java FX更改标签 [英] Java FX change label in previous stage

查看:133
本文介绍了前阶段的Java FX更改标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序(我只想了解控制器切换机制)。第一个窗口显示标签和按钮。当您单击按钮时,将显示另一个带有按钮的窗口。现在,当您单击第二个按钮时,第一个窗口中的标签应该会更改。我在这里阅读了一些帖子,也尝试过此 Java FX更改了前一个场景中的Label文本,但是没有成功。如果您可以在这个简单的示例中向我解释它,也许我将能够更好地理解控制器逻辑。这是我的代码,感谢您的帮助:

I have pretty simple application (i just want to understand controller switching mechanism). First window shows label and button. When you click the button, another window with button will show. Now when you click this second button, Label in first window should change. I have read some posts here, also tried this one Java FX change Label text in a previous stage scene, however, with no success. If you could explain it to me on this simple example, maybe i will be able to better understand controllers logic. Here is my code, thank for any help:

PrimaryController,其标签将被更改,并带有按钮,这将打开新窗口

PrimaryController with label to be changed and the button, which opens new window

public class PrimaryController implements Initializable {

@FXML
private Label label;

@FXML
private Button btnButton;

@Override
public void initialize(URL url, ResourceBundle rb) {
    btnButton.setOnAction((event) -> {

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("secondaryFXML.fxml"));
            Parent root = loader.load();
            loader.<SecondaryController>getController().setPrimaryController(this);
            Stage stage = new Stage();
            Scene scene = new Scene(root);

            stage.setScene(scene);
            stage.show();
        } catch (IOException ex) {
            Logger.getLogger(PrimaryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    });        
}    

public void setLabelText(String string){
    label.setText(string);
}

}

具有更改标签按钮的辅助控制器

Secondary Controller with change label button

public class SecondaryController implements Initializable {

@FXML
private Button btnChangeLabel;

private PrimaryController primary;

@Override
public void initialize(URL url, ResourceBundle rb) {

    btnChangeLabel.setOnAction((event) -> {            
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("primaryFXML.fxml"));
            loader.load();
            PrimaryController primaryCtrl = loader.<PrimaryController>getController();
            primaryCtrl.setLabelText("eres");

        } catch (IOException ex) {
            Logger.getLogger(SecondaryController.class.getName()).log(Level.SEVERE, null, ex);
        }
    });
}

public void setPrimaryController(PrimaryController primary){
    this.primary = primary;
}

}

推荐答案

在按钮的动作事件处理程序中,您将再次加载FXML文件(创建在那里定义的所有控件的新实例)并获取新UI的控制器。然后,在该UI的控制器上调用 setLabelText(...),更改新创建的标签中的文本。您从未显示过从FXML文件加载的UI,因此不会产生可见效果。

In the button's action event handler, you're loading the FXML file again (creating new instances of all the controls defined there) and getting the controller for the new UI. Then you call setLabelText(...) on the controller for that UI, changing the text in the newly-created label. You haven't ever displayed the UI you loaded from the FXML file, so this will have no visible effect.

当然,您实际上并不需要新的实例所有控件中:您(大概)想更改已经显示的标签的文本。您已经将对现有 PrimaryController 实例的引用传递给 SecondaryController 实例,因此只需调用 setLabelText(...)

Of course, you don't actually want a new instance of all your controls: you (presumably) want to change the text of the label that is already displayed. You have already passed a reference to the existing PrimaryController instance to the SecondaryController instance, so just call setLabelText(...) on that:

@Override
public void initialize(URL url, ResourceBundle rb) {
    btnChangeLabel.setOnAction((event) ->             
        primary.setLabelText("eres"));
}

这篇关于前阶段的Java FX更改标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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