JAVA问题,从另一个类访问方法

时间:2018-10-07 20:35:56

标签: javafx

你好,我有2个班有问题。首先是带有许多标签的TopMenuIconsController,其次是带有hmmm控制器(类似于MVC)的CheatPaneController。我想在CheatPane中从TopMenu调用refreshGui,但我总是得到Nullpointer

public class CheatPaneController {

@FXML public AnchorPane cheatPane;
@FXML public Label cmdHistory;
@FXML public TextField cmdTextField;

private String appInput;
private ArrayList<String> outPut = new ArrayList<>();
private String[] part = new String[3];

private boolean checkCmd() {
    boolean foo = false;

    switch (part[0]) 

        case "add_cash":
            System.out.println("Cash + " + part[1]);
            outPut.add(appInput);
            Bank.setCash(Bank.getCash() + Double.parseDouble(part[1]));
            new TopMenuIconsController();
            TopMenuIconsController.topMenuIconsController.refreshGui();
            foo = true;
            break;





public class TopMenuIconsController implements Initializable {


@FXML public ImageView avatarTopMenuIcons;
@FXML public Label levelTopMenuIcons;
@FXML public Label cashTopMenuIcons;
@FXML public Label cashInBankTopMenuIcons;
@FXML public Label nameTopMenuIcons;
@FXML public AnchorPane topMenuIcon;
@FXML public ProgressBar expBarTopMenuIcons;
@FXML public ProgressBar energyBarTopMenuIcons;
@FXML public ProgressBar healthBarTopMenuIcons;
@FXML public Label presentExpTopMenuIcons;
@FXML public Label expToNextLevelTopMenuIcons;
@FXML public Label hpTopMenuIcons;
@FXML public Label maxHpTopMenuIcons;
@FXML public Label eneTopMenuIcons;
@FXML public Label maxEneTopMenuIcons;
@FXML public ProgressBar hungerBarTopMenuIcons;
@FXML public Label presentHungerTopMenuItems;


public static TopMenuIconsController topMenuIconsController;

public TopMenuIconsController() {
    this.topMenuIconsController=this;

}


@Override
public void initialize(URL location, ResourceBundle resources) {
    PlayerModel playerModel = new PlayerModel();
    BankModel bankModel = new BankModel();
    StatsModel statsModel = new StatsModel();

    playerModel.loadPlayerFromDB(NamePaneController.counter);
    bankModel.loadBankFromDB(NamePaneController.counter);
    statsModel.loadFromDB(NamePaneController.counter);


    refreshGui();
}

void refreshGui() {
    double foo=0;

    nameTopMenuIcons.setText(Player.getName());
    levelTopMenuIcons.setText(String.valueOf(Player.getLevel()));
    Image image = new Image(String.valueOf(Player.getAvatar()));
    avatarTopMenuIcons.setImage(image);

    cashTopMenuIcons.setText(String.valueOf(Bank.getCash()));
    cashInBankTopMenuIcons.setText(String.valueOf(Bank.getCashInBank()));

    foo=(double) Stats.getHunger()/Stats.getMaxHunger();
    foo=0.63;
    hungerBarTopMenuIcons.setProgress(foo);
    presentHungerTopMenuItems.setText(String.valueOf((int)(foo*100))+"%");

    foo=(double)Stats.getEnergy()/Stats.getMaxEnergy();
    energyBarTopMenuIcons.setProgress(foo);
    eneTopMenuIcons.setText(String.valueOf(Stats.getEnergy()));
    maxEneTopMenuIcons.setText(String.valueOf("/  " + Stats.getMaxEnergy()));

    foo=(double)Stats.getHealth()*100/Stats.getMaxHealth();
    healthBarTopMenuIcons.setProgress(foo);
    hpTopMenuIcons.setText(String.valueOf(Stats.getHealth()));
    maxHpTopMenuIcons.setText(String.valueOf("/  " + Stats.getMaxHealth()));

    foo=(double)Stats.getExperience()*100/Stats.getMaxExperience();
    expBarTopMenuIcons.setProgress(foo);
    presentExpTopMenuIcons.setText(String.valueOf(Stats.getExperience()));
    expToNextLevelTopMenuIcons.setText(String.valueOf("/  " + Stats.getMaxExperience()));
    System.out.println("w8 1");
    try {
        Thread.sleep(2500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("w8 2");

I used article like this

但是我可能思路错误。 你能告诉我我应该怎么做吗?

0 个答案:

没有答案