【问题标题】:Modifying a JTextField after reading String from another JTextField从另一个 JTextField 读取字符串后修改 JTextField
【发布时间】:2013-06-01 03:31:00
【问题描述】:

在从另一个 JTextField 读取 String 后,在另一个类中更新 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;
    }
}

玩家 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 在一个单独的类中。

【问题讨论】:

  • 遇到异常时共享堆栈跟踪

标签: java arrays swing jbutton jtextfield


【解决方案1】:

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

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

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

另外:Java 命名约定表明类名以 uppcase 字母开头,因此请使用类名,例如 PlayerPlate

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多