import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;import static java.awt.BorderLayout.*;
public class jisuan{
static TextField textField= new TextField(30); static Button[] b = new Button[18];
public static void main(String[] args)
{ Frame f=new Frame("计算器");
Panel p1=new Panel();
p1.add(textField);
f.add(p1 , NORTH);
Panel p2=new Panel();
p2.setLayout(new GridLayout(3,5,4,4));
String[] name=
{"0","1","2","3","4","5","6","7","8","9","+","-","*","/",".","="
}; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
li f1=new li();
for(int i=0;i<name.length;i++)
{
b[i]=new Button(name[i]);
p2.add(b[i]);
}
for(int i=0;i<name.length;i++)
{
b[i].addActionListener(f1);
}
f.add(p2);
f.pack();
f.setVisible(true); class li implements ActionListener{
public void actionPerformed(ActionEvent e) {
String value="0";
StringBuffer temp = new StringBuffer("");
String optValue = "0";
String optType="";
boolean isChoiseOptType=true; if (e.getSource().equals(b[0])) {
temp.append(b[0].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[1])) {
temp.append(b[1].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[2])) {
temp.append(b[2].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[3])) {
temp.append(b[3].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[4])) {
temp.append(b[4].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[5])) {
temp.append(b[5].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[6])) {
temp.append(b[6].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[7])) {
temp.append(b[7].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[8])) {
temp.append(b[8].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[9])) {
temp.append(b[9].getLabel());
textField.setText(temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[14])) {
if(temp.length()<=0)
temp.append("0");
temp.append(b[14].getLabel());
textField.setText( temp.toString());
isChoiseOptType=false;
} else if (e.getSource().equals(b[10])) {
if(!isChoiseOptType){
value= textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
optValue=value;
temp=new StringBuffer("");
}
optType=b[10].getLabel();
isChoiseOptType=true;
} else if (e.getSource().equals(b[11])) {
if(!isChoiseOptType){
value= textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
optValue=value;
temp=new StringBuffer("");
}
optType=b[11].getLabel();
isChoiseOptType=true;
} else if (e.getSource().equals(b[12])) {
if(!isChoiseOptType){
value= textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
optValue=value;
temp=new StringBuffer("");
}
optType=b[12].getLabel();
isChoiseOptType=true;
} else if (e.getSource().equals(b[13])) {
if(!isChoiseOptType){
value= textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
optValue=value;
temp=new StringBuffer("");
}
optType=b[13].getLabel();
isChoiseOptType=true;
}else if (e.getSource().equals(b[15])) {
if(! optType.equals("")){
BigDecimal opt1=new BigDecimal( optValue);
value= textField.getText();
if(value.lastIndexOf(".")==value.length()-1){
value=value.substring(0,value.length()-1);
}
BigDecimal opt2=new BigDecimal(value);
BigDecimal result=new BigDecimal(0);
if( optType.equals("+")){
result=opt1.add(opt2);
}else if( optType.equals("-")){
result=opt1.subtract(opt2);
}else if( optType.equals("*")){
result=opt1.multiply(opt2);
}else if( optType.equals("/")){
result=opt1.divide(opt2);
}else if( optType.equals("%")){
result=opt1.remainder(opt2);
}
textField.setText(result.toString());
temp=new StringBuffer("");
isChoiseOptType=false;
optValue="0";
}
}
}//actioner
}//class li}//public void main
}li f1=new li();这行代码出错了。。提示:
没有任何类型 jisuan 的外层实例可访问。必须用类型 jisuan 的外层实例(例如,x.new A(),其中 x 是 jisuan 的实例)来限定分配。
什么无法解释类型。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货