2017-07-14 5 views
0

Modifier: hadnt initilaised layeredpane idiot de moi. Désolé pour le long code jsut au début du constructeur mpanel j'ajoute un volet ajlayer mais ça ne se rajoute pas donne une erreur sur eclipse pourquoi?
Je suis vraiment nouveau avec java si je n'ai pas la moindre idée de vitres layerdPourquoi ne suis-je pas en mesure d'ajouter jlayered volet? Il montre null erreur d'erreur de pointeur

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() 
{ 

ici j'obtiens l'erreur à annuler des exceptions pointeur

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); 
} 
} 

Répondre

0

Vous devez initialiser layeredPane avant de commencer l'appel de méthodes sur il. quelque chose comme ci-dessous

layeredPane= new JLayeredPane() 
layeredPane.addMouseListener(this); 
layeredPane.addMouseMotionListener(this); 
layeredPane.setPreferredSize(boardSize); 

Il ne fera pas mal aussi de comprendre en soi ce que signifie NPE et il est la cause racine par googler sur le web. Par exemple, vous pouvez regarder sof

+0

Merci beaucoup travaillé: D –