有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从不同类访问方法

我试图从另一个类调用一个类的void。对象工作正常,但由于某些原因,它无法完成操作

我正在做一个黑杰克游戏,当用户输入正确的赌注时,我需要一些按钮。以下是我所拥有的:

 public static void showButtons(Boolean x) { // to show or hide the buttons
            if(x) {
                hitButton.setVisible(true);
                standButton.setVisible(true);
                separator.setVisible(true);
            }
            else {
                hitButton.setVisible(false);
                standButton.setVisible(false);
                separator.setVisible(false);

           }
}

一旦赌注被验证为整数,它将通过以下步骤:

private void bdButtonActionPerformed(java.awt.event.ActionEvent evt) { //bdButton is the bet button
        ...
        if(validBet(s)) {
            bet = Integer.parseInt(s); // parses input string into an int
            System.out.println("bet accepted"); // debug to know if it works this far
            NewRound nr = new NewRound();
            System.out.println("created object");
            nr.newHand(bet);
            //showButtons(true); // this works, it changes the buttons, but only from here
        }
        else {
            ...
        }
    }

以下是新方法:

public static void newHand(int bet) {
        System.out.println("In newHand"); // debug
        BlackJack b = new BlackJack();

        b.showButtons(true);
        System.out.println("passed showButtons"); // the code gets to here, but buttons are still not visible
    }

共 (1) 个答案

  1. # 1 楼答案

    你的newHand方法是静态的,所以应该用类名来调用它