编辑:没有对我进行分层的愚蠢行为。
抱歉,在mpanel构造函数开头的长代码jsut我添加了一个jlayered窗格,但它没有添加,在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);
}
}
答案 0 :(得分:0)
在开始调用方法之前,必须先初始化layeredPane
。类似下面的内容
layeredPane= new JLayeredPane()
layeredPane.addMouseListener( this );
layeredPane.addMouseMotionListener( this );
layeredPane.setPreferredSize(boardSize);
通过网络搜索来固有地了解NPE的含义以及它的根本原因也不会受到伤害。例如,您可以查看sof