编译通过
但运行出现以下问题
Exception in thread "main" java.lang.NullPointerException
at Calculator.Setframe(Calculator.java:53)
at Calculator.main(Calculator.java:98)
代码如下
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
public class Calculator implements ActionListener
{
private int i,j,k,l,r,t;
JPanel p1,p2,p3;
private int num2[]=new int[10];
private JFrame fm = new JFrame("计算器");
private JTextField tf= new JTextField(22);
private JButton bt1 = new JButton("Backspace");
private JButton bt2 = new JButton("CE");
private JButton bt3 = new JButton("C");
JButton bt[]=new JButton[10];
private JButton bts[]=new JButton[14];
private String str[] = {"MC","/","sqrc","MR","*","%","MS","-","1/x","M+","+/-",".","+","="};
private String num[]={"7","8","9","4","5","6","1","2","3","0"};
String str1;
JMenuBar mb = new JMenuBar();
JMenu fileMenu1 = new JMenu("编辑(E)");
JMenu fileMenu2 =new JMenu("查看(V)");
JMenu fileMenu3 =new JMenu("帮助(H)");
public void Setframe()
{
Container c=fm.getContentPane();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel(new GridLayout(4,6));
//p1.setLayout(new FlowLayout()); 默认为流式布局
tf.setEditable(false);
p1.add(tf);
p2.add(bt1);
p2.add(bt2);
p2.add(bt3);
for(i=0;i<10;i++)
{
j=0;
bt[i]=new JButton(num[i]);
j++;
}
i=0;
j=0;
for(k=0;k<=3;k++)
for(l=0;l<=5;l++)
{
if(l==0||l==4||l==5||(l==2&&k==3)||(l==3&&k==3))
{
p3.add(bts[i]=new JButton(str[i]));
i++;
bts[i].addActionListener(this);
}
else
{
p3.add(bt[j]);
j++;
bt[j].addActionListener(this);
}
}
c.setLayout(new FlowLayout()); //注意Layout L为大写
//c.add(p1,BorderLayout.NORTH);
c.add(p1);
c.add(p2);
c.add(p3);
fm.setSize(260,245);
fm.addWindowListener(new WinClose());
fm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
r=0;
if(e.getSource()==bt3)
{tf.setText(""); r=0;}
for(t=0;t<=9;t++)
{
if(e.getSource()==bt[t])
{
tf.setText(tf.getText()+e.getActionCommand());
num2[r]= Integer.parseInt(bt[t].getText());
r++;
}
}
if(e.getSource()==bts[1]) {tf.setText(tf.getText()+e.getActionCommand()); k=1;}
if(e.getSource()==bts[4]) {tf.setText(tf.getText()+e.getActionCommand()); k=2;}
if(e.getSource()==bts[7]) {tf.setText(tf.getText()+e.getActionCommand()); k=3;}
if(e.getSource()==bts[12]) {tf.setText(tf.getText()+e.getActionCommand()); k=4;}
if(e.getSource()==bts[13]) {if(r==2&&k==1) {l=num2[0]/num2[1]; r=0;}
if(r==2&&k==2) {l=num2[0]*num2[1]; r=0;}
if(r==2&&k==3) {l=num2[0]-num2[1]; r=0;}
if(r==2&&k==4) {l=num2[0]+num2[1]; r=0;}
str1 = Integer.toString(l);
tf.setText(str1);
}
}
public void init()
{
fileMenu1.add("复制(C) ctrl+c");
fileMenu1.add("粘贴(V) ctrl+v");
fileMenu2.add("标准型(T)");
fileMenu2.add("科学型(S)");
fileMenu2.add("数字分组(T)");
fileMenu3.add("帮助主题(H)");
fileMenu3.add("关于计算机(A)");
mb.add(fileMenu1);
mb.add(fileMenu2);
mb.add(fileMenu3);
fm.setJMenuBar(mb);
}
public static void main(String arg[])
{
Calculator Cal=new Calculator();
Cal.init();
Cal.Setframe();
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ //覆盖WindowAdapter类中同名方法,单击窗口关闭按钮时产生该事件
System.exit(0); //结束程序运行,关闭窗口
}
}
[此贴子已经被作者于2006-10-29 23:56:15编辑过]