扩展类中的另一个方法可以修改标签吗?

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


我有两节课:


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();

  } 

}

另一个扩展了 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();

      }

    }

  } 

这里奇怪的是,如果我再次单击任何小吃,标签会正确显示所选食物的价格以及所选每个座位的电影价格。


蝴蝶不菲
浏览 75回答 1
1回答

心有法竹

所以有很多错误(夸大效果),但你正在学习,所以我会尽力提供帮助。请在复制并粘贴到您的程序之前阅读全部内容,以便您理解它从你得到一个空指针的原因开始,你有一个事实,extend DashBoardUserController这意味着DashBoardUserController程序初始化时没有与你交谈TheatresController,当你尝试访问它时,它给了你一个空指针,因为没有初始化在扩展类所以第 1 步删除extend DashBoardUserController因此,既然它已经消失了,我们需要获取DashBoardUserController对该类的引用,以便它可以调用必要的方法,我们可以在初始化时TheatresController这样做        FXMLLoader loader = new FXMLLoader(getClass().getResource(resourceLocation));        Pane pane = loader.load();        TheatresController controller = loader.getController();        controller.setDashBoardUserControllerReference(this);//We'll get here接下来是在中制作必要的方法和变量TheatresControllerprivate DashboardUserController dashboardUserController;//Set at top of classpublic void setDashBoardUserControllerReference(DashboardUserController dashboardUserController){    this.dashboardUserController = dashboardUserController;}完成这将修复空指针移动以生成您的方法,因为有很多代码复制对您来说是不必要的输入@FXMLpublic 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 命名约定,处理代码复制祝好先生好运代码。如果它不起作用,我可以发布完整的课程,但我认为您应该尝试将其找出为它的好习惯,我给了您所有您需要的代码,如果您需要更多帮助,请告诉我
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java