gpt4 book ai didi

java - 使用KeyListener使用setText时出现NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:18:29 24 4
gpt4 key购买 nike

这是一个猜词游戏。第一个字母每次都会显示,其余的则不会。每当用户键入时,我希望它更改板上的字母,这是一个屏幕截图:http://puu.sh/7moQS.jpg

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at BoardUI.keyTyped(BoardUI.java:48)

其中 BoardUI.java:48 行位于

b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setText(typed + "");

游戏.java

public class Game {
}

testGUI.java

public class testGUI {
public static void main(String[] args) {
new GUI("Lingo");
}
}

BoardUI.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class BoardUI extends JPanel implements KeyListener {

private final Game game;
private Board b = new Board();
private Color hoverColor = new Color(100, 100, 255);

public BoardUI(LayoutManager layout, Game game, String word) {
super(layout);
this.b = new Board();
this.game = game;
initComponents(word);
}

public BoardUI(Game game) {
this.game = game;
}

private void initComponents(String word) {
this.setLayout(new GridLayout(5, 5, 2, 2));
for (int i = 0; i < 5 * 5; i++) {
b.getLetters()[i] = new Label();
b.getLetters()[i].setBackground(Color.WHITE);
b.getLetters()[i].setForeground(Color.BLACK);
b.getLetters()[i].setAlignment(Label.CENTER);
b.getLetters()[i].setFont(new Font("Big", Font.BOLD, 48));
this.add(b.getLetters()[i]);
}

for (int i = 0; i < 5; i++) {
b.getLetters()[i].setText(word.charAt(i) + "");
b.getLetters()[i].setBackground(Color.BLUE);
}
b.setCurrentColumn(0);
b.setCurrentRow(0);
}

@Override
public void keyTyped(KeyEvent e) {
char typed = e.getKeyChar();

if (Character.isLetter(typed) && b.getCurrentColumn() < 5) {
typed = Character.toUpperCase(typed);
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setText(typed + "");
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(Color.BLUE);

if (b.getCurrentColumn() == 0) {
for (int i = 1; i < 5; i++) {
b.getLetters()[b.position(b.getCurrentRow(), i)].setText(".");
b.getLetters()[b.position(b.getCurrentRow(), i)].setBackground(Color.BLUE);
}

b.nextColumn(true);

if (b.getCurrentColumn() < 5) {
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(hoverColor);
}

if (typed == 8 && b.getCurrentColumn() >= 0) {
this.oneBack();
}
}
}
}

private void oneBack() {
if (b.getCurrentColumn() < 5) {
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(Color.BLUE);
b.nextColumn(false);
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setText(".");
b.getLetters()[b.position(b.getCurrentRow(), b.getCurrentColumn())].setBackground(Color.BLUE);
}
}

@Override
public void keyPressed(KeyEvent e) {

}

@Override
public void keyReleased(KeyEvent e) {

}
}

Board.java:

import java.awt.*;

public class Board {
private Reader reader = new Reader();
private Label[] letters = new Label[5 * 5];
private String[] words = new String[100];
private int currentRow, currentColumn;

public Label[] getLetters() {
return letters;
}

public String firstWord() {
return getWord().charAt(0) + "....";
}

public String getWord() {
int count = reader.LeesWoordenVanFile(words);
int x = (int) (Math.random() * 100);
int gokX = (x % count);
return words[gokX].toUpperCase();
}

public int position(int row, int column) {
return row * 5 + column;
}

public void nextColumn(boolean next) {
if (next) {
currentColumn++;
} else {
currentColumn--;
}
}

public int getCurrentRow() {
return currentRow;
}

public int getCurrentColumn() {
return currentColumn;
}

public void setCurrentRow(int currentRow) {
this.currentRow = currentRow;
}

public void setCurrentColumn(int currentColumn) {
this.currentColumn = currentColumn;
}
}

GUI.java:

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

public class GUI extends JFrame {

private JPanel gamePanel;
private Game game;
private Board board = new Board();
private BoardUI boardUI = new BoardUI(game);

public GUI(String title) throws HeadlessException {
super(title);
game = new Game();
setupVieuw();
showFrame();
this.addKeyListener(boardUI);
}

public void setupVieuw() {
getContentPane().removeAll();
initComponents(board.firstWord());
layoutComponents();
this.validate();
}

public void layoutComponents() {
getContentPane().add(gamePanel, BorderLayout.CENTER);
}

private void initComponents(String word) {
gamePanel = new JPanel(new BorderLayout());
gamePanel.add(new BoardUI(new BorderLayout(),game, word));
}

private void showFrame() {
this.setBounds(50, 50, 800, 800);
this.setPreferredSize(new Dimension(600, 600));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
this.doLayout();
}
}

最佳答案

这确实令人困惑,因为您有一些冗余代码,并且在某些情况下会执行两次操作,但这是我发现的。

在 GUI.java 中,您将在此处实例化 BoardUI:

private Game game;
private Board board = new Board();
private BoardUI boardUI = new BoardUI(game);

请注意,

  1. 您正在使用 null Game 对象初始化 BoardUI
  2. 您正在调用此 BoardUI 构造函数:

    public BoardUI(Game game) {
    this.game = game;
    }

    并且不是这个构造函数 - 又名。实际上用 new Label() 对象填充您的 letters 数组:

    public BoardUI(LayoutManager layout, Game game, String word) {
    super(layout);
    this.b = new Board();
    this.game = game;
    initComponents(word);
    }

仍然在 GUI.java 中,您最终确实调用了不同的 initComponents(),但您使用新的 BoardUI 实例来调用它

private void initComponents(String word) {
gamePanel = new JPanel(new BorderLayout());
gamePanel.add(new BoardUI(new BorderLayout(),game, word));
}

返回后,您回到了 GUI 类构造函数的最后一行,并将关键监听器设置为尚未运行的 BoardUI initComponents():

this.addKeyListener(boardUI);

TL;DR:您正在向尚未运行 initComponents()BoardUI 对象添加一个关键监听器

关于java - 使用KeyListener使用setText时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22257495/

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