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

问题描述 投票:2回答:1

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

我有两节课:

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

现在,如果我点击食物按钮,则subtotalCounter和Label正确刷新:Notice the right-bottom Label "Subtotal"

Consolle Value

但是当我点击座位时,电影的价格被正确添加但Label变为“null”并给我一个NullPointerException。

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

enter image description here enter image description here

enter image description here

首先我认为问题是在nullPointException上所以我检查了this问题。然后我试着理解扩展类as thiscan是否调用Label并对其进行修改,答案似乎是肯定的但是由于可怕的原因似乎并不适用于我的场景。

感谢大家。

java javafx label extends
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

接下来是在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命名约定,处理代码复制我看到了很多代码可以像我在上面的方法中给你看到的那样泛化

祝好先生好运代码。如果它无法正常工作,我可以发布完整的课程,但我认为你应该尝试将其弄清楚作为其良好做法,我给了你所需的所有代码,如果你需要更多的帮助,请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.