让我们说您在MainWindow中有标签。
,您想更改此标签的值 从Window2
不在同一个窗口中!
我要在MainWindow打开时进行更改
答案 0 :(得分:-1)
如果您使用2个带有自己控制器的.fxml文件,这很容易 如果是这种情况,请创建一个新的类文件,即“ AllControllers”
您有两个2个控制器,分别是ControllerWindow1和ControllerWindow2
public class AllControllers {
private static ControllerWindow1 control1;
private static ControllerWindow2 control2;
public static ControllerWindow1 getControl1() {
return control1;
}
public static void setControl1(ControllerWindow1 control1) {
Controlls.control1 = control1;
}
public static ControllerWindow2 getControl2() {
return control2;
}
public static void setControl2(ControllerWindow2 control2) {
Controlls.control2 = control2;
}
}
您必须像这样初始化每个控制器
public class ControllerWindow1 implements Initializable{
@FXML
public Label mylabel;
@Override
public void initialize(URL location, ResourceBundle resources) {
AllControllers.setControl1(this);
}
}
现在,您可以从任何类访问控制器。只需使用
AllControllers.getControl1().mylabel.setText("hello");