所以我正在尝试为一个由按钮控制的球创建一个 GUI,以通过迷宫运行。但刚开始我想创建一个带有几个方向按钮的框架,每当我尝试在 Eclipse 中运行它时,它都会出现一个根本不是很具体的错误。我的代码如下。错误指向代码的“buttonTL、buttonTR、buttonBL 和 buttonBR”部分。感谢您提供任何帮助。
错误是:
mazeassignment.One.createGUI(One.java:55) at mazeassignment.One.main(One.java:23) 的线程“main”java.lang.NullPointerException 中的异常
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class One extends JFrame implements ActionListener//, ChangeListener
{
private JButton buttonRight, buttonLeft, buttonUp, buttonDown, buttonPlay, buttonExit, buttonReset, buttonGrid, buttonTL, buttonTR, buttonBL, buttonBR, buttonM, buttonOption1, buttonOption3, buttonCompass;
private JPanel panelCentre, panelRight, panelBottom, buttonPanel, compassPanel, optionsPanel, debugPanel;
private JSlider sliderSpeed;
private JLabel appSpeed;
private JButton [] JBArray = new JButton[630];
private int bLocation = 290;
public int ballLocation = 287;
private Icon bImage, iconBall, tile, iconWall, iconCompassNorth;
public static void main (String[] args)
{
One frame = new One();
frame.setSize(875, 600);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout() );
panelCentre = new JPanel();
panelCentre.setPreferredSize(new Dimension(625, 450));
panelCentre.setBackground(Color.BLUE);
window.add(panelCentre);
panelCentre.setLayout(new GridLayout(21, 30));
panelRight = new JPanel();
panelRight.setPreferredSize(new Dimension(200, 450));
panelRight.setBackground(Color.GRAY);
window.add(panelRight, BorderLayout.EAST);
panelBottom = new JPanel();
panelBottom.setPreferredSize(new Dimension(875, 100));
panelBottom.setBackground(Color.GREEN);
window.add(panelBottom, BorderLayout.SOUTH);
debugPanel = new JPanel();
debugPanel.setPreferredSize(new Dimension(200, 100));
debugPanel.setBackground(Color.WHITE);
panelRight.add(debugPanel);
buttonTL = new JButton("");
buttonPanel.add(buttonTL);
buttonTL.setEnabled(false);
buttonUp = new JButton("^");
buttonPanel.add(buttonUp);
buttonUp.addActionListener(this);
buttonTR = new JButton("");
buttonPanel.add(buttonTR);
buttonTR.setEnabled(false);
buttonLeft = new JButton("<");
buttonPanel.add(buttonLeft);
buttonLeft.addActionListener(this);
buttonM = new JButton("");
buttonPanel.add(buttonM);
buttonM.setEnabled(false);
buttonRight = new JButton(">");
buttonPanel.add(buttonRight);
buttonRight.addActionListener(this);
buttonBL = new JButton("");
buttonPanel.add(buttonBL);
buttonBL.setEnabled(false);
buttonDown = new JButton("v");
buttonPanel.add(buttonDown);
buttonDown.addActionListener(this);
for(int i=0; i<630; i++)
{
JBArray[i] = new JButton();
buttonGrid = new JButton(""+i);
panelCentre.add(JBArray[i]);
JBArray[i].setBorderPainted(false);
}
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
JBArray[bLocation].setIcon(iconBall);
JBArray[bLocation].setIcon(tile);
if (source == buttonRight)
{
JBArray[bLocation+1].setIcon(iconBall);
JBArray[bLocation-1].setIcon(tile);
bLocation=bLocation+1;
}
if (source == buttonLeft)
{
JBArray[bLocation-1].setIcon(iconBall);
JBArray[bLocation+1].setIcon(tile);
bLocation=bLocation-1;
}
if (source == buttonUp)
{
JBArray[bLocation-30].setIcon(iconBall);
JBArray[bLocation+30].setIcon(tile);
bLocation=bLocation-30;
}
if (source == buttonDown)
{
JBArray[bLocation+30].setIcon(iconBall);
JBArray[bLocation-30].setIcon(tile);
bLocation=bLocation+30;
}
}
}