急求解答!!

aaa1810 2008-03-11 12:07:48
import javawt.*;
import javawt.event.*;
class AWT4 extends WindowAdapter implements ActionListener,WindowListener
{
Frame f=new Frame("模拟面板3号实验品");
MenuBar mb;
Menu mf,me,mh,ms;
CheckboxMenuItem cm1;
Panel p1,p2;
String a[]={"1","2","3","4","5","6","7","8","9","*","0","#"};
Button b[];
TextField tf=new TextField(50);

public void setMenuBar()
{
mf=new Menu("文件(F)");
me=new Menu("编辑(E)");
mh=new Menu("帮助(H)");
ms=new Menu("菜单栏示范");
mb.add(mf);
mb.add(me);
mb.add(mh);
mb.add(ms);
mf.add(new MenuItem("打开"));
mf.add(new MenuItem("保存"));
mf.add(new MenuItem("关闭"));
mf.add(new CheckboxMenuItem("我是NB",true));
mf.add(new CheckboxMenuItem("我不是NB"));
}
public void listenter()
{
f.addWindowListener(this);
mf.addActionListener(this);
me.addActionListener(this);
mh.addActionListener(this);
ms.addActionListener(this);
}

public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="打开")
tf.setText("已经打开");
if(e.getActionCommand()=="保存")
tf.setText("已经保存");
if(e.getActionCommand()=="关闭")
System.exit(0);
}
public void display()
{
f.setSize(480,360);
f.setMenuBar(mb);
f.setLayout(new FlowLayout());
p1=new Panel();
p1.add(tf);
p2=new Panel();
p2.setLayout(new GridLayout(4,3,5,5));
for(int i=0; i <a.length;i++)
{
Button b[]=new Button[a.length];
p2.add(b[i]);
}

f.setVisible(true);
}
public static void main(String arg[])
{
(new AWT4()).display();
}

}

编译通过
运行失败,说是没找到指针,这是怎么回事?求指正!
...全文
57 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
w358581619 2008-03-11
  • 打赏
  • 举报
回复
Button b[] = new Button[a.length];
p2.add(b[i]);
老紫竹 2008-03-11
  • 打赏
  • 举报
回复
楼上正解
Button b[] = new Button[a.length];
p2.add(b[i]);

这里声明了一个对象数组,可是没有对数组里面的对象进行初始化。导致b[i] 为null
临远 2008-03-11
  • 打赏
  • 举报
回复


import java.awt.*;
import java.awt.event.*;

class AWT4 extends WindowAdapter implements ActionListener,WindowListener {
Frame f=new Frame("模拟面板3号实验品");
MenuBar mb;
Menu mf,me,mh,ms;
CheckboxMenuItem cm1;
Panel p1,p2;
String a[]={"1","2","3","4","5","6","7","8","9","*","0","#"};
Button b[];
TextField tf=new TextField(50);

public void setMenuBar() {
mf=new Menu("文件(F)");
me=new Menu("编辑(E)");
mh=new Menu("帮助(H)");
ms=new Menu("菜单栏示范");
mb.add(mf);
mb.add(me);
mb.add(mh);
mb.add(ms);
mf.add(new MenuItem("打开"));
mf.add(new MenuItem("保存"));
mf.add(new MenuItem("关闭"));
mf.add(new CheckboxMenuItem("我是NB",true));
mf.add(new CheckboxMenuItem("我不是NB"));
}

public void listenter() {
f.addWindowListener(this);
mf.addActionListener(this);
me.addActionListener(this);
mh.addActionListener(this);
ms.addActionListener(this);
}

public void windowClosing(WindowEvent e) {
System.exit(0);
}

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="打开")
tf.setText("已经打开");
if(e.getActionCommand()=="保存")
tf.setText("已经保存");
if(e.getActionCommand()=="关闭")
System.exit(0);
}

public void display() {
f.setSize(480,360);
f.setMenuBar(mb);
f.setLayout(new FlowLayout());
p1=new Panel();
p1.add(tf);
p2=new Panel();
p2.setLayout(new GridLayout(4,3,5,5));

for(int i=0; i <a.length;i++) {
//Button b[]=new Button[a.length];
//p2.add(b[i]);
p2.add(new Button("" + i));
}
f.add(p2);

f.setVisible(true);
}
public static void main(String arg[]) {
(new AWT4()).display();
}

}


62,634

社区成员

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

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