Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这行给出了一个空指针,我不知道为什么_Java_Eclipse_Jframe_Jpanel - Fatal编程技术网

Java 这行给出了一个空指针,我不知道为什么

Java 这行给出了一个空指针,我不知道为什么,java,eclipse,jframe,jpanel,Java,Eclipse,Jframe,Jpanel,我正在尝试向所有这些面板添加按钮,以便检查它们是否被单击。我对Java还是个新手,我们就是这样被教导如何做的 现在我正在制作一个大面板,在上面添加48个新面板,然后在每个面板上添加按钮,这样我就可以制作一个动作事件。如果有一种方法可以让我检查我是否点击了面板,那么我可以这样做,但我不知道怎么做 我在线路面板[x]上得到NullPointerException package CatchTheMouse; import java.awt.*; import java.awt.event.*; i

我正在尝试向所有这些面板添加按钮,以便检查它们是否被单击。我对Java还是个新手,我们就是这样被教导如何做的

现在我正在制作一个大面板,在上面添加48个新面板,然后在每个面板上添加按钮,这样我就可以制作一个动作事件。如果有一种方法可以让我检查我是否点击了面板,那么我可以这样做,但我不知道怎么做

我在线路面板[x]上得到NullPointerException

package CatchTheMouse;

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

public class CatchTheMouse extends JFrame implements ActionListener, MouseListener{
    final int ROWS = 8;
    final int COLS = 6;
    final int GAP = 2;
    final int MAX_PANELS = ROWS * COLS;
    int clicks;
    int hits;
    int percentage = 0;
    int width;
    int height;
    int panelX;
    int panelY;
    int whichPanel = (int)(Math.random() * 47 + 1);

    JButton[] click = new JButton[MAX_PANELS];
    JLabel grats = new JLabel("");
    JLabel spot = new JLabel("X");
    JPanel[] panel = new JPanel[MAX_PANELS];
    JPanel pane = new JPanel(new GridLayout(ROWS, COLS, GAP, GAP));
    Font xFont = new Font("Ariel", Font.BOLD, 20);
    Font font = new Font("Ariel", Font.PLAIN, 12);

    public CatchTheMouse() {
        super("Catch the Mouse");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300,300);
        add(spot);
        spot.setFont(xFont);
        add(grats);
        grats.setFont(font);
        add(pane);
        for(int x = 0; x < MAX_PANELS; ++x) {
            panel[x] = new JPanel();
            pane.add(panel[x]);
            panel[x].setBackground(Color.RED);
            panel[x].add(click[x]);
            click[x].addActionListener(this);
            click[x].setVisible(false);
        }
        pane.setBackground(Color.BLACK);
        panel[whichPanel].add(spot);
    }

    public void mouseClicked(MouseEvent e) {
        clicks = e.getClickCount();
    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
        Object src = e.getSource();
        if(src == click[whichPanel]) {
            hits++;
            grats.setText("You have made " + Integer.toString(hits) + " hits");
        }
    }

    public static void main(String[] args) {
        CatchTheMouse frame = new CatchTheMouse();
        frame.setVisible(true);
    }
}
猜一猜,这句话:

panel[x].add(click[x]);
您正在尝试添加尚未构建的JButton。在添加之前先构建它们

click[x] = new JButton("something");
panel[x].add(click[x]);
不过,将来在这里寻求帮助时,请包括所有相关信息,特别是引发您遇到的任何异常的行。

您缺少单击[x]=使用单击[x]之前的新JButton。通过面板[x]的初始化,您就正确了


是的。我意识到我没有把它包括进去,所以我编辑了进去。对不起
    for(int x = 0; x < MAX_PANELS; ++x) {
        panel[x] = new JPanel();
        pane.add(panel[x]);
        panel[x].setBackground(Color.RED);           
        click[x] = new JPanel(); // add this
        panel[x].add(click[x]);
        click[x].addActionListener(this);
        click[x].setVisible(false);
    }