gpt4 book ai didi

java - 当单词绝对不为空时获取字符串方法返回空

转载 作者:行者123 更新时间:2023-12-01 09:20:23 25 4
gpt4 key购买 nike

我正在编写一个刽子手游戏,由于某种原因,我的 getW() 方法在应该返回一个肯定不为 null 的字符串时却返回 null。我广泛添加了 System.out.println 以确保 word 保持不变。除了 getW() 方法之外,其他地方似乎都是相同的,这就是 getW() 返回 null 的原因。

这是我的代码:

Hangman.java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Hangman extends JPanel{
private String word;

public void setupLayout(JFrame window){
window.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

HangmanArea panel1 = new HangmanArea();
panel1.setBackground(Color.blue);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
window.add(panel1, c);

GuessedLetters panel2 = new GuessedLetters();
panel2.setBackground(Color.green);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
window.add(panel2, c);

Letters panel3 = new Letters();
panel3.setBackground(Color.black);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 2;
window.add(panel3, c);
System.out.println("Before repaint: "+ word);
panel1.callRepaint();
panel2.callRepaint();
panel3.callRepaint();
System.out.println("After repaint: " + word);
}

public void startWindow(){
JFrame window = new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenHeight = (int) screenSize.getHeight();
int windowHeight = (int) ((screenHeight / 4) * 3);
window.setSize(windowHeight, windowHeight);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setupLayout(window);
window.setVisible(true);
}

public void startGame(){
Prep prepare = new Prep();
word = prepare.findWord(true);
System.out.println("Word found: " + word);
startWindow();
System.out.println("Word is unchanged: " + word);
}

public static void main(String[] args){
Hangman start = new Hangman();
start.startGame();
}
public String getW(){
System.out.println("getW()'s word: " + word); //At this point, word is null
return word;
}
}

Letters.java:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Letters extends JPanel {
private String word = "aaa";
public void callRepaint(){
Hangman object = new Hangman();
word = object.getW();
System.out.println("Word for Letters: " + word);
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.white);
g.drawString("letters", 50, 50);
}
}

如果这看起来代码很多,我很抱歉,但 Hangman.java 和 Letters.java 是两个重要的类。 Prep.java 的 findWord() 方法只是从单词列表中获取一个随机单词并返回它。我认为没有必要发布该内容,但我可以。除了 callRepaint()paintCompnent(Graphics g) 方法之外,GuessedLetters.java 和 HangmanArea.java 完全是空的。如果您需要,我可以发布它们,但我认为它们不是很重要。

输出可能如下所示:

Word found: night
Before repaint: night
getW()'s word: null
Word for Letters: null
After repaint: night
Word is unchanged: night

我不知道是什么原因造成的。

最佳答案

问题是您没有将已初始化 word 变量的对象(在您发布的示例输出中为“night”)传递给 callRepaint 方法,而是在该方法内创建一个新实例(未连接到您的“主”对象)。相反,您应该将对象作为参数传递,例如:

public void callRepaint(Hangman object){
word = object.getW();
System.out.println("Word for Letters: " + word);
repaint();
}

并在您的 Hangman 类中这样调用它:

panel1.callRepaint(this);
panel2.callRepaint(this);
panel3.callRepaint(this);

或者可能只是传递字符串,例如:

public void callRepaint(String word){
System.out.println("Word for Letters: " + word);
repaint();
}

这样调用它:

panel1.callRepaint(getW());
panel2.callRepaint(getW());
panel3.callRepaint(getW());

关于java - 当单词绝对不为空时获取字符串方法返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40203957/

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