import java.awt.*;
import java.awt.event.*;class Mypanel extends Panel implements ActionListener
{
Button button1,button2;
Color backColor;
Mypanel()
{
button1=new Button("确定");
button1=new Button("取消");
add(button1);
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
setLayout(new FlowLayout());
setBackground(Color.pink);
backColor=getBackground();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
setBackground(Color.cyan);
}
else if(e.getSource()==button2)
{
setBackground(backColor);
}
}
}
class WindowPanel extends Frame implements ActionListener
{
Mypanel panel1,panel2;
Button button;
WindowPanel()
{
setLayout(new FlowLayout());
panel1=new Mypanel();
panel2=new Mypanel();
button=new Button("退出");
add(panel1);
add(panel2);
add(button);
button.addActionListener(this);
setBounds(60,60,200,200);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public class Example7_10
{
public static void main(String[] args)
{
new WindowPanel();
}
}
运行是出现下面的错误:Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at Mypanel.<init>(Example7_10.java:13)
at WindowPanel.<init>(Example7_10.java:41)
at Example7_10.main(Example7_10.java:62)我也找不到哪个对象在使用前没初始化,请各位大虾帮忙
import java.awt.event.*;class Mypanel extends Panel implements ActionListener
{
Button button1,button2;
Color backColor;
Mypanel()
{
button1=new Button("确定");
button1=new Button("取消");
add(button1);
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
setLayout(new FlowLayout());
setBackground(Color.pink);
backColor=getBackground();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
setBackground(Color.cyan);
}
else if(e.getSource()==button2)
{
setBackground(backColor);
}
}
}
class WindowPanel extends Frame implements ActionListener
{
Mypanel panel1,panel2;
Button button;
WindowPanel()
{
setLayout(new FlowLayout());
panel1=new Mypanel();
panel2=new Mypanel();
button=new Button("退出");
add(panel1);
add(panel2);
add(button);
button.addActionListener(this);
setBounds(60,60,200,200);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public class Example7_10
{
public static void main(String[] args)
{
new WindowPanel();
}
}
运行是出现下面的错误:Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at Mypanel.<init>(Example7_10.java:13)
at WindowPanel.<init>(Example7_10.java:41)
at Example7_10.main(Example7_10.java:62)我也找不到哪个对象在使用前没初始化,请各位大虾帮忙
button2 = new Button("取消");