匿名用户
1级
2008-05-29 回答
package javatest;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class calculatorface extends JFrame{
/**
每次用时都要按下clear按钮,然后就是小数点按钮我没有处理,做的不是太好,里面除运算无法计算到小数
*/
private static final long serialVersionUID = 1581029666853134020L;
ArrayList al = new ArrayList();
JButton jb1 = new JButton("1");
JButton jb2 = new JButton("2");
JButton jb3 = new JButton("3");
JButton jb4 = new JButton("4");
JButton jb5 = new JButton("5");
JButton jb6 = new JButton("6");
JButton jb7 = new JButton("7");
JButton jb8 = new JButton("8");
JButton jb9 = new JButton("9");
JButton jb0 = new JButton("0");
JButton jbxing = new JButton("*");
JButton jbdian = new JButton(".");
JButton jbjian = new JButton("-");
JButton jbjia = new JButton("+");
JButton jbchu = new JButton("/");
JButton jbdeng = new JButton("=");
JButton jbC = new JButton("Clear");
public JLabel jl = new JLabel("0.",JLabel.RIGHT);
public calculatorface(){
super("计算器");
init();
}
public void init(){
Container c = getContentPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JPanel jp5 = new JPanel();
JPanel jp6 = new JPanel();
/*设定容器的布局方式*/
jp1.setLayout(new GridLayout(1,1));
jp2.setLayout(new GridLayout(1,4));
jp3.setLayout(new GridLayout(1,4));
jp4.setLayout(new GridLayout(1,4));
jp5.setLayout(new GridLayout(1,4));
jp6.setLayout(new GridLayout(1,4));
c.setLayout(new GridLayout(6,1));
/*将文本域放入容器jp1*/
jp1.add(jl);
/*将按钮放入容器jp2*/
jp2.add(jb7);
jp2.add(jb8);
jp2.add(jb9);
jp2.add(jbchu);
/*将按钮放入容器jp3*/
jp3.add(jb4);
jp3.add(jb5);
jp3.add(jb6);
jp3.add(jbxing);
/*将按钮放入容器jp4*/
jp4.add(jb1);
jp4.add(jb2);
jp4.add(jb3);
jp4.add(jbjian);
/*将按钮放入容器jp5*/
jp5.add(jb0);
jp5.add(jbdian);
jp5.add(jbjia);
jp5.add(jbdeng);
/*将按钮放入容器jp5*/
jp6.add(jbC);
/*将容器放入框架*/
c.add(jp1);
c.add(jp6);
c.add(jp2);
c.add(jp3);
c.add(jp4);
c.add(jp5);
/*设置大小并显示*/
this.setSize(300,250);
this.setVisible(true);
/*实例化一个监听器对象*/
actionDemo ad = new actionDemo();
/*为组件添加监听器*/
jbC.addActionListener(ad);
jb1.addActionListener(ad);
jb2.addActionListener(ad);
jb3.addActionListener(ad);
jb4.addActionListener(ad);
jb5.addActionListener(ad);
jb6.addActionListener(ad);
jb7.addActionListener(ad);
jb8.addActionListener(ad);
jb9.addActionListener(ad);
jb0.addActionListener(ad);
jbxing.addActionListener(ad);
jbdian.addActionListener(ad);
jbjian.addActionListener(ad);
jbjia.addActionListener(ad);
jbdeng.addActionListener(ad);
jbchu.addActionListener(ad);
}
/*事件监听器*/
class actionDemo implements ActionListener{
/*具体的功能在这里实现*/
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jbC)){
jl.setText("");
al.add("");
al.add("");
al.add("");
al.add("");
al.add("");
}
if(e.getSource().equals(jb1)){
jl.setText("");
String str=jl.getText();
str=str+"1";
jl.setText(str);
}
else if(e.getSource().equals(jb2)){
jl.setText("");
String str=jl.getText();
str=str+"2";
jl.setText(str);
}
else if(e.getSource().equals(jb3)){
String str=jl.getText();
str=str+"3";
jl.setText(str);
}
else if(e.getSource().equals(jb4)){
String str=jl.getText();
str=str+"4";
jl.setText(str);
}
else if(e.getSource().equals(jb5)){
String str=jl.getText();
str=str+"5";
jl.setText(str);
}
else if(e.getSource().equals(jb6)){
String str=jl.getText();
str=str+"6";
jl.setText(str);
}
else if(e.getSource().equals(jb7)){
String str=jl.getText();
str=str+"7";
jl.setText(str);
}
else if(e.getSource().equals(jb8)){
String str=jl.getText();
str=str+"8";
jl.setText(str);
}
else if(e.getSource().equals(jb9)){
String str=jl.getText();
str=str+"9";
jl.setText(str);
}
else if(e.getSource().equals(jb0)){
String str=jl.getText();
str=str+"0";
jl.setText(str);
}
else if(e.getSource().equals(jbdian)){
String str=jl.getText();
str=str+".";
jl.setText(str);
}
else if(e.getSource().equals(jbxing)){
al.set(0,jl.getText());
al.set(1,"xing");
jl.setText("");
}
else if(e.getSource().equals(jbchu)){
al.set(0,jl.getText());
al.set(1,"chu");
jl.setText("");
}
else if(e.getSource().equals(jbjian)){
al.set(0,jl.getText());
al.set(1,"jian");
jl.setText("");
}
else if(e.getSource().equals(jbjia)){
al.set(0,jl.getText());
al.set(1,"jia");
jl.setText("");
}
else if(e.getSource().equals(jbdeng)){
/*当点击等于号时,我首先从数组索引1处取得该元素判断他是什么运算符*/
String operator=(String) al.get(1);
System.out.println("该运算符是: "+operator);
/*当点击等于号时,从标签处获得数字是多少*/
int last = Integer.parseInt(jl.getText());
if(operator.equals("jian")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first-last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"JIAN");
jl.setText(new Long(sum).toString());
}
if(operator.equals("jia")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first+last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"JIA");
jl.setText(new Long(sum).toString());
}
if(operator.equals("chu")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first/last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"CHU");
jl.setText(new Long(sum).toString());
}
if(operator.equals("xing")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first*last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"XING");
jl.setText(new Long(sum).toString());
}
if(operator.equals("")){
/*当你在一直按等于号之前,获得标签上的数字*/
int in=Integer.parseInt(jl.getText());
System.out.println("当你在一直按等于号之前,获得标签上的数字 "+in);
/*表示当按完运算符后再按的数字号码是多少,这其实已经保存在数组里了,这个号码可以说是last变量存储的*/
String xlast=(String) al.get(2);
System.out.println("表示当按完运算符后再按的数字号码是多少 "+xlast);
int newxlast=Integer.parseInt(xlast);
long sum = 0;
if(al.get(3).equals("JIAN")){
sum=in-newxlast;
}
if(al.get(3).equals("JIA")){
sum=in+newxlast;
}
if(al.get(3).equals("CHU")){
sum=in/newxlast;
}
if(al.get(3).equals("XING")){
sum=in*newxlast;
}
System.out.println("sum表示最终的结果是 "+sum);
jl.setText(new Long(sum).toString());
}
}
}
}
public static void main(String [] args)
{
calculatorface c = new calculatorface();
}
}
这是我做的计算器,有一个缺点,就是除法不是很准确,剩下运算都是对的。
第一次计算前,要按下一下clear按钮,以后如果要重新计算的话还需要按一下clear按钮。结果才会正确。