下面是小弟写的一段代码,想把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); }}
但是执行的时候提示下面的错误,这是怎么回事啊?
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); }}
解决方案 »
- dom4j 解析xml错误
- 两个面试编程题,菜鸟请教!在线等。。。。。。。。。。。。。。。。
- 关于文件与文件流!
- java连接oracle的时候如何配置
- 谁能给我一个连接sql server的例子? 谢谢
- 用JAVA在系统中怎么获取Unix下的时间格式?(10位数字那种..)
- 急,急,急,急,急的我在线等候!
- 用jdbc:odbc:odbcDataSource在连接本机的SQL Server数据源可以,没问题,但如果我想连到服务器上的数据源上时,用jdbc:odbc应如何写?
- 高分求救!!!(200)我回多问相同问题来给分的,急急急
- 好纠结 提供给APP的服务写在web项目上还是单独一个项目中
- 一个自动显示的问题
- 请教JDBC ResultSet的运行机制?
Panel pl=new Panel();
Button bt1=new Button();pl.add(btn1);
fr.add(pl,BorderLayout.SOUTH);
...
这个地方的代码敲错了:
button2A=new Button("2A");
subPanel1.add(button2C);
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class SeatClient extends Frame implements ActionListener { Panel subPanel1 = new Panel(new GridLayout(3,3));
Panel subPanel2 = new Panel(new GridLayout(4,6));
Panel subPanel3 = new Panel(new GridLayout(2,4)); JTextArea displayMessenger; Button allocateButton,
button1A,
button2A,
button3A,
button1B,
button2B,
button3B,
button1C,
button2C,
button3C, button4A,
button5A,
button6A,
button7A,
button4B,
button4C,
button5C;
InputStream is=null;
OutputStream os=null;
PrintWriter pw=null;
BufferedReader br=null;
Socket s; SeatClient(String title){
super(title);
this.addWindowListener(new WindowListener() {
public void windowClosed(WindowEvent e) {
} public void windowActivated(WindowEvent arg0) {
// TODO 自动生成方法存根
} public void windowClosing(WindowEvent arg0) {
System.exit(0);
} public void windowDeactivated(WindowEvent arg0) {
// TODO 自动生成方法存根
} public void windowDeiconified(WindowEvent arg0) {
// TODO 自动生成方法存根
} public void windowIconified(WindowEvent arg0) {
// TODO 自动生成方法存根
} public void windowOpened(WindowEvent arg0) {
// TODO 自动生成方法存根
}
}); //set the grids of the frame as(5,1)
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);
add(subPanel1); button2A=new Button("2A");
subPanel1.add(button2A);
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);
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 = new Button("4A");
button4A.addActionListener(this);
button4B = new Button("4B");
button4B.addActionListener(this);
button4C = new Button("4C");
button4C.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); }}
自动生成方法存根 那几段代码是不是和下面的是一个原理?
//dispose();By default, when the close button on a frame is clicked, nothing happens.
//This shows how to properly close the application:
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
Frame frame = (Frame)evt.getSource();
// Hide the frame
frame.setVisible(false);
// If the frame is no longer needed, call dispose
frame.dispose();
}
});
Panel pl=new Panel();
Button bt1=new Button();pl.add(btn1);
fr.add(pl,BorderLayout.SOUTH);
...
--------------------------------
up
Button btn[]=new Button[100];
for(int i=0;i<100;i++)
{
btn[i]=new Button(Intger.toString(i));
btn[i].addActionListener(this);
}
其它的我没看,太长了,看了头痛...
觉得说得好,就给分哈..
Container cp =jf.getContentPane();
cp.add(new JPanel());
这样就行了
import java.awt.*;
public class Panel1 {
public static void main(String[] args) {
Panel1 gui = new Panel1();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel panel = new JPanel(new GridLayout(2,2));
panel.setBackground(Color.DARK_GRAY);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JButton button = new JButton("yyc");
JButton button1 = new JButton("chunyou128");
panel.add(button);
panel.add(button1);
frame.getContentPane().add(BorderLayout.EAST,panel);
frame.setSize(250,200);
frame.setVisible(true);
}
}
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;/**//**
* @author HexUzHoNG Created on 2005-6-23
*
*/
public class GridLayoutDemo { public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
//设置为2列
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
GridData gd = new GridData(GridData.FILL_BOTH);
Button butOne = new Button(shell, SWT.PUSH);
butOne.setText("one");
butOne.setLayoutData(gd);
gd = new GridData(GridData.FILL_BOTH);
Button butTwo = new Button(shell, SWT.PUSH);
butTwo.setText("two");
butTwo.setLayoutData(gd);
gd = new GridData(GridData.FILL_BOTH);
Button butThree = new Button(shell, SWT.PUSH);
butThree.setText("three");
butThree.setLayoutData(gd);
gd = new GridData(GridData.FILL_BOTH);
Button butFour = new Button(shell, SWT.PUSH);
butFour.setText("four");
butFour.setLayoutData(gd);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}}