求助,急...!
本人初学者,遇到一个问题,麻烦哪位朋友帮忙解决哈。。。。。万分感谢!!!
问题: 编译一个简单的计算机图形窗口,要求(在点击相应数字时在文本域中显示点击数字)
可是编译出.class文件,并也显示出图形窗口,但点击上面的数字时,编译器报错。
求解决方法. 谢谢.........(代码是自己编写,如有不规范的地方请多多见谅)
错误提示如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at awt.layout.Calculator$TestListener.actionPerformed(Calculator.java:53)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
------------------------------------------------------------------------------------------------
at awt.layout.Calculator$TestListener.actionPerformed(Calculator.java:53)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
------------------------------------------------------------------------------------------------
原代码如下:
import java.awt.*;
import java.awt.event.*;
import java.awt.event.*;
public class Calculator
{
Frame f;
Button b;
TextField t;
Panel p;
public static void main(String[] args)
{
Calculator c = new Calculator();
c.init();
}
public void init()
{
Frame f = new Frame("计算器");
TextField t = new TextField(50);
Panel p = new Panel();
TestListener ts = new TestListener();
p.setLayout(new GridLayout(6,5));
Button [] bt =new Button[30];
String [] buttonname ={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+","a","b","c","d"};
for(int i=0;i<=29;i++)
{
p.add(bt[i] = new Button(buttonname[i]));
bt[i].addActionListener(ts);
}
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
f.add(t,BorderLayout.NORTH);
f.add(p,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
public class TestListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
t.setText(s);
}
}
}
{
Frame f;
Button b;
TextField t;
Panel p;
public static void main(String[] args)
{
Calculator c = new Calculator();
c.init();
}
public void init()
{
Frame f = new Frame("计算器");
TextField t = new TextField(50);
Panel p = new Panel();
TestListener ts = new TestListener();
p.setLayout(new GridLayout(6,5));
Button [] bt =new Button[30];
String [] buttonname ={"MC","MR","MS","M+","M-","←","CE","C","±","√","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","=","0",".","+","a","b","c","d"};
for(int i=0;i<=29;i++)
{
p.add(bt[i] = new Button(buttonname[i]));
bt[i].addActionListener(ts);
}
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
f.add(t,BorderLayout.NORTH);
f.add(p,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
public class TestListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
t.setText(s);
}
}
}