簡體   English   中英

該行提供了一個空指針,我不知道為什么

[英]This line is giving a nullpointer and I have no idea why

我正在嘗試向所有這些面板添加按鈕,以便可以檢查是否單擊了它們。 我還是Java的新手,這就是我們被教導如何做的方法。

現在,我正在制作一個大面板,並在其上添加48個新面板,然后在每個面板上添加按鈕,以便進行動作事件。 如果我可以檢查一下是否單擊面板,則可以這樣做,但是我不知道如何。

我在“ panel [x] .add(click [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]);

不過,將來在這里尋求幫助時,請包括所有相關信息,尤其是包括引發您所遇到的任何異常的行。

在使用click[x]之前,您缺少click[x] = new JButton() 通過panel[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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM