import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class NamePanel extends Panel
{
Label name;TextField text;
NamePanel()
{
name=new Label( "姓名 ");
text=new TextField(10);
setLayout(new GridLayout(1,2));
add(name);add(text);
}
}
class SexPanel extends Panel
{
Label xingbie;
Checkbox female,male;
CheckboxGroup sex;
GridLayout a=new GridLayout(1,2);
Panel p1=new Panel();
SexPanel()
{
this.setLayout(a);
sex=new CheckboxGroup();
xingbie=new Label( "性别 ");
female=new Checkbox( "女 ",true,sex);
male=new Checkbox( "男 ",false,sex);
p1.add(female);p1.add(male);
add(xingbie);add(p1);
}
}
class Interest extends Panel
{
Label interests;
Checkbox box1,box2,box3,box4;
GridLayout b=new GridLayout(1,2);
Panel p2=new Panel();
Interest()
{
setLayout(b);
interests= new Label( "爱好 ");
box1=new Checkbox( "体育 ");box2=new Checkbox( "游戏 ");
box3=new Checkbox( "唱歌 ");box4=new Checkbox( "跳舞 ");
p2.add(box1);p2.add(box2);p2.add(box3);p2.add(box4);
add(interests);
add(p2);
}
}
class All extends Panel
{
Button submit,reset;
NamePanel allname;
SexPanel allsex;
Interest allinterest;
GridLayout c=new GridLayout(4,1);
Panel p=new Panel();
All()
{
setLayout(c);
p.add(submit);p.add(reset);
allname=new NamePanel();
allsex=new SexPanel();
allinterest=new Interest();
add(allname);add(allsex);add(allinterest);add(p);
}
}
public class Mytest extends Applet implements ActionListener
{
All test;
Label success;
CardLayout card;
public void init()
{
card=new CardLayout();
setLayout(card);
test=new All();
add( " ",test);
success=new Label( "成功发送 ");
add( " ",success);
test.submit.addActionListener(this);
test.reset.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==test.submit)
{
card.last(this);
}
else if(e.getSource()==test.reset)
{
test.allname.text.setText(null);
test.allsex.female.setState(false);
test.allsex.male.setState(false);
test.allinterest.box1.setState(false);
test.allinterest.box2.setState(false);
test.allinterest.box3.setState(false);
test.allinterest.box4.setState(false);
}
}
}
编译之后我用 <applet code= "Mytest.java " height=300 width=300 />
运行时候在浏览器里面就提示这个java lang nullpointerException
错在哪里呢?
------解决方案--------------------
你添加的按扭没有实例化
------解决方案--------------------
Button submit,reset;
======================================
在什么地方new的?