JavaFX - 不同控制器中按钮和文本字段之间的通信不起作用

时间:2015-12-16 21:33:18

标签: java eclipse javafx

我的桌面应用程序(诊所)出了问题,这里是RegistrationController,它负责输入有关患者的数据,点击“添加”按钮后 - 这些数据将转移到另一个控制器,称为InfoPatientController(使用FXML文件,显示有关患者的输入信息),对于测试我设置在其他窗口中单击“添加”后出现文本“asdf”,但它不起作用,这里是代码和屏幕:

java.lang.RuntimeException:java.lang.reflect.InvocationTargetException - 出现这些错误

我创建了MainController,但我不知道如何管理一个类中的所有控制器。是否有可能以我的方式管理它?

http://imgur.com/a/Xqbfv

这里是RegistrationPatientController:

vm.model.$update(function () {
    $state.go(userStates.app);
});

} 这是InfoPatientController:

public class RegistrationPatientController implements Initializable {

@FXML
Button registrationButton, infopatientButton, checkoutButton, medicalstaffButton, usersButton, logoutButton,
        exitButton;

@FXML
TextField PIDField, nameField, lastNameField, genderField, ageField, phonenumberField, adressField, diseaseField,
        IDField, dataField, conditionField, priceField;

@FXML
RadioButton normalRoom, vipRoom;

@FXML
MenuButton roomMenuField;

@FXML
MenuItem room101, room102, room103, room104, room105, room106, room107, room108;

@FXML
Button confirmButton, closeButton, addpatientButton;

ToggleGroup choiceRoom = new ToggleGroup();

@FXML
InfoPatientController infoPatientController;

Stage stage;
BorderPane root;

boolean start = true;

String name;
String lastname;
String gender;
String age;
String phonenumber;
String adress;
String disease;
String condition;
String roomtype;

@FXML
public void enterInfoAboutPatient(ActionEvent event) {

}

@FXML
public void roomChoiceAndCalculatePrice(ActionEvent event) {

    normalRoom.setToggleGroup(choiceRoom);
    vipRoom.setToggleGroup(choiceRoom);
    if (choiceRoom.equals(normalRoom)) {
        normalRoom.isSelected();

    }
    if (choiceRoom.equals(vipRoom)) {
        vipRoom.isSelected();
    }

}

@FXML
public void roomNumberChoice(ActionEvent event) {
    String numberRoom101 = "101";
    String numberRoom102 = "102";
    String numberRoom103 = "103";
    String numberRoom104 = "104";
    String numberRoom105 = "105";
    String numberRoom106 = "106";
    String numberRoom107 = "107";
    String numberRoom108 = "108";
    String firstpriceNormalRoom = "30";
    String secondpriceNormalRoom = "40";
    String firstpriceVipRoom = "80";
    String secondpriceVipRoom = "120";

    if (normalRoom.isSelected()) {
        if (event.getSource() == room101) {
            roomMenuField.setText(numberRoom101);
            priceField.setText(firstpriceNormalRoom);
            return;
        }

        if (event.getSource() == room102) {
            roomMenuField.setText(numberRoom102);
            priceField.setText(firstpriceNormalRoom);
            return;
        }

        if (event.getSource() == room103) {
            roomMenuField.setText(numberRoom103);
            priceField.setText(secondpriceNormalRoom);
            return;
        }

        if (event.getSource() == room104) {
            roomMenuField.setText(numberRoom104);
            priceField.setText(secondpriceNormalRoom);
            return;
        }

        if (event.getSource() == room105) {
            roomMenuField.setText(numberRoom105);
            priceField.setText(firstpriceNormalRoom);
            return;
        }

        if (event.getSource() == room106) {
            roomMenuField.setText(numberRoom106);
            priceField.setText(firstpriceNormalRoom);
            return;
        }

        if (event.getSource() == room107) {
            roomMenuField.setText(numberRoom107);
            priceField.setText(firstpriceNormalRoom);
            return;
        }

        if (event.getSource() == room108) {
            roomMenuField.setText(numberRoom108);
            priceField.setText(secondpriceNormalRoom);
            return;
        }
    }

    if (vipRoom.isSelected()) {
        if (event.getSource() == room101) {
            roomMenuField.setText(numberRoom101);
            priceField.setText(firstpriceVipRoom);
            return;
        }

        if (event.getSource() == room102) {
            roomMenuField.setText(numberRoom102);
            priceField.setText(firstpriceVipRoom);
            return;
        }

        if (event.getSource() == room103) {
            roomMenuField.setText(numberRoom103);
            priceField.setText(secondpriceVipRoom);
            return;
        }

        if (event.getSource() == room104) {
            roomMenuField.setText(numberRoom104);
            priceField.setText(secondpriceVipRoom);
            return;
        }

        if (event.getSource() == room105) {
            roomMenuField.setText(numberRoom105);
            priceField.setText(firstpriceVipRoom);
            return;
        }

        if (event.getSource() == room106) {
            roomMenuField.setText(numberRoom106);
            priceField.setText(firstpriceVipRoom);
            return;
        }

        if (event.getSource() == room107) {
            roomMenuField.setText(numberRoom107);
            priceField.setText(firstpriceVipRoom);
            return;
        }

        if (event.getSource() == room108) {
            roomMenuField.setText(numberRoom108);
            priceField.setText(secondpriceVipRoom);
            return;
        }

    }

}

@FXML
public void handleAddPatient(ActionEvent event) {

    // name = nameField.getText();
    // lastname = lastNameField.getText();
    // gender = genderField.getText();
    // age = ageField.getText();
    // phonenumber = phonenumberField.getText();
    // adress = adressField.getText();
    // disease = diseaseField.getText();
    // condition = conditionField.getText();

    infoPatientController.infoageField.setText("asdf");

    // boolean enterCondition = !nameField.getText().isEmpty() &&
    // !lastNameField.getText().isEmpty()
    /// && !genderField.getText().isEmpty() && !ageField.getText().isEmpty()
    // && !phonenumberField.getText().isEmpty() &&
    // !adressField.getText().isEmpty()
    // && !diseaseField.getText().isEmpty() &&
    // !conditionField.getText().isEmpty()
    // && (normalRoom.isSelected() || vipRoom.isSelected());
    //
    // boolean choiceRoomCondition = (event.getSource() == room101) ||
    // (event.getSource() == room102)
    // || (event.getSource() == room103) || (event.getSource() == room104)
    // || (event.getSource() == room105)
    // || (event.getSource() == room106) || (event.getSource() == room107)
    // || (event.getSource() == room108);

    // if(enterCondition&choiceRoomCondition)
    // {
    // EntityManagerFactory entityManagerFactory =
    // Persistence.createEntityManagerFactory("test");
    // EntityManager entityManager =
    // entityManagerFactory.createEntityManager();

    // Patient patient = new Patient();
    // patient.setID(001);
    // patient.setFirstName(name);
    // patient.setLastName(lastname);
    // patient.setGender(gender);
    // patient.setAge(age);
    // patient.setPhonenumber(phonenumber);
    // patient.setAdress(adress);
    // patient.setDisease(disease);
    // patient.setCondition(condition);

    // entityManager.getTransaction().begin();
    // entityManager.persist(patient);
    // entityManager.getTransaction().commit();
    //
    // entityManager.close();
    // entityManagerFactory.close();
    // }

}

@FXML
public void closeRegistration(ActionEvent event) throws IOException {
    root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));
    stage = (Stage) registrationButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
}

