【发布时间】:2014-09-02 22:55:10
【问题描述】:
我来了
**Exception in thread "main" java.lang.NullPointerException
at sources.Own_gui.<init>(Own_gui.java:32)
at sources.Maingui.main(Maingui.java:6)**
当我做我的项目时出错。我试图弄清楚但没有成功。
own_gui 类的代码:
package sources;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Own_gui extends JFrame implements ActionListener
{
JFrame frameOwn;
JPanel panelHit,panelPlace,panelButtons,panelTable;
JButton buttonBoat[][],buttonAttack[][];
public Own_gui()
{
this.setLayout(new GridLayout(2,2));
this.setTitle("First Player");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
panelButtons=new JPanel();
panelHit=new JPanel();
panelPlace=new JPanel();
panelTable=new JPanel();
panelHit.setLayout(new GridLayout(10,10));
panelPlace.setLayout(new GridLayout(10,10));
panelButtons.setLayout(new GridLayout(3,2));
for(int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
buttonBoat[i][j]=new JButton();
buttonAttack[i][j]=new JButton();
panelPlace.add(buttonBoat[i][j]);
panelHit.add(buttonAttack[i][j]);
buttonBoat[i][j].setActionCommand(i+","+j);
buttonBoat[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setActionCommand(i+","+j);
buttonAttack[i][j].setEnabled(false);
buttonBoat[i][j].addActionListener(this);
buttonAttack[i][j].addActionListener(this);
}
}
this.add(panelButtons);
this.add(panelHit);
this.add(panelPlace);
this.add(panelTable);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
playercui 类代码:
package sources;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class playergui extends JFrame implements ActionListener
{
JFrame framePlayer;
JPanel panelHit,panelPlace,panelButtons,panelTable;
JButton buttonBoat[][],buttonAttack[][];
public playergui()
{
this.setLayout(new GridLayout(2,2));
this.setTitle("Second Player");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,500);
this.setVisible(true);
panelButtons=new JPanel();
panelHit=new JPanel();
panelPlace=new JPanel();
panelTable=new JPanel();
panelHit.setLayout(new GridLayout(10,10));
panelPlace.setLayout(new GridLayout(10,10));
panelButtons.setLayout(new GridLayout(3,2));
for(int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
buttonBoat[i][j]=new JButton();
panelPlace.add(buttonBoat[i][j]);
buttonAttack[i][j]=new JButton();
panelHit.add(buttonAttack[i][j]);
buttonBoat[i][j].setActionCommand(i+","+j);
buttonBoat[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setBackground(Color.WHITE);
buttonAttack[i][j].setActionCommand(i+","+j);
buttonAttack[i][j].setEnabled(false);
buttonBoat[i][j].addActionListener(this);
buttonAttack[i][j].addActionListener(this);
}
}
this.add(panelButtons);
this.add(panelHit);
this.add(panelPlace);
this.add(panelTable);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
主类代码
package sources;
public class Maingui
{
public static void main(String []args)
{
Own_gui owg=new Own_gui();
playergui pgi= new playergui();
}
}
我在 netbeans 中运行这个程序,它使用按钮来获取输入。
【问题讨论】:
-
声明和初始化是两个不同的东西。你在哪里初始化
buttonBoat数组?
标签: java eclipse swing netbeans import