这个问题在这里已经有答案了:
什么是nullpointerexception,如何修复它(12个答案)
三年前关门了。
编辑:我还没有初始化layeredpane。抱歉,在mpanel构造器的开始,jsut代码太长了,我添加了一个分层窗格,但是没有添加它,在eclipse上出现错误,为什么?
我对java真的很新,所以我对图层窗格没有任何概念
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.*;
// battleship.MainPanel.GridListener;
public class mPanel extends JFrame implements MouseListener, MouseMotionListener
{
public JLayeredPane layeredPane=null;
private static final int UNCLICKED = 6;
private static final int EMPTY = 7;
private static final int HIT = 8;
private static final int MISS = 9;
private static final Color COLOR_UNCLICKED = Color.white;
private static final Color COLOR_MISS = Color.blue;
private static final Color COLOR_HIT = Color.red;
private JLabel title;
private JPanel titlePanel;
private JButton[][] gridButton,gridButton1;
private JPanel gridPanel,gridPanel1;
private int[][] board,board1;
Dimension boardSize = new Dimension(340, 410);
GridListener gridListener = new GridListener();
int count=0;
private JLabel ship1;
//private GridListen gridListen = new GridListen();
public mPanel()
{
这里我得到了关于空指针异常的错误
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
layeredPane.setPreferredSize(boardSize);
getContentPane().add(layeredPane);
title = new JLabel("BATTLESHIP!");
ship1= new JLabel("Ship1");
titlePanel = new JPanel();
titlePanel.add(title);
gridButton = new JButton[10][10];
gridButton1 = new JButton[10][10];
gridPanel = new JPanel();
gridPanel1= new JPanel();
gridPanel.setLayout(new GridLayout(10, 10));
gridPanel1.setLayout(new GridLayout(10, 10));
for (int r = 0; r < gridButton.length; r++)
for (int c = 0; c < gridButton[r].length; c++)
{
gridButton[r][c] = new JButton();
gridButton[r][c].setBackground(COLOR_UNCLICKED);
gridButton[r][c].setEnabled(true);
gridButton[r][c].addActionListener(gridListener);
gridPanel.add(gridButton[r][c]);
}
gridPanel1.setLayout(new GridLayout(10, 10));
for (int r = 0; r < gridButton1.length; r++)
for (int c = 0; c < gridButton1[r].length; c++)
{
gridButton1[r][c] = new JButton();
gridButton1[r][c].setBackground(COLOR_UNCLICKED);
gridButton1[r][c].setEnabled(true);
gridButton1[r][c].addActionListener(gridListener);
gridPanel1.add(gridButton1[r][c]);
}
this.setLayout(new BorderLayout());
this.add(titlePanel, "North");
this.add(gridPanel, BorderLayout.LINE_START);
this.add(gridPanel1, BorderLayout.LINE_END);
this.setPreferredSize(new Dimension(1100, 400));
board = new int[10][10];
for (int r = 0; r < board.length; r++)
for (int c = 0; c < board.length; c++)
{
board[r][c] = UNCLICKED;
gridButton[r][c].setEnabled(true);
}
board1 = new int[10][10];
for (int r = 0; r < board1.length; r++)
for (int c = 0; c < board1.length; c++)
{
board1[r][c] = UNCLICKED;
gridButton1[r][c].setEnabled(false);
}
/*add(ship1);
ship1.setBounds(100, 100, 150, 40);
ship1.addMouseMotionListener(new MouseAdapter(){
public void mouseDragged(MouseEvent E)
{
int X=E.getX()+ship1.getX();
int Y=E.getY()+ship1.getY();
ship1.setBounds(X,Y,150,40);
System.out.println(X+" "+Y);
}
});*/
}
class GridListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent evt) {
if(count%2==0)
{
//System.out.println(count);
for (int r = 0; r < gridButton.length; r++)
for(int c = 0; c < gridButton[r].length; c++)
{
if (evt.getSource() != gridButton[r][c])
continue;
handleGridButton(r,c);
return;
}
}
else
{
//System.out.println(count);
for (int r = 0; r < gridButton1.length; r++)
for(int c = 0; c < gridButton1[r].length; c++)
{
if (evt.getSource() != gridButton1[r][c])
continue;
handleGridButton(r,c);
return;
}
}
}
}
public void handleGridButton(int r, int c)
{
if(count%2==0)
{
if (board[r][c] == UNCLICKED)
{
++count;
board[r][c] = MISS;
gridButton[r][c].setBackground(COLOR_MISS);
}
}
else
{
++count;
if (board1[r][c] == UNCLICKED)
{
board1[r][c] = MISS;
gridButton1[r][c].setBackground(COLOR_MISS);
}
}
gridenable();
System.out.println(count);
}
public void gridenable()
{
if(count%2==0)
{
for (int r = 0; r < gridButton.length; r++)
{
for(int c = 0; c < gridButton[r].length; c++)
gridButton1[r][c].setEnabled(false);
}
for (int r = 0; r < gridButton.length; r++)
{
for(int c = 0; c < gridButton[r].length; c++)
if(board[r][c]!=MISS)
gridButton[r][c].setEnabled(true);
}
}
else
{
for (int r = 0; r < gridButton.length; r++)
{
for(int c = 0; c < gridButton[r].length; c++)
gridButton[r][c].setEnabled(false);
}
for (int r = 0; r < gridButton.length; r++)
{
for(int c = 0; c < gridButton[r].length; c++)
if(board1[r][c]!=MISS)
gridButton1[r][c].setEnabled(true);
}
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String args[])
{
mPanel n= new mPanel();
n.pack();
n.setVisible(true);
n.setResizable(false);
}
}
1条答案
按热度按时间66bbxpm51#
你必须初始化
layeredPane
在开始调用它的方法之前。像下面这样从本质上理解npe的含义以及它的根本原因,在网上搜索也不会有什么坏处。例如,你可以看看sof