是否可以通过扩展 class 中的另一种方法修改标签?

Could a Label be modified by another method inside an extended class?

我正在尝试创建一个电影院预订系统,允许用户购买门票和小吃,并在每次点击时刷新标签。

我有两个 classes:

public class DashboardUserController{

  public Label subtotal;
  public static Double subtotalCounter = 0.0;
  public static Double filmPrice;

  @FXML
  public void onWaterBottleClick(){
    subtotalCounter = subtotalCounter + 1.50;
    orderRecipt = orderRecipt + "Water Bottle 1.50£ \n";
    setSubtotal();
  }
  // and similar methods for each type of snack 

public void setSubtotal(){
    subtotal.setText(subtotalCounter.toString() + " £");
  }

// set the price for each click on the label

  public void addTicket() {
    System.out.println(subtotalCounter);
    subtotalCounter = subtotalCounter + filmPrice;
    setSubtotal();
  } 
}

另一个 class 扩展了 DashboardUserController

public class TheatresController extends DashboardUserController {

  @FXML
  private void onClick(MouseEvent event) {                  //method for selection/deselection of seats
    ImageView imageView = (ImageView) event.getSource();
    if (imageView.getImage() == redSeat) {
      Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you want to proceed with the unbooking?", ButtonType.YES, ButtonType.NO);
      if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
        imageView.setImage(imageView.getImage() == redSeat ? greenSeat : redSeat);
      }
    } else {
      Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Do you want to proceed with the booking?", ButtonType.YES, ButtonType.NO);
      if (alert.showAndWait().orElse(ButtonType.NO) == ButtonType.YES) {
        imageView.setImage(imageView.getImage() == redSeat ? greenSeat : redSeat);
        addTicket();
      }
    }
  } 

奇怪的是,如果我再次单击任何零食,标签会正确显示所选食品的价格加上所选每个座位的电影价格。

所以有很多错误(夸大效果)但是你正在学习所以我会尽力帮助。请在复制并粘贴到您的程序之前阅读所有内容,以便您理解它

开始你得到空指针的原因是因为你有 extend DashBoardUserController 这意味着 DashBoardUserController 程序初始化时没有与你的 TheatresController 并且当您尝试访问它时它给了您一个空指针,因为扩展 class

中没有初始化任何内容

所以第 1 步删除 extend DashBoardUserController

因此,既然它已经消失了,我们需要获得 DashBoardUserController 对 class 的引用,以便它可以调用必要的方法,我们可以在 TheatresController 的初始化时执行此操作,例如所以

        FXMLLoader loader = new FXMLLoader(getClass().getResource(resourceLocation));
        Pane pane = loader.load();
        TheatresController controller = loader.getController();
        controller.setDashBoardUserControllerReference(this);//We'll get here

接下来是在TheatresController

中制作必要的方法和变量
private DashboardUserController dashboardUserController;//Set at top of class

public void setDashBoardUserControllerReference(DashboardUserController dashboardUserController){
    this.dashboardUserController = dashboardUserController;
}

完成此操作将修复空指针,继续生成您的方法,因为有很多代码复制对您来说是不必要的输入

@FXML
public void onjosephclick() {
    //weekPickerInput();                                //You can delete from here
    //addFilmsInList();
    //filmList.setShowLocation("Joseph P");
    //System.out.println("joseph button pressed");
    //try {
    //    Pane pane = FXMLLoader.load(getClass().getResource("scenes/josephpane.fxml"));
    //    seatsSelection.getChildren().setAll(pane);
    //} catch (IOException e) {
    //    System.out.println(e);
    //}
    //setStuff();
    //seatsavailable.setText(Integer.toString(seatsJoseph));
    //filmPrice = filmList.searchFilm().get().getPrice();
    //System.out.println("Price:"+filmPrice);            //Down to here
    genericOnClick("Joseph P","scenes/josephpane.fxml",seatsJoseph);
    //change the strings for each call and remove the unnecessary stuff 
    //in the methods it will clean it up a lot
}

private void genericOnClick(String location, String resourceLocation, int seats) {
    weekPickerInput();
    addFilmsInList();
    filmList.setShowLocation(location);
    System.out.println("Location:"+location);
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource(resourceLocation));
        Pane pane = loader.load();
        TheatresController controller = loader.getController();
        controller.setDashBoardUserControllerReference(this);
        seatsSelection.getChildren().setAll(pane);
    } catch (IOException e) {
        System.out.println(e);
    }
    setStuff();
    seatsavailable.setText(Integer.toString(seats));
    filmPrice = filmList.searchFilm().get().getPrice();
    System.out.println("Price:"+filmPrice);
}

最终要学习的其他一些东西是 java 命名约定,处理代码复制我看到了大量可以像我在上面的方法中向您展示的那样被泛化的代码

祝你好运,先生。如果它不起作用,我可以 post 完整 class 但我认为你应该尝试把它弄清楚作为它的良好做法,我给了你所有你需要的代码,如果你需要更多帮助,请告诉我