@FXML
public void chooseOption(ActionEvent event) throws IOException {

    if (event.getSource() == registrationButton) {
        root = FXMLLoader.load(getClass().getResource("/view/RegistrationPatient.fxml"));
        stage = (Stage) registrationButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 750, 530));
        stage.show();
        return;
    }

    if (event.getSource() == infopatientButton) {
        root = FXMLLoader.load(getClass().getResource("/view/InfoPatient.fxml"));
        stage = (Stage) infopatientButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 750, 530));
        stage.show();
        return;
    }

    if (event.getSource() == checkoutButton) {
        root = FXMLLoader.load(getClass().getResource("/view/CheckOutPatient.fxml"));
        stage = (Stage) checkoutButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 750, 530));
        stage.show();
        return;
    }

    if (event.getSource() == medicalstaffButton) {
        root = FXMLLoader.load(getClass().getResource("/view/MedicalStaff.fxml"));
        stage = (Stage) medicalstaffButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 750, 530));
        stage.show();
        return;
    }

    if (event.getSource() == usersButton) {
        root = FXMLLoader.load(getClass().getResource("/view/Users.fxml"));
        stage = (Stage) usersButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 750, 530));
        stage.show();
        return;
    }

    if (event.getSource() == infopatientButton) {
        root = FXMLLoader.load(getClass().getResource("/view/InfoPatient.fxml"));
        stage = (Stage) infopatientButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 750, 530));
        stage.show();
        return;
    }

    if (event.getSource() == logoutButton) {
        root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
        stage = (Stage) logoutButton.getScene().getWindow();
        stage.setTitle("Przychodnia");
        stage.setScene(new Scene(root, 280, 280));
        stage.show();
        return;
    }

    if (event.getSource() == exitButton) {
        stage = (Stage) exitButton.getScene().getWindow();
        stage.close();
    }
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    PIDField.setEditable(false);
    dataField.setEditable(false);
    IDField.setEditable(false);
    priceField.setEditable(false);

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
    Date date = new Date();
    dataField.setText((dateFormat.format(date)));

}

