1

入力を変更するメソッドを使用してダイアログボックスの入力を取得しようとしてcharEditいますが、代わりに変数は最後に null のままです。

public String showInputDialog(String stringy)
{
    String input = JOptionPane.showInputDialog(this,stringy);
    if(input == null || input.isEmpty())
    {
        input = showInputDialog(stringy);
    }
    return input;
}

public void charEdit(Checkbox chara,String account,String password){
    chara.setLabel(showInputDialog("Character Name?"));
    account=(showInputDialog("Account Name?"));
    password=(showInputDialog("Account Password?"));
    chara.setEnabled(true);
}

public void menuItemSelected(MenuItem menuObj){
    if (menuObj==help){
        messageBox("Edit character info and then click the login button");
    }
    else if (menuObj==charOneEdit){
        charEdit(characterOne,charAArray[0],charPArray[0]);
    }
}

変数 characterOne が値を保持しないのはなぜですか?

4

1 に答える 1

0

あなたの問題は、文字列が不変であることです。したがって、このメソッドを呼び出す:

public void charEdit(Checkbox chara,String account,String password){
    chara.setLabel(showInputDialog("Character Name?"));
    account=(showInputDialog("Account Name?"));
    password=(showInputDialog("Account Password?"));
    chara.setEnabled(true);
}

メソッドに渡された引数によって参照される文字列は変更されません。

言い換えれば、これを呼び出す:

String acct = "";
String password = ""; // passwords should not be held in Strings by the way
charEdit(something, acct, password);

アカウントやパスワードは変更しません。

1 つの解決策は、クラス フィールドを使用し、それらを変更することです。もう 1 つは、アカウントとパスワードのフィールドを持つオブジェクトへの参照を渡し、そのフィールドの状態を変更する方法です。

于 2013-11-03T14:36:16.977 に答える