Java 从另一个JTextField读取字符串后修改JTextField
从另一个Java 从另一个JTextField读取字符串后修改JTextField,java,arrays,swing,jbutton,jtextfield,Java,Arrays,Swing,Jbutton,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++) {
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;
}
播放器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-buy元音
,它会从他们的总收入中减去250美元(pMoney
)。并在GUI上显示更改。在修补了几个小时后,我真的不知道为什么这不起作用。当我试图使用它时,我一直收到nullPointerExceptions
。谢谢你的帮助
注意:除classplayerPlate
之外的所有内容都在同一个类中playerPlate
在一个单独的类中。您是playerPlate
构造函数中的变量playerMoney
。方法buyvouel
依赖于调用setText
时实例化playerPlate
,否则将抛出NullPointerException
。替换
JTextField playerMoney = new JTextField("$"+pMoney);
与
旁白:Java命名约定表明类名以大写字母开头,因此在出现异常时使用类名,例如PlayerPlate
共享堆栈跟踪
JTextField playerMoney = new JTextField("$"+pMoney);
playerMoney = new JTextField("$"+pMoney);