为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

蝴蝶不菲 2022-07-20 20:33:04
我正在尝试创建一个允许用户购买门票和小吃的电影院预订系统,并在每次点击时刷新标签。我有两节课: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();      }    }  } 这里奇怪的是,如果我再次单击任何小吃,标签会正确显示所选食物的价格以及所选每个座位的电影价格。
查看完整描述

1 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

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


从你得到一个空指针的原因开始,你有一个事实,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

接下来是在中制作必要的方法和变量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 命名约定,处理代码复制


祝好先生好运代码。如果它不起作用,我可以发布完整的课程,但我认为您应该尝试将其找出为它的好习惯,我给了您所有您需要的代码,如果您需要更多帮助,请告诉我


查看完整回答
反对 回复 2022-07-20
  • 1 回答
  • 0 关注
  • 74 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信