我编个像程序再带那种计算器,我想用循环给BUTTON复制 提示错误Exception in thread "main" java.lang.NullPointerExceptionat Calculator.<init>(Calculator.java:67)at Calculator.main(Calculator.java:90)
代码如下 半成品。。
import java.awt.*;
import java.awt.event.*;
public class Calculator extends WindowAdapter implements ActionListener{
String str;
Button buttonname1[],buttonname2[];
Frame f;
Panel p1=new Panel(),p2=new Panel(),p3=new Panel(),p4=new Panel(),p5=new Panel();MenuBar mb;
Checkbox c1=new Checkbox("度");
Checkbox c2=new Checkbox("弧度");
Checkbox c3=new Checkbox("梯度");
Menu m1=new Menu("查看");
Menu m2=new Menu("编辑");
Menu m3=new Menu("帮助");
TextField t1=new TextField(100);
String Button1[]={"MC","MR","MS","M+","M-","退格","CE","C","正负","根号","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-"," ","0"," ",".","+","="};String Button2[]={" ","Inv","In","(",")","Int","sinh","sin","x²","阶乘","dms","cosh","cos","Xy","y√x","π","tanh","立方","立方根","F-E","Exp","Mod","log","10的x次方"};
public void addMenu(Frame f){
MenuBar mb=new MenuBar();
f.setMenuBar(mb);
mb.add(m1);mb.add(m2);mb.add(m3);
m1.add(new MenuItem("标准型",new MenuShortcut(KeyEvent.VK_T)));
m1.add(new MenuItem("科学性",new MenuShortcut(KeyEvent.VK_S)));
m1.add(new MenuItem("程序员",new MenuShortcut(KeyEvent.VK_P)));
m1.add(new MenuItem("统计信息",new MenuShortcut(KeyEvent.VK_A)));
m1.addSeparator();
m1.add(new MenuItem("历史记录",new MenuShortcut(KeyEvent.VK_V)));
m1.add(new MenuItem("数字分组",new MenuShortcut(KeyEvent.VK_I)));
m1.addSeparator();
m1.add(new MenuItem("基本",new MenuShortcut(KeyEvent.VK_B)));m1.add(new MenuItem("单位转换",new MenuShortcut(KeyEvent.VK_U)));
m1.add(new MenuItem("日期计算",new MenuShortcut(KeyEvent.VK_D)));
Menu m12=new Menu("工作表");
m1.add(new MenuItem("抵押",new MenuShortcut(KeyEvent.VK_M)));
m1.add(new MenuItem("汽车租赁",new MenuShortcut(KeyEvent.VK_V)));
m1.add(new MenuItem("油耗(mpg)",new MenuShortcut(KeyEvent.VK_F)));
m1.add(new MenuItem("油耗(l/100km)",new MenuShortcut(KeyEvent.VK_U)));
m2.add(new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C)));
m2.add(new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_P)));
m2.addSeparator();Menu m22=new Menu("历史记录");
m2.add(new MenuItem("复制历史记录",new MenuShortcut(KeyEvent.VK_I)));
m2.add(new MenuItem("编辑",new MenuShortcut(KeyEvent.VK_E)));
m2.add(new MenuItem("取消编辑",new MenuShortcut(KeyEvent.VK_N)));
m2.add(new MenuItem("清除",new MenuShortcut(KeyEvent.VK_L)));
m3.add(new MenuItem("查看帮助",new MenuShortcut(KeyEvent.VK_V)));
m3.addSeparator();
m3.add(new MenuItem("关于计算器",new MenuShortcut(KeyEvent.VK_B)));
}
Calculator(){
t1.setEditable(false);
f=new Frame("计算器");
addMenu(f);f.setSize(600,400);f.setLocation(200,200);
p2.add(c1);p2.add(c2);p2.add(c3);
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i=0;i<25;i++){
buttonname1[i] = new Button(Button2[i]);
p3.add(buttonname1[i]);
buttonname1[i].addActionListener(this);}
p4.add(p2);p4.add(p3);p4.setLayout(new BorderLayout());
p4.add(p3,"Center");p4.add(p4,"South");
for(int j=0;j<30;j++){
buttonname1[j]=new Button(Button1[j]);
p5.add(buttonname2[j]);
buttonname2[j].addActionListener(this);
}
p1.add(t1);p1.add(p4);p1.add(p5);
p1.setLayout(new BorderLayout());
p1.add(t1,"north");p1.add(p4,"West");p1.add(p5,"East");
f.addWindowListener(this);f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
//if(e.getSource()==)}
public static void main(String args[]){
new Calculator();}}
求指教这问题什么意思 该怎么改 跪谢了、。
代码如下 半成品。。
import java.awt.*;
import java.awt.event.*;
public class Calculator extends WindowAdapter implements ActionListener{
String str;
Button buttonname1[],buttonname2[];
Frame f;
Panel p1=new Panel(),p2=new Panel(),p3=new Panel(),p4=new Panel(),p5=new Panel();MenuBar mb;
Checkbox c1=new Checkbox("度");
Checkbox c2=new Checkbox("弧度");
Checkbox c3=new Checkbox("梯度");
Menu m1=new Menu("查看");
Menu m2=new Menu("编辑");
Menu m3=new Menu("帮助");
TextField t1=new TextField(100);
String Button1[]={"MC","MR","MS","M+","M-","退格","CE","C","正负","根号","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-"," ","0"," ",".","+","="};String Button2[]={" ","Inv","In","(",")","Int","sinh","sin","x²","阶乘","dms","cosh","cos","Xy","y√x","π","tanh","立方","立方根","F-E","Exp","Mod","log","10的x次方"};
public void addMenu(Frame f){
MenuBar mb=new MenuBar();
f.setMenuBar(mb);
mb.add(m1);mb.add(m2);mb.add(m3);
m1.add(new MenuItem("标准型",new MenuShortcut(KeyEvent.VK_T)));
m1.add(new MenuItem("科学性",new MenuShortcut(KeyEvent.VK_S)));
m1.add(new MenuItem("程序员",new MenuShortcut(KeyEvent.VK_P)));
m1.add(new MenuItem("统计信息",new MenuShortcut(KeyEvent.VK_A)));
m1.addSeparator();
m1.add(new MenuItem("历史记录",new MenuShortcut(KeyEvent.VK_V)));
m1.add(new MenuItem("数字分组",new MenuShortcut(KeyEvent.VK_I)));
m1.addSeparator();
m1.add(new MenuItem("基本",new MenuShortcut(KeyEvent.VK_B)));m1.add(new MenuItem("单位转换",new MenuShortcut(KeyEvent.VK_U)));
m1.add(new MenuItem("日期计算",new MenuShortcut(KeyEvent.VK_D)));
Menu m12=new Menu("工作表");
m1.add(new MenuItem("抵押",new MenuShortcut(KeyEvent.VK_M)));
m1.add(new MenuItem("汽车租赁",new MenuShortcut(KeyEvent.VK_V)));
m1.add(new MenuItem("油耗(mpg)",new MenuShortcut(KeyEvent.VK_F)));
m1.add(new MenuItem("油耗(l/100km)",new MenuShortcut(KeyEvent.VK_U)));
m2.add(new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C)));
m2.add(new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_P)));
m2.addSeparator();Menu m22=new Menu("历史记录");
m2.add(new MenuItem("复制历史记录",new MenuShortcut(KeyEvent.VK_I)));
m2.add(new MenuItem("编辑",new MenuShortcut(KeyEvent.VK_E)));
m2.add(new MenuItem("取消编辑",new MenuShortcut(KeyEvent.VK_N)));
m2.add(new MenuItem("清除",new MenuShortcut(KeyEvent.VK_L)));
m3.add(new MenuItem("查看帮助",new MenuShortcut(KeyEvent.VK_V)));
m3.addSeparator();
m3.add(new MenuItem("关于计算器",new MenuShortcut(KeyEvent.VK_B)));
}
Calculator(){
t1.setEditable(false);
f=new Frame("计算器");
addMenu(f);f.setSize(600,400);f.setLocation(200,200);
p2.add(c1);p2.add(c2);p2.add(c3);
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i=0;i<25;i++){
buttonname1[i] = new Button(Button2[i]);
p3.add(buttonname1[i]);
buttonname1[i].addActionListener(this);}
p4.add(p2);p4.add(p3);p4.setLayout(new BorderLayout());
p4.add(p3,"Center");p4.add(p4,"South");
for(int j=0;j<30;j++){
buttonname1[j]=new Button(Button1[j]);
p5.add(buttonname2[j]);
buttonname2[j].addActionListener(this);
}
p1.add(t1);p1.add(p4);p1.add(p5);
p1.setLayout(new BorderLayout());
p1.add(t1,"north");p1.add(p4,"West");p1.add(p5,"East");
f.addWindowListener(this);f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
//if(e.getSource()==)}
public static void main(String args[]){
new Calculator();}}
求指教这问题什么意思 该怎么改 跪谢了、。