首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用NullPointerException使用KeyListener使用setText时

使用NullPointerException使用KeyListener使用setText时
EN

Stack Overflow用户
提问于 2014-03-07 18:05:57
回答 1查看 880关注 0票数 0

这是猜字游戏。第一个字母每次都显示出来,而他们没有休息。每当用户输入我希望它更改板上字母的时候,下面是一个屏幕截图:http://puu.sh/7moQS.jpg

错误:

代码语言:javascript
运行
复制
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 + "");

Game.java

代码语言:javascript
运行
复制
public class Game {
}

testGUI.java

代码语言:javascript
运行
复制
public class testGUI {
public static void main(String[] args) {
    new GUI("Lingo");
}
}

BoardUI.java

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
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();
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-07 18:49:32

这非常令人困惑,因为您有一些多余的代码,并且在某些情况下执行了两次,但是下面是我所发现的。

在GUI.java中,您在这里实例化BoardUI

代码语言:javascript
运行
复制
private Game game;
private Board board = new Board();
private BoardUI boardUI = new BoardUI(game);

注意到,

  1. 您正在使用空的BoardUI对象来初始化Game
  2. 您正在调用这个BoardUI构造函数: 公共BoardUI(游戏游戏){ this.game =对策;} 而不是这个构造器--也就是。实际上用letters对象填充new Label()数组的方法: 公共BoardUI(LayoutManager布局,游戏游戏,字符串词){超级(布局);this.b .b=新板();this.game =游戏;initComponents( word);}

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

代码语言:javascript
运行
复制
private void initComponents(String word) {
    gamePanel = new JPanel(new BorderLayout());
    gamePanel.add(new BoardUI(new BorderLayout(),game, word));
}

在返回之后,回到GUI类构造函数的最后一行,并将键侦听器设置为未运行initComponents()BoardUI

代码语言:javascript
运行
复制
this.addKeyListener(boardUI);

TL;DR:您正在向没有运行BoardUIinitComponents()对象中添加一个键侦听器

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22257495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档