//以下是实现计算器的代码
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cccc extends Applet {
/**
*
*/
private static final long serialVersionUID = 1L; //没啥作用
private JButton[] button=new JButton[19]; //定义一个按钮数组
private static final int HEIGHT=50; //确定每个按钮的高度
private static final int LENGTH=50; //确定每个按钮的长度
private static final int INPUT_NUMBER_ONE=0; //定义输入状态
private static final int INPUT_NUMBER_TWO=1;
private static final int NOT_INPUT=-1;
private int button_X=20; //定按钮的起始坐标
private int button_Y=10;
private int i;
private int Input_State=INPUT_NUMBER_ONE; //初始化输入状态
private int Operator_num; //操作符的下标
private JTextArea textarea;
private String[] s={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+","Clear","Reset","Close"}; //定义一个字符串数组
private StringBuffer s1=new StringBuffer();
private StringBuffer s2=new StringBuffer();
private StringBuffer s4=new StringBuffer();
private String s3=new String();
private String s5=new String();
private double result1; //获取 number_one
private double result2; //获取 number_two
private double result; //得到最终运算结果
private int []Operator={0,0,0,3,0,0,0,7,0,0,0,11,0,0,14,15}; //对应于按钮数组,不为0的为操作符
public void init()
{
resize(280,450); //设定页面大小
setLayout(null); //设定布局方式
for (i=0;i<16;i++)
{
if(i%4==0&&i!=0) //每行设定四个按钮,再次初始化坐标
{
button_X=20;
button_Y+=60;
}
button[i]=new JButton(s[i]); //实类化一个按钮
button[i].setBounds(button_X, button_Y, HEIGHT, LENGTH); //确定按钮的坐标与大小
button_X+=60;
add(button[i]);
button[i].addActionListener(new ButtonAction()); //监听按钮时间
}
button_X=20;button_Y=260;
textarea=new JTextArea("",5,100); //设定文本域
textarea.setBounds(button_X, button_Y, LENGTH+170, HEIGHT+40);
textarea.setBackground(Color.LIGHT_GRAY);
textarea.setWrapStyleWord(true);
textarea.setEditable(false);
add(textarea);
button_X=20;button_Y=380;
for(i=16;i<19;i++) //设定选择按钮(clear Reset close )
{
button[i]=new JButton(s[i]);
button[i].setBackground(Color.green);
button[i].setBounds(button_X, button_Y, LENGTH+30, HEIGHT-10);
button_X+=80;
add(button[i]);
button[i].addActionListener(new ButtonAction());
}
for(i=0;i<5;i++) //只是文本域中布局
{
s1=s1.append(" ");
}
Init_TextArea(); //初始化文本域
}
public void Init_TextArea()
{
textarea.setText(s1+"0\n\n\n\n"+s1+"0");
textarea.setFont(new Font("宋体",100,15)); //设置字体
}
//实现监听事件的类
class ButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource(); //获取事件源
for(i=0;i<19;i++) //匹配19个按钮
{
if(obj==button[i]&&i>15) //3个选择按钮的监听事件
{
if(i==17)
{
s2.delete(0, s2.length());
s4.delete(0, s4.length());
s3=null;
s5=null;
Input_State=INPUT_NUMBER_ONE;
Init_TextArea();
}
if(i==18)
{
System.exit(1);
}
if(i==16)
{
if(Input_State==INPUT_NUMBER_ONE)
{
if(s2.length()==0)
{
continue;
}
//字符串删去最后一个字符,用StringBuffer类型会在源字符串上进行操作,减少内存的浪费
s2.deleteCharAt(s2.length()-1);
s3=s2.toString();
textarea.setText(s1+" "+s3+"\n\n\n\n"+s1+"0"); //文字输入方式是由左向右
textarea.setAlignmentX(LEFT_ALIGNMENT);
}
if(Input_State==INPUT_NUMBER_TWO)
{
if(s4.length()==0)
{
continue;
}
s4.deleteCharAt(s4.length()-1);
s5=s4.toString();
textarea.setText(s1+s3+"\n\n"+s[Operator[Operator_num]]+s1+s5+"\n\n"+s1+"0");
}
}
}
else
{
if(obj==button[i]&&Operator[i]!=0)
{
if(i==14) //如果事件源的按钮是=号
{
result1=Double.parseDouble(s3);
result2=Double.parseDouble(s5);
switch(Operator_num) //选择是哪种操作符
{
case 3:result=result1/result2;break;
case 7:result=result1*result2;break;
case 11:result=result1-result2;break;
case 15:result=result1+result2;break;
}
textarea.setText(s1+s3+"\n\n"+s[Operator[Operator_num]]+s1+s5+"\n\n"+s1+""+result);
Input_State=NOT_INPUT;
//打印输出结果后,将输入状态设为 NOT_INPUT
}
else
{
textarea.setText(s1+s3+"\n\n"+s[Operator[i]]+"\n\n"+s1+"0");
Input_State=INPUT_NUMBER_TWO;
s4.delete(0, s4.length());
Operator_num=i; //记录操作符号的下标
}
}
if(obj==button[i]&&Operator[i]==0)
{
if(Input_State==INPUT_NUMBER_ONE)
{
s2.append(s[i]);
s3=s2.toString();
textarea.setText(s1+" "+s3+"\n\n\n\n"+s1+"0");
textarea.setAlignmentX(LEFT_ALIGNMENT);
}
if(Input_State==INPUT_NUMBER_TWO)
{
s4.append(s[i]);
s5=s4.toString();
textarea.setText(s1+s3+"\n\n"+s[Operator[Operator_num]]+s1+s5+"\n\n"+s1+"0");
}
}
}
}
}
}
}
后记:
最近在学西java,突然想实现这样一个程序,所以花费了些事件.上面就是劳动成果.上面只是实现了简单的 运算,但是起始道理都是一样的,如果大家感兴趣的话可以把它修改的更加完善.如果大家有什么好的建议, 请留言.还有如果你把代码拷过去之后有什么错误的话,估计是括号没对齐(就是括号跑到注释里面去了)
因为这里面的编辑器没有自动对齐功能,我也是一个一个修改的.有什么错误大家谅解!.....