如何在JFram中添加JPanel?
下面是小弟写的一段代码,想把JFrame分成5个格子,然后在上面三个格子里分别添加一个JPanel,JPanel中再添加按钮。
但是执行的时候提示下面的错误,这是怎么回事啊?
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1015)
at java.awt.Container.add(Container.java:351)
at SeatClient.<init>(SeatClient.java:82)
at SeatClient.main(SeatClient.java:177)
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SeatClient extends JFrame implements ActionListener {
JPanel subPanel1 = new JPanel(new GridLayout(3,3));
JPanel subPanel2 = new JPanel(new GridLayout(4,6));
JPanel subPanel3 = new JPanel(new GridLayout(2,4));
JTextArea displayMessenger;
Button allocateButton,
button1A,
button2A,
button3A,
button1B,
button2B,
button3B,
button1C,
button2C,
button3C,
button4A,
button5A,
button6A,
button7A,
button4B,
button5B,
button6B,
button7B,
button4C,
button5C,
button6C,
button7C,
button4D,
button5D,
button6D,
button7D,
button4E,
button5E,
button6E,
button7E,
button4F,
button5F,
button6F,
button7F,
button8A,
button9A,
button8B,
button9B,
button8C,
button9C,
button8D,
button9D;
InputStream is=null;
OutputStream os=null;
PrintWriter pw=null;
BufferedReader br=null;
Socket s;
SeatClient(String title){
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//set the grids of the frame as(5,1)
getContentPane().setLayout(new GridLayout(5,1));
button1A=new Button("1A");
subPanel1.add(button1A);
button1B=new Button("1B");
subPanel1.add(button1B);
button1C=new Button("1C");
subPanel1.add(button1C);
getContentPane().add(subPanel1);
button2A=new Button("2A");
subPanel1.add(button2C);
button2B=new Button("2B");
subPanel1.add(button2B);
button2C=new Button("2C");
subPanel1.add(button2C);
button3A=new Button("3A");
subPanel1.add(button3A);
button3B=new Button("3B");
subPanel1.add(button3B);
button3C=new Button("3C");
subPanel1.add(button3C);
getContentPane().add(subPanel2);
//getContentPane().add(subPanel3);
displayMessenger = new JTextArea("Grey=free, click seat to reserve."+"\n\n"
+"Green= reserved, click seat to free."+"\n\n"
+"Red=allocated"+"\n");
add(displayMessenger);
allocateButton = new Button("Allocate");
add(allocateButton);
setSize(800,600);
//pack();
setVisible(true);
button1A.addActionListener(this);
button2A.addActionListener(this);
button3A.addActionListener(this);
button1B.addActionListener(this);
button2B.addActionListener(this);
button3B.addActionListener(this);
button1C.addActionListener(this);
button2C.addActionListener(this);
button3C.addActionListener(this);
button4A.addActionListener(this);
button4B.addActionListener(this);
button4C.addActionListener(this);
button4D.addActionListener(this);
button4E.addActionListener(this);
button4F.addActionListener(this);
button5A.addActionListener(this);
button5B.addActionListener(this);
button5C.addActionListener(this);
button5D.addActionListener(this);
button5E.addActionListener(this);
button5F.addActionListener(this);
button6A.addActionListener(this);
button6B.addActionListener(this);
button6C.addActionListener(this);
button6D.addActionListener(this);
button6E.addActionListener(this);
button6F.addActionListener(this);
button7A.addActionListener(this);
button7B.addActionListener(this);
button7C.addActionListener(this);
button7D.addActionListener(this);
button7E.addActionListener(this);
button7F.addActionListener(this);
button8A.addActionListener(this);
button8B.addActionListener(this);
button8C.addActionListener(this);
button8D.addActionListener(this);
button9A.addActionListener(this);
button9B.addActionListener(this);
button9C.addActionListener(this);
button9D.addActionListener(this);
try{
s=new Socket("",2000);
is=s.getInputStream();
os=s.getOutputStream();
pw=new PrintWriter(os,true);
br=new BufferedReader(new InputStreamReader(is));
}
catch(IOException e){
System.out.println("Error connecting wth the Server "+e);
}
}
public void actionPerformed(ActionEvent e){
}
public static void main(String[] args){
//prompt user to enter name
String name= JOptionPane.showInputDialog("What is your name?");
SeatClient c = new SeatClient(name);
}
}