| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 426 人关注过本帖
标题:[求助]初学者的小问题
只看楼主 加入收藏
mctracy84082
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-11-27
收藏
 问题点数:0 回复次数:4 
[求助]初学者的小问题
import java.awt.*;
import java.awt.event.*;
class WindowChoice extends Frame implements ItemListener,ActionListener
{
Choice choice;
TextField text;
TextArea area;
Button ad,del;
WindowChoice()
{
setLayout(new FlowLayout());
choice=new Choice();
text=new TextField(8);
area=new TextArea(8,25);
choice.add("音乐天地");
choice.add("无数天地");
choice.add("象棋乐园");
choice.add("交友聊天");
ad.addActionListener(this);
del.addActionListener(this);
text.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(del);
add(ad);
add(area);
add(text);
setSize(200,200);
setVisible(true);
validate();
}
public void itemStateChanged(ItemEvent e)
{
String name=choice.getSelectedItem();
int index=choice.getSelectedIndex();
area.setText("\n"+index+":"+name);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ad||e.getSource()==text)
{
String name=text.getText();
choice.add(name);
choice.select(name);
area.append("\n列表添加"+name);

}
else if(e.getSource()==del)
{
area.append("\n列表删除"+choice.getSelectedItem());
choice.remove(choice.getSelectedIndex());
}
}
}
public class Example6
{
public static void main(String [] args)
{
new WindowChoice();
}
}

出现Exception in thread "main" java.lang.NullPointerException
at WindowChoice.<init>(Example6.java:19)
at Example6.main(Example6.java:59)
刚开始学习java各位高手能指点一下么,感激涕零!!!!
搜索更多相关主题的帖子: Choice choice import 音乐 
2006-11-27 21:49
禹_二
Rank: 1
等 级:新手上路
帖 子:661
专家分:0
注 册:2006-9-13
收藏
得分:0 
你的两个按钮都没有实例化,ad=new Button("添加");del.....
看你初学,出现这种问题可以原谅,以后细心点.

2006-11-27 22:06
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 
Exception in thread "main" java.lang.NullPointerException
"说明是引用错误,即你声明了对象但没有创建就使用,会出现异常的"


import java.awt.*;
import java.awt.event.*;
class WindowChoice extends Frame implements ItemListener,ActionListener
{
Choice choice;
TextField text;
TextArea area;
Button ad,del;
WindowChoice()
{
setLayout(new FlowLayout());
choice=new Choice();
text=new TextField(8);
area=new TextArea(8,25);
choice.add(\"音乐天地\");
choice.add(\"无数天地\");
choice.add(\"象棋乐园\");
choice.add(\"交友聊天\");
ad=new Button(\"添加\"); //光声明了没有创佳对象
del=new Button(\"删除\");

ad.addActionListener(this);
del.addActionListener(this);
text.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(del);
add(ad);
add(area);
add(text);
setSize(200,200);
setVisible(true);
validate();
}
public void itemStateChanged(ItemEvent e)
{
String name=choice.getSelectedItem();
int index=choice.getSelectedIndex();
area.setText(\"\n\"+index+\":\"+name);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ad||e.getSource()==text)
{
String name=text.getText();
choice.add(name);
choice.select(name);
area.append(\"\n列表添加\"+name);

}
else if(e.getSource()==del)
{
area.append(\"\n列表删除\"+choice.getSelectedItem());
choice.remove(choice.getSelectedIndex());
}
}
}
public class Example6
{
public static void main(String [] args)
{
new WindowChoice();
}
}

unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-11-27 22:11
禹_二
Rank: 1
等 级:新手上路
帖 子:661
专家分:0
注 册:2006-9-13
收藏
得分:0 
功能也不行.

2006-11-27 22:16
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 
嗯 应该是"表单"练习的小程序
既然是AWT 最好再加个 窗口监听

addWindowListener( new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
//这样能把关掉窗口

unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-11-27 22:22
快速回复:[求助]初学者的小问题
数据加载中...
 
   



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

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