2013-06-01 60 views
1

在从另一个JTextField读取String后,在不同类中更新JTextField时遇到一些问题。这里是有问题的方法:从另一个JTextField读取字符串后修改JTextField

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

播放器(S)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。谢谢你的帮助。

注意:除了playerPlate类之外的所有内容都属于同一类。 playerPlate是在一个单独的类。

+0

共享堆栈跟踪,当你得到例外 – pinkpanther

回答

3

你是shadowing变量playerMoney在构造函数playerPlate。方法buyVowel依赖playerPlate在调用setText时被实例化,否则将抛出NullPointerException。更换

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

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

旁白:Java命名惯例表明,类名开始uppcase书,以便使用类的名称,如PlayerPlate

+0

工程就像一个魅力。谢谢你,先生! – TheFatNarwhal

相关问题