亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

擴展類中的另一個方法可以修改標簽嗎?

擴展類中的另一個方法可以修改標簽嗎?

蝴蝶不菲 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 關注
  • 92 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號