Java 为什么是JTextfield';s文本值没有通过使用变量进行可视化更改?
我正在做一个用JAVA制作棋盘游戏的项目。现在我想当一个玩家获胜时,分数将显示在Jtextfield中。因此,首先我使用了一个方法从GamePlan类中Declarewin method()中的Player1Graphics类中获取Jtextfield。但这显示了一个NullPointerException 所以我想了另一种方法,因为我不能将GameGui类的对象转换为任何其他类,因为它将创建无限循环。因此,我使用了另一个名为Player class的类,并创建了一个变量“SName”,该变量将从decarwin()方法中的“score”变量中获取值。然后,我在Player1Graphics类中使用了一个updateTexted()方法,该类是GameGui类的内部类,并设置了“SName”变量。现在在运行时,所有变量都工作正常,它们按预期传递值,JTextfield文本值也在更新,但JTextfield的可视值没有更新。它没有从0更改为1或从1更改为2 我正在给下面的代码Java 为什么是JTextfield';s文本值没有通过使用变量进行可视化更改?,java,variables,methods,parameter-passing,jtextfield,Java,Variables,Methods,Parameter Passing,Jtextfield,我正在做一个用JAVA制作棋盘游戏的项目。现在我想当一个玩家获胜时,分数将显示在Jtextfield中。因此,首先我使用了一个方法从GamePlan类中Declarewin method()中的Player1Graphics类中获取Jtextfield。但这显示了一个NullPointerException 所以我想了另一种方法,因为我不能将GameGui类的对象转换为任何其他类,因为它将创建无限循环。因此,我使用了另一个名为Player class的类,并创建了一个变量“SName”,该变量将
JTextField showscore = new JTextField(20);
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
updatetextfield(player.sname);
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showscore.getText());
}
public class Player{
String sname = Integer.toString(GamePlan.score);
}
这是GameGui类的代码
public class GameGui extends JFrame{
GameGui(){
super("Board");
this.setVisible(true);
this.setSize(700,800);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(main_panel);
main_panel.setLayout(new BorderLayout());
main_panel.setBackground(Color.WHITE);
resourcer();
}
resourcer(){
//players
JPanel playersroom = new JPanel();
//playersroom.setBorder(BorderFactory.createLineBorder(Color.black, 1));
playersroom.setBackground(Color.white);
playersroom.setLayout(new BorderLayout());
JPanel player1room = new JPanel();
player1room.setLayout(new BorderLayout());
//player1room.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));
player1room.setPreferredSize(new Dimension(350,300));
player1room.add(new Player1graphics(),BorderLayout.CENTER);
playersroom.add(player1room,BorderLayout.WEST);
arena.add(playersroom,BorderLayout.CENTER);
main_panel.add(arena,BorderLayout.SOUTH);
}
这是保存JTextfield的player1Graphics类,它是GameGui类的内部类
public class Player1graphics extends JPanel{
private String newname;
JLabel name = new JLabel("PLAYER 1",SwingConstants.CENTER);
JButton setname = new JButton("CN");
GamePlan g = new GamePlan();
JButton token = new JButton("POISON");
JTextField showscore = new JTextField(20); //this is the JTextfield
Player1graphics(){
//repaint();
this.setLayout(null);
this.setBackground(new Color(255,220,154));
//setname code & name code
//JButton setname = new JButton("CN");
setname.setBounds(160, 40, 30, 20);
setname.setBorder( BorderFactory.createLineBorder(Color.black, 1, true));
setname.setToolTipText("Change the name");
name.setBounds(132, 70, 80, 30);
name.setOpaque(true);
name.setBackground(new Color(102,140,185));
name.setForeground(new Color(202,208,215));
name.setBorder(BorderFactory.createLineBorder(Color.black, 1));
setname.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newname = JOptionPane.showInputDialog(null,"ENTER YOUR NAME");
if (newname==null){
name.setText("PLAYER 1");
}else{
t1=1;
name.setText(newname);
getfirstplayername(newname);
}
}
});
//token button code
//JButton token = new JButton("POISON");
token.setBackground(new Color(233,249,254));
token.setBounds(132, 110, 80, 30);
token.setToolTipText("token can be used only one time");
ActionClass ac = new ActionClass();
ac.getfirstplayerpoisonbutton(token);
token.addActionListener(ac);
//won and showin textfieldcode
JLabel won = new JLabel("Won",SwingConstants.CENTER);
won.setBorder( BorderFactory.createLineBorder(new Color(102,140,185), 2, true));
won.setBounds(132, 150, 50, 30);
won.setBackground(new Color(102,140,185));
won.setForeground(Color.WHITE);
//JTextfield codes
//JTextField showwin = new JTextField();
//GamePlan gm = new GamePlan();
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
//showwin.setOpaque(true);
//showwin.setBackground(Color.WHITE);
//showwin.setText(player.sname);
updatetextfield(player.sname);
this.add(name);
this.add(setname);
this.add(token);
this.add(won);
this.add(showscore);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D myg = (Graphics2D) g;
myg.setStroke(new BasicStroke(1));
myg.setColor(new Color(163,193,231));
myg.fillOval(25, 10, 300, 200);
}
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showwin.getText());
}
}
这是JTextfield代码
JTextField showscore = new JTextField(20);
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
updatetextfield(player.sname);
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showscore.getText());
}
public class Player{
String sname = Integer.toString(GamePlan.score);
}
这是updatetextfield方法代码
JTextField showscore = new JTextField(20);
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
updatetextfield(player.sname);
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showscore.getText());
}
public class Player{
String sname = Integer.toString(GamePlan.score);
}
这是我在player类中创建的变量
JTextField showscore = new JTextField(20);
Player player = new Player();
showscore.setBounds(185, 150, 30, 30);
showscore.setEditable(false);
updatetextfield(player.sname);
public void updatetextfield(String s){
showscore.setText(s);
showscore.repaint();
//JOptionPane.showMessageDialog(null, showscore.getText());
}
public class Player{
String sname = Integer.toString(GamePlan.score);
}
这是declarewin方法,其中score变量更新,它位于另一个名为GamePlan类的类中
public class GamePlan{
static int score=0;
public void declarewin(){
score++; //score variable updating
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
bbutton[i][j].setEnabled(false);
}
}
JOptionPane.showMessageDialog(null, score);
Player pl = new Player();
JOptionPane.showMessageDialog(null, pl.sname);
JOptionPane.showMessageDialog(null, "YOU have won the game");
}
公共类游戏计划{
静态智力得分=0;
公共无效声明(){
score++;//score变量更新
对于(int i=0;i,每次更新player.sname变量的值时,都需要更新文本字段
现在,您可以创建分数的副本并将其保存在文本字段中。
您需要使用新值再次调用updatetextfield函数
请参见此示例:
public class GamePlan{
static int score=0;
//First new line
GameGui referenceToGui //Get this reference somethere
public void declarewin(){
score++; //score variable updating
//Second new line
referenceToGui.updateTextField(""+score);
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
bbutton[i][j].setEnabled(false);
}
}
JOptionPane.showMessageDialog(null, score);
Player pl = new Player();
JOptionPane.showMessageDialog(null, pl.sname);
JOptionPane.showMessageDialog(null, "YOU have won the game");
}
公共类游戏计划{
静态智力得分=0;
//第一条新线路
GameGui referenceToGui//在那里获取此引用
公共无效声明(){
score++;//score变量更新
//第二条新线路
referenceToGui.updateTextField(“+”分数);
对于(int i=0;i是否已将Player1graphics panel添加到主窗口?),放置主框架代码除了在初始化代码中调用updatetextfield
方法外,您是否还调用过该方法?我已添加了主框架代码………@Michele LacorteI正在调用updatetextfield()Player1GraphicsClass构造函数中的方法………..@substrif如果你只在构造函数中调用它,它在启动游戏后将永远不会更新它,因此为什么它保持在0?我已经完成了你所展示的………但是作为updatetextfield()方法位于player1graphics类中,这是GameGUi类的内部类。因此,我无法直接使用GameGUi引用使用updatetextfield()方法。因此,我使用了如下命令//code'GameGUi reference;reference.pl1.updatetextfield(“+score”);//pl1是player1graphics类的对象,但编译器向我显示空指针异常……@Denis……:/