JavaFx 中的选项卡与场景构建器之间的通信

communication betwenn Tabs in JavaFx ans Scene builder

我制作了一个 mainEntredController,其中包含一个带有两个选项卡(HomeController)和(PatientController)的 TabPane,MainEntred 控制着这两个选项卡之间的参数通道,但我有一个问题,即选项卡不会重新调整父级(MainEntredController)或彼此。它返回此行的错误 *** labelpatient.setText(mainec.LoadtxtFromlabelhometabhome()); ****** 这个想法是从第一个选项卡中获取文本以在第二个选项卡的标签中显示它

听到我的代码 第一个是 MainEntred Class Container

public class MainEntredController  {
@FXML MenuItem closemi;
@FXML  HomeController homecontroller;
@FXML  PatientController patientcontroller;


 public void initialize(URL location, ResourceBundle resources) {
    System.out.println("Application Started");
homecontroller.intit(this);
patientcontroller.init(this);

}



public void ClickMenuItemClose(){
    System.exit(0);
}


public String LoadtxtFromlabelhometabhome() {
//  System.out.println(homecontroller.labelhome.getText());
    return  homecontroller.labelhome.getText();
}

public void setTabpatientlabel(String text) {
    //patientcontroller.labelpatient.setText(text);

}

}

第二个是第一个选项卡 HomeController

public class 家庭控制器 {

public MainEntredController mainec;

@FXML public Label labelhome;
@FXML private TextField tfhome;
@FXML private Button savehome;
@FXML private Button sendhome;
//public HomeController(){}

 public void btnSaveHomeClicked(ActionEvent event){
System.out.println("le boutton save home est cliqué");
labelhome.setText(tfhome.getText());
 }
public void btnSendHomeClicked(ActionEvent event){
System.out.println("le boutton send home est cliqué");
// send data to tab patient
 mainec.setTabpatientlabel(labelhome.getText());
  }

public void intit(MainEntredController mainEntredController) {
mainec = mainEntredController;
}

}

第三个是第二个选项卡 PatientController

 public class PatientController {

 public MainEntredController mainec;

@FXML public Label labelpatient;
@FXML private TextField tfpatient;
@FXML private Button savepatient;
@FXML private Button loadpatient;


  public void btnSavePatientClicked(ActionEvent event){
System.out.println("le boutton save Patient est cliqué");
labelpatient.setText(tfpatient.getText());
   }

   public void btnLoadPatientClicked(ActionEvent event){
System.out.println("le boutton Load patient est cliqué");
labelpatient.setText(mainec.LoadtxtFromlabelhometabhome());
   }

 public void init(MainEntredController mainEntredController) {
  mainec = mainEntredController;
  }
 }

以及 MainEntred 的 FXML 文件

<fx:include id="homecontroller" source="home.fxml" />
<fx:include id="patientcontroller" source="patient.fxml" />

我猜那两个问题很烦请教如何解决 我不知道为什么我在声明的那一行有 java.lang.NullPointerException 他无法获得来源(来源不明)

如果我理解正确,那么你想在控制器之间进行通信。好吧,这可以很容易地完成。

在你的 main 中输入如下内容:

FXMLLoader loader = new FXMLLoader(Main.class.getResource("your.fxml"));
Parent node = loader.load(); // this loads view 
YourController instance = loader.getController();
some_class.initialize(instance);

我终于找到问题所在了。问题是,在两个选项卡的 MainController 的声明中,我们将 fx:id 声明在 fxml 文件中 并添加单词 "Controller"

像这样

    @FXML private HomeController homeController;
    @FXML  private PatientController patientController;

fx:id="home" 第二个包括 fx:id="patient"