如何在JFram中添加JPanel?

zhaozy1982 2007-05-20 07:34:03
下面是小弟写的一段代码,想把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);

}

}
...全文
572 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
s74110s 2007-05-28
  • 打赏
  • 举报
回复
无语
chunyou128 2007-05-28
  • 打赏
  • 举报
回复
import org.eclipse.swt.SWT;
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();
}

}
chunyou128 2007-05-28
  • 打赏
  • 举报
回复
import javax.swing.*;
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);
}
}
jianhuaitianxia 2007-05-27
  • 打赏
  • 举报
回复
汗代码的说
thinker87 2007-05-27
  • 打赏
  • 举报
回复
JFrame jf=new JFrame();
Container cp =jf.getContentPane();
cp.add(new JPanel());
这样就行了
zhaozy1982 2007-05-27
  • 打赏
  • 举报
回复
怎么给分啊?
没看到给分的按钮嘛、、、、、、、
yuyu622 2007-05-27
  • 打赏
  • 举报
回复
你把你定义Button改为数据,注册事件也用数组,那样会好些.
Button btn[]=new Button[100];
for(int i=0;i<100;i++)
{
btn[i]=new Button(Intger.toString(i));
btn[i].addActionListener(this);
}
其它的我没看,太长了,看了头痛...
觉得说得好,就给分哈..
chunyou128 2007-05-25
  • 打赏
  • 举报
回复
Frame fr=new Frame();
Panel pl=new Panel();
Button bt1=new Button();

pl.add(btn1);
fr.add(pl,BorderLayout.SOUTH);
...
--------------------------------
up
zhaozy1982 2007-05-24
  • 打赏
  • 举报
回复
自动生成方法存根?

能不能解释一下是什么意思啊?
自动生成方法存根 那几段代码是不是和下面的是一个原理?
//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();
}
});
pudexiang 2007-05-22
  • 打赏
  • 举报
回复
下面是修改过的代码。在AWT的Frame添加的Panel:
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);

}

}


pudexiang 2007-05-22
  • 打赏
  • 举报
回复
LZ定义那么多button却不实例化,运行出来一大堆Exception
sun_shine_17 2007-05-22
  • 打赏
  • 举报
回复
看到那么多Button都晕了...
zhaozy1982 2007-05-22
  • 打赏
  • 举报
回复
呵呵,多谢。后来才发现的!按钮太多了。。。。。。

如果我想把按钮定义和添加用数组的方式实现,那应该怎么样写呢?
wssssw 2007-05-21
  • 打赏
  • 举报
回复
不得已,不得已
  • 打赏
  • 举报
回复
看了这段代码,惊为天人
wssssw 2007-05-20
  • 打赏
  • 举报
回复
晕啊,楼主这么粗心的
这个地方的代码敲错了:
button2A=new Button("2A");
subPanel1.add(button2C);
koutan 2007-05-20
  • 打赏
  • 举报
回复
Frame fr=new Frame();
Panel pl=new Panel();
Button bt1=new Button();

pl.add(btn1);
fr.add(pl,BorderLayout.SOUTH);
...
  • 打赏
  • 举报
回复
寒LZ的代码
zhaozy1982 2007-05-20
  • 打赏
  • 举报
回复
再问一句,如果要用AWT中的Frame,添加Panel,来完成这个界面应该怎么改变啊?
zhaozy1982 2007-05-20
  • 打赏
  • 举报
回复
不好意思,能不能只用GridLayout,因为这是老师要求的:(

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