| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1044 人关注过本帖
标题:图形编程的问题,请帮我看看
只看楼主 加入收藏
caochuan2000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:35
注 册:2009-4-11
结帖率:76.47%
收藏
已结贴  问题点数:20 回复次数:7 
图形编程的问题,请帮我看看
我检查了很多次了,代码觉得一点问题都没有啊,为什么编译的时候会出现:
Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at myawt1.YourFrame.<init>(YourFrame.java:23)
    at myawt1.YourFrame.main(YourFrame.java:143)
说我有空指针,我查了没问题啊,请哪位大大帮我看下,小弟初学

package myawt1;
import java.awt.*;
import java.awt.event.*;

public class YourFrame extends Frame{
    private Panel BorderLayoutPanel;
    private Panel FlowLayoutPanel;
    private Panel GridLayoutPanel;
    private Panel CardLayoutPanel;
    public YourFrame(String name)
    {
        super(name);
        setSize(400, 300);
        setLocation(100, 100);
        setBackground(Color.CYAN);
        addWindowListener(new YourWindowListener());
        setLayout(new GridLayout(2,2));
        putBorderLayoutPanel();
        putFlowLayoutPanel();
        putGridLayoutPanel();
        putCardLayoutPanel();
        add(BorderLayoutPanel);
        add(FlowLayoutPanel);
        add(GridLayoutPanel);
        add(CardLayoutPanel);
        MenuBar mb=new MenuBar();
        Menu m1=new Menu("文件");
        Menu m2=new Menu("编辑");
        Menu m3=new Menu("运行");
        Menu m4=new Menu("帮助");
        MenuItem mi1=new MenuItem("新建");
        MenuItem mi2=new MenuItem("打开");
        MenuItem mi3=new MenuItem("保存");
        MenuItem mi4=new MenuItem("关闭");
        mi4.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
        MenuItem mi5=new MenuItem("复制");
        MenuItem mi6=new MenuItem("粘贴");
        MenuItem mi7=new MenuItem("查找");
        MenuItem mi8=new MenuItem("删除");
        MenuItem mi9=new MenuItem("运行");
        MenuItem mi10=new MenuItem("暂停");
        mb.add(m1);
        mb.add(m2);
        mb.add(m3);
        mb.add(m4);
        m1.add(mi1);
        m1.add(mi2);
        m1.add(mi3);
        m1.add(mi4);
        m2.add(mi5);
        m2.add(mi6);
        m2.add(mi7);
        m2.add(mi8);
        m3.add(mi9);
        m4.add(mi10);
        setMenuBar(mb);
    }
    public void putBorderLayoutPanel()
    {
        Panel BorderLayoutPanel=new Panel(new BorderLayout());
        Button btn1=new Button("东邪黄老邪");
         Button btn2=new Button("西毒欧阳锋");
         Button btn3=new Button("南帝段王爷");
         Button btn4=new Button("北丐洪七公");
         Button btn5=new Button("中神通王重阳");
         BorderLayoutPanel.add(btn1,BorderLayout.EAST);
         BorderLayoutPanel.add(btn2,BorderLayout.WEST);
         BorderLayoutPanel.add(btn3,BorderLayout.SOUTH);
         BorderLayoutPanel.add(btn4,BorderLayout.NORTH);
         BorderLayoutPanel.add(btn5,BorderLayout.CENTER);
    }
    public void putFlowLayoutPanel()
    {
        Panel FlowLayoutPanel=new Panel(new FlowLayout());
        Button btn6=new Button("确定");
        btn6.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ((Button)e.getSource()).setLabel("你按了确定");;
            }
        });
        Button btn7=new Button("取消");
        Button btn8=new Button("退出");
        btn8.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });        
        TextArea ta=new TextArea();
        FlowLayoutPanel.add(btn6);
        FlowLayoutPanel.add(btn7);
        FlowLayoutPanel.add(btn8);
        FlowLayoutPanel.add(ta);
    }
    public void putGridLayoutPanel()
    {
        Panel GridLayoutPanel=new Panel(new GridLayout(2,2));
        Button btn9=new Button("毒蛇神殿");
        Button btn10=new Button("风暴要塞");
        Button btn11=new Button("海加尔山");
        Button btn12=new Button("黑暗神庙");
        GridLayoutPanel.add(btn9);
        GridLayoutPanel.add(btn10);
        GridLayoutPanel.add(btn11);
        GridLayoutPanel.add(btn12);
    }
    public void putCardLayoutPanel()
    {
        final CardLayout cl=new CardLayout();
        final Panel CardLayoutPanel=new Panel(cl);
        ActionListener al1=new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                cl.next(CardLayoutPanel);
            }         
        };
        Button btn13=new Button("凯尔萨斯");
        Button btn14=new Button("瓦斯琪");
        Button btn15=new Button("阿克蒙德");
        Button btn16=new Button("伊利丹·怒风");
        btn13.addActionListener(al1);
        btn14.addActionListener(al1);
        btn15.addActionListener(al1);
        btn16.addActionListener(al1);
        CardLayoutPanel.add(btn13, "1");
        CardLayoutPanel.add(btn14, "2");
        CardLayoutPanel.add(btn15, "3");
        CardLayoutPanel.add(btn16, "4");
    }
    public static void main(String[] args)throws Exception
    {
        YourFrame f=new YourFrame("wow!");
        f.show();
    }
   }
