1

別のからJTextFieldを読み取った後、別のクラスで を更新する際に問題が発生する。問題のメソッドは次のとおりです。StringJTextField

public JTextField buyVowel(playerPlate player)
{
    String get = input.getText();
    String[] vowels = new String[]{"a","e","i","o","u"};
    for(int i =0; i<vowels.length; i++)
    {
        if(get.equals(vowels[i]))
        {
            player.pMoney =- 250;
            player.playerMoney.setText("$"+player.pMoney);

        }
    }
    return player.playerMoney;
}

playerPlateは別クラスです。

このメソッドを使用して、プログラムがどのプレーヤーを変更する必要があるかを判断しています。

public playerPlate getCurrentPlayer()
{
    if(currentPlayer == 1)
    {
        return player1;
    }
    else if(currentPlayer == 2)
    {
        return player2;
    }
    else
    {
        return player3;
    }
}

プレーヤー 1、2、および 3 は のインスタンスですplayerPlate

このクラスのインスタンス変数を変更したい:

package wheelOfFortune;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class playerPlate extends JPanel
                            implements ActionListener
{
public String pName;
public int pMoney = 500;
public int currentPlayer;
public JTextField playerMoney;

public playerPlate(String player, Color color, int currentPlayer)
{
    setBorder(BorderFactory.createLineBorder(Color.BLACK,2));
    setBackground(color);
    pName = player;
    JTextField playerNames = new JTextField(pName);
    playerNames.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));
    playerNames.setEditable(false);
    playerNames.setFont(new Font("Impact", Font.PLAIN, 24));
    playerNames.setHorizontalAlignment(JTextField.CENTER);
    playerNames.setBackground(Color.WHITE);

    JTextField playerMoney = new JTextField("$"+pMoney);
    playerMoney.setBorder(BorderFactory.createLineBorder(Color.BLACK,2));
    playerMoney.setEditable(false);
    playerMoney.setFont(new Font("Impact", Font.BOLD, 32));
    playerMoney.setHorizontalAlignment(JTextField.CENTER);
    playerMoney.setBackground(Color.WHITE);

    Box b1 = Box.createVerticalBox();
    b1.add(playerNames);
    b1.add(Box.createVerticalStrut(5));
    Box b2 = Box.createHorizontalBox();
    b2.add(Box.createHorizontalStrut(60));
    Box b3 = Box.createVerticalBox();
    b3.add(playerMoney);
    b3.add(Box.createVerticalStrut(8));
    b2.add(b3);
    b1.add(b2);
    b1.add(Box.createVerticalStrut(5));
    add(b1);    
}
public void actionPerformed(ActionEvent e) 
{

}
}

actionPerformedメインクラス内のメソッドは次のとおりです。

public void actionPerformed(ActionEvent e) 
{
    JButton b = (JButton)e.getSource();
    if(b==spin)
    {
        spinWheel(wheelStuff);
        repaint();
    }
    if(b==next)
    {
        updatePlayer();
        repaint();
    }
    if(b==reset)
    {
        letterBoard.reset();
        updateCat();
        repaint();
    }
    if(b==buyVowel)
    {
        buyVowel(getCurrentPlayer());
        repaint();
    }
}

私がやりたいことの要点は、ユーザーが母音を に入力しJTextField input、それをクリックするJButton buyVowelと、合計金額 ( ) から 250 ドルが差し引かれることpMoneyです。そして、変更を GUI に表示します。これを数時間いじった後、正直なところ、なぜこれが機能しないのかわかりません。nullPointerExceptions使用しようとすると、受信し続けます。ご協力いただきありがとうございます。

注: class を除くすべてplayerPlateが同じクラスにあります。playerPlate別クラスです。

4

1 に答える 1

3

のコンストラクターで変数をシャドーイングしています。このメソッドは、呼び出し時にインスタンス化されることに依存しています。それ以外の場合は、がスローされます。交換playerMoneyplayerPlatebuyVowelplayerPlatesetTextNullPointerException

JTextField playerMoney = new JTextField("$"+pMoney);

playerMoney = new JTextField("$"+pMoney);

余談: Java の命名規則では、クラス名は大文字で始まるため、 などのクラス名を使用しPlayerPlateます。

于 2013-06-01T03:36:15.253 に答える