【问题标题】:Could a Label be modified by another method inside an extended class?扩展类中的另一个方法可以修改标签吗?
【发布时间】:2019-03-05 15:07:07
【问题描述】:

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

我有两门课:

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

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

【问题讨论】:

  • 请创建一个MCVE。总而言之,它是一个最小(少量代码)完整(完全可运行)可验证(我可以放入我的 IDE 并运行没有错误)示例(重新创建您的问题)。
  • 在这里我可以澄清一下,通过重新创建您的问题,我的意思是以最小的方式重写必要的代码,以便堆栈溢出的其他人可以在不需要链接代码的 IDE 中运行您的代码,但是将其嵌入到帖子中,您可以执行此操作,但是如果您发现无法始终如一地重新创建问题,那么将代码缩进 4 个空格,就像您已经完成的那样,那么代码存在更大的问题,我们看不到(我不知道) t 想调试你的整个应用程序)据说也许通过调试器来帮助你找到你想要重新创建的问题
  • 不幸的是...
  • @Matt 如果我给你发了一个包含整个代码的链接,你能检查一下吗?
  • @Matt 谢谢。 ufile.io/pt728

标签: java javafx label extends


【解决方案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命名约定,处理代码复制我看到了大量可以像我在上面的方法中展示的那样泛化的代码

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

【讨论】:

  • 如果此答案满足您的问题,请单击问题旁边的复选以“接受”答案,以免其他人认为此问题仍然未解决
  • 好的,应用刚才的更改,非常感谢你,如果一切正常,我会立即接受这个问题。
  • 非常感谢您的帮助。我仔细阅读了您关注的所有内容。显然我还需要练习,我确信我可以使用关键字“扩展”来调用这些方法。现在一切正常。
  • 不用担心,从容应对,边走边学,不懂的时候寻求帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2021-05-26
  • 2012-09-30
  • 1970-01-01
相关资源
最近更新 更多