class YourWindowListener extends WindowAdapter
{
    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
}
搜索更多相关主题的帖子: 图形 
2010-01-02 11:02
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:20 
private Panel BorderLayoutPanel;
private Panel FlowLayoutPanel;
private Panel GridLayoutPanel;
private Panel CardLayoutPanel;   

面板没有实例化

人总需相信自己,人总需依靠自己. ---永远的Beyond---
2010-01-02 12:58
caochuan2000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:35
注 册:2009-4-11
收藏
得分:0 
实例化了啊,我在每个方法里面第1句就实例化了
2010-01-02 13:58
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:0 
面板最好是在构造函数中实例化。


人总需相信自己,人总需依靠自己. ---永远的Beyond---
2010-01-02 18:34
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:0 
在构造函数中实例化:
程序代码:
public YourFrame(String name)
    {
        super(name);
        BorderLayoutPanel=new Panel();
        FlowLayoutPanel=new Panel();
        GridLayoutPanel=new Panel();
        CardLayoutPanel=new Panel();
        setSize(400, 300);
        setLocation(100, 100);
把方法里实例化的语句去掉.

人总需相信自己,人总需依靠自己. ---永远的Beyond---
2010-01-02 18:40
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:0 
你在每个方法里new面板的时候是重新声明的.你把方法内Panel声明去掉也是可以的.
程序代码:
public void putBorderLayoutPanel()
    {
        BorderLayoutPanel=new Panel(new BorderLayout());
        Button btn1=new Button("东邪黄老邪");
         Button btn2=new Button("西毒欧阳锋");
         Button btn3=new Button("南帝段王爷");
         Button btn4=new Button("北丐洪七公");
         Button btn5=new Button("中神通王重阳");
         BorderLayoutPanel.add(btn1,BorderLayout.EAST);
         BorderLayoutPanel.add(btn2,BorderLayout.WEST);
         BorderLayoutPanel.add(btn3,BorderLayout.SOUTH);
         BorderLayoutPanel.add(btn4,BorderLayout.NORTH);
         BorderLayoutPanel.add(btn5,BorderLayout.CENTER);
    }
    public void putFlowLayoutPanel()
    {
        FlowLayoutPanel=new Panel(new FlowLayout());
        Button btn6=new Button("确定");
        btn6.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                ((Button)e.getSource()).setLabel("你按了确定");;
            }
        });
        Button btn7=new Button("取消");
        Button btn8=new Button("退出");
        btn8.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e) 
            {
                System.exit(0);
            }
        });        
        TextArea ta=new TextArea();
        FlowLayoutPanel.add(btn6);
        FlowLayoutPanel.add(btn7);
        FlowLayoutPanel.add(btn8);
        FlowLayoutPanel.add(ta);
    }
    public void putGridLayoutPanel()
    {
        GridLayoutPanel=new Panel(new GridLayout(2,2));
        Button btn9=new Button("毒蛇神殿");
        Button btn10=new Button("风暴要塞");
        Button btn11=new Button("海加尔山");
        Button btn12=new Button("黑暗神庙");
        GridLayoutPanel.add(btn9);
        GridLayoutPanel.add(btn10);
        GridLayoutPanel.add(btn11);
        GridLayoutPanel.add(btn12);
    }
    public void putCardLayoutPanel()
    {
        final CardLayout cl=new CardLayout();
        CardLayoutPanel=new Panel(cl);//这个面板如果要声明为final的话必须在上面定义时声明
        ActionListener al1=new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                cl.next(CardLayoutPanel);
            }          
        };
        Button btn13=new Button("凯尔萨斯");
        Button btn14=new Button("瓦斯琪");
        Button btn15=new Button("阿克蒙德");
        Button btn16=new Button("伊利丹·怒风");
        btn13.addActionListener(al1);
        btn14.addActionListener(al1);
        btn15.addActionListener(al1);
        btn16.addActionListener(al1);
        CardLayoutPanel.add(btn13, "1");
        CardLayoutPanel.add(btn14, "2");
        CardLayoutPanel.add(btn15, "3");
        CardLayoutPanel.add(btn16, "4");
    }

人总需相信自己,人总需依靠自己. ---永远的Beyond---
2010-01-02 18:50
caochuan2000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:35
注 册:2009-4-11
收藏
得分:0 
谢谢,学习了
2010-01-03 13:33
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
兄弟,空指针异常不应该啊

可惜不是你,陪我到最后
2010-01-04 09:36
快速回复:图形编程的问题,请帮我看看
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040137 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved