gpt4 book ai didi

java - 从另一个 JTextField 读取字符串后修改 JTextField

转载 作者:行者123 更新时间:2023-11-29 05:42:55 25 4
gpt4 key购买 nike

在从另一个 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 输入 中键入一个元音字母并单击 JButton buyVowel 时,它会从他们的总金额中减去 250 美元(pMoney)。并在 GUI 上显示更改。修改了几个小时后,老实说,我不知道为什么这不起作用。我在尝试使用它时一直收到 nullPointerExceptions。感谢您的帮助。

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

最佳答案

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

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

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

旁白:Java 命名约定表明类名以 大写 字母开头,因此请使用类名,例如 PlayerPlate

关于java - 从另一个 JTextField 读取字符串后修改 JTextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16868784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com