这是错误:

public class InfoPatientController implements Initializable {

@FXML
Button registrationButton, infopatientButton, checkoutButton, medicalstaffButton, usersButton,
logoutButton, exitButton;

@FXML
TextField infoPIDField, infonameField, infolastNameField, infogenderField, infoageField,
infophonenumberField, infoadressField, infodiseaseField, infoconditionField, infodataField,
infoRoomNumber, infoRoomType;

@FXML
ListView<String> patientList;

@FXML
Button confirmButton, editButton, closeButton;


@FXML
RegistrationPatientController registrationPatientController;

Stage stage;
BorderPane root;    

boolean start = true;

private MainController main;



  @FXML
  public void enteredInfoAboutPatient(ActionEvent event)
 {

 }

 @FXML
 public void optionsAction(ActionEvent event)
 {

 }

@FXML
public void chooseOption(ActionEvent event) throws IOException 
{

    if(event.getSource()==registrationButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/RegistrationPatient.fxml"));
    stage = (Stage) registrationButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
    return;
    }


    if (event.getSource()==infopatientButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/InfoPatient.fxml"));
    stage = (Stage) infopatientButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
    return;
    }

    if(event.getSource()==checkoutButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/CheckOutPatient.fxml"));
    stage = (Stage) checkoutButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
    return;
    }


    if (event.getSource()==medicalstaffButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/MedicalStaff.fxml"));
    stage = (Stage) medicalstaffButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
    return;
    }

    if(event.getSource()==usersButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/Users.fxml"));
    stage = (Stage) usersButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
    return;
    }


    if (event.getSource()==infopatientButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/InfoPatient.fxml"));
    stage = (Stage) infopatientButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 750, 530));
    stage.show();
    return;
    }

    if(event.getSource()==logoutButton)
    {
    root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
    stage = (Stage) logoutButton.getScene().getWindow();
    stage.setTitle("Przychodnia");
    stage.setScene(new Scene(root, 280, 280));
    stage.show();
    return;
    }


    if (event.getSource()==exitButton)
    {
    stage = (Stage) exitButton.getScene().getWindow();
    stage.close();
    }
 }

@Override
public void initialize(URL location, ResourceBundle resources) {



 }

RegistrationController中的行nr 221:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at 
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 49 more
Caused by: java.lang.NullPointerException
at         controller.RegistrationPatientController.handleAddPatient(RegistrationPatientController.java:221)
... 58 more

这是RegistrationPatient.FXML:

infoPatientController.infoageField.setText("asdf");

0 个答案:

没有答案