posts - 4,  comments - 0,  trackbacks - 0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyCalculator extends JFrame implements ActionListener{

    MyPanel mp;
    ResPanel rp;
    
boolean pflag = false// 判断输入框里面是否有“."
    String re = ""// 初始显示框里面的内容
    double temp=0;  //暂存点击 + - * /前的内容
    int op=0;    //0为= 1为+ 2为- 3为* 4为/ 记住操作类型
    public static void main(String[] args) {
        
// TODO Auto-generated method stub

        MyCalculator myCalculator 
= new MyCalculator();

    }


    
public MyCalculator() {

        rp 
= new ResPanel();
        mp 
= new MyPanel();
        mp.one.addActionListener(
this);
        mp.two.addActionListener(
this);
        mp.three.addActionListener(
this);
        mp.four.addActionListener(
this);
        mp.five.addActionListener(
this);
        mp.six.addActionListener(
this);
        mp.seven.addActionListener(
this);
        mp.eight.addActionListener(
this);
        mp.nine.addActionListener(
this);
        mp.zero.addActionListener(
this);
        mp.tzero.addActionListener(
this);
        mp.point.addActionListener(
this);
        mp.plus.addActionListener(
this);
        mp.minus.addActionListener(
this);
        mp.mul.addActionListener(
this);
        mp.div.addActionListener(
this);
        mp.del.addActionListener(
this);
        mp.ce.addActionListener(
this);
        mp.equal.addActionListener(
this);
        
this.add(rp, BorderLayout.NORTH);
        
this.add(mp, BorderLayout.CENTER);
        
this.setTitle("Make By Stone");
        
this.setSize(280200);
        
this.setResizable(false);
        
this.setLocation(200200);
        
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
this.setVisible(true);
    }


    @Override
    
public void actionPerformed(ActionEvent e) {
        
// TODO Auto-generated method stub
        re = rp.text.getText(); // 取出显示框里面的内容
        if (e.getSource() == mp.one) {

            
if (re.equals("0")) // 判断
            {
                rp.text.setText(
"1");
            }
 else {
                rp.text.setText(re 
+ "1");
            }

        }

        
if (e.getSource() == mp.two) {

            
if (re.equals("0")) {
                rp.text.setText(
"2");
            }
 else {
                rp.text.setText(re 
+ "2");
            }

        }

        
if (e.getSource() == mp.three) {

            
if (re.equals("0")) {
                rp.text.setText(
"3");
            }
 else {
                rp.text.setText(re 
+ "3");
            }

        }

        
if (e.getSource() == mp.four) {

            
if (re.equals("0")) {
                rp.text.setText(
"4");
            }
 else {
                rp.text.setText(re 
+ "4");
            }

        }

        
if (e.getSource() == mp.five) {

            
if (re.equals("0")) {
                rp.text.setText(
"5");
            }
 else {
                rp.text.setText(re 
+ "5");
            }

        }

        
if (e.getSource() == mp.six) {

            
if (re.equals("0")) {
                rp.text.setText(
"6");
            }
 else {
                rp.text.setText(re 
+ "6");
            }

        }

        
if (e.getSource() == mp.seven) {

            
if (re.equals("0")) {
                rp.text.setText(
"7");
            }
 else {
                rp.text.setText(re 
+ "7");
            }

        }


        
if (e.getSource() == mp.eight) {

            
if (re.equals("0")) {
                rp.text.setText(
"8");
            }
 else {
                rp.text.setText(re 
+ "8");
            }

        }

        
if (e.getSource() == mp.nine) {

            
if (re.equals("0")) {
                rp.text.setText(
"9");
            }
 else {
                rp.text.setText(re 
+ "9");
            }

        }

        
if (e.getSource() == mp.zero) {

            
if (re.equals("0")) {
                rp.text.setText(
"0");
            }
 else {
                rp.text.setText(re 
+ "0");
            }

        }

        
if (e.getSource() == mp.tzero) {

            
if (re.equals("0.0"|| re.equals("0")) {
                rp.text.setText(
"0");
            }
 else {
                rp.text.setText(re 
+ "00");
            }

        }

        
if (e.getSource() == mp.point) {
             
            
if (pflag) {

            }
 else {
                
if (re.equals("0")) {
                    rp.text.setText(
"0.");
                }
 else {
                    rp.text.setText(re 
+ ".");
                }

            }

            pflag 
= true;
        }

        
if (e.getSource() == mp.del) {

            rp.text.setText(re.substring(
0, re.length()-1));
        }

        
if (e.getSource() == mp.ce) {
            temp
=0;
            rp.text.setText(
"0");
        }

        
if (e.getSource() == mp.plus) {

            
if(op!=0){
                
this.calculate();
                op
=1;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }
else{
                op
=1;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }

            
        }

        
if (e.getSource() == mp.minus) {

            
if(op!=0){
                
this.calculate();
                op
=2;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }
else{
                op
=2;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }

            
        }

        
if (e.getSource() == mp.mul) {

            
if(op!=0){
                
this.calculate();
                op
=3;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }
else{
                op
=3;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }

            
        }

        
if (e.getSource() == mp.div) {

            
if(op!=0){
                
this.calculate();
                op
=4;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }
else{
                op
=4;
                temp
=Double.valueOf(rp.text.getText());
                rp.text.setText(
"0");
            }

            
        }

        
if (e.getSource() == mp.equal) {
            
this.calculate();
        }


    }

    
public void calculate(){
        String s
=rp.text.getText();
        
double temp2=Double.valueOf(s);
        
switch(op){
        
case 0:
            rp.text.setText(String.valueOf(temp2));
            
break;
        
case 1:
            temp
=temp+temp2;
            rp.text.setText(String.valueOf(temp));
            op
=0;
            
break;
        
case 2:
            temp
=temp-temp2;
            rp.text.setText(String.valueOf(temp));
            op
=0;
            
break;
        
case 3:
            temp
=temp*temp2;
            rp.text.setText(String.valueOf(temp));
            op
=0;
            
break;
        
case 4:
            temp
=temp/temp2;
            rp.text.setText(String.valueOf(temp));
            op
=0;
            
break;
        }

    }

}


class ResPanel extends JPanel {
    JTextField text;

    
public ResPanel() {
        text 
= new JTextField(20);
        text.setHorizontalAlignment(SwingConstants.RIGHT);
        text.setText(
"0");
        text.enable(
false);
        
this.add(text);
    }

}


class MyPanel extends JPanel {
    JButton zero 
= new JButton("0");
    JButton one 
= new JButton("1");
    JButton two 
= new JButton("2");
    JButton three 
= new JButton("3");
    JButton four 
= new JButton("4");
    JButton five 
= new JButton("5");
    JButton six 
= new JButton("6");
    JButton seven 
= new JButton("7");
    JButton eight 
= new JButton("8");
    JButton nine 
= new JButton("9");
    JButton plus 
= new JButton("+");
    JButton minus 
= new JButton("-");
    JButton mul 
= new JButton("*");
    JButton div 
= new JButton("/");
    JButton point 
= new JButton(".");
    JButton tzero 
= new JButton("00");
    JButton equal 
= new JButton("=");
    JButton ce 
= new JButton("CE");
    JButton del 
= new JButton("");
    JButton mFor 
= new JButton("YH");

    
public MyPanel() {
        JPanel jp1 
= new JPanel();
        JPanel jp2 
= new JPanel();
        JPanel jp3 
= new JPanel();
        jp1.setLayout(
new GridLayout(4455));
        jp1.add(one);
        jp1.add(two);
        jp1.add(three);
        jp1.add(four);
        jp1.add(five);
        jp1.add(six);
        jp1.add(seven);
        jp1.add(eight);
        jp1.add(nine);
        jp1.add(zero);
        jp1.add(tzero);
        jp1.add(point);
        jp2.setLayout(
new GridLayout(4355));
        jp2.add(plus);
        jp2.add(minus);
        jp2.add(mul);
        jp2.add(div);
        jp2.add(del);
        jp2.add(ce);
        jp2.add(equal);
        jp2.add(mFor);
        jp3.add(jp1, BorderLayout.WEST);
        jp3.add(jp2, BorderLayout.EAST);
        
this.add(jp3, BorderLayout.CENTER);
    }

}
[img]/Files/shanrenxj/images/myCalculator.jpg[/img][  源文件:/Files/shanrenxj/源文件/MyCalculator.zip
posted on 2012-01-03 16:12 爱燕神鹰 阅读(70) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