<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Java計算器

    本程序寫于剛剛學習j2se的swing時,記得課堂上老師說起,他們那會兒畢業找工作,能夠寫出個計算器就算是會寫程序的人,就不愁找不到工作了,于是我就寫了這個程序,安慰一下失業不久的自己。回想起來,這個東西還是挺能夠鍛煉邏輯思維的,當初確實是費了不少心思,界面就可以看到,懶這個鍵是調用Windows系統的計算器,當初就是一步步照它而完成的,當然這個程序并沒有實現科學計算。由于程序是剛剛系統學習編程不久時寫的,很多地方存在不規范,而且一些地方繞了彎子,諸如界面按鈕的定義并不是采用數組而成,而是手工畫上去的,再有就是java監聽器的使用。
    以下為運行效果圖:


    以下為程序源代碼:
    package calculate;

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;

    /**
     * <p>Title: 簡單計算器</p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2007</p>
     *
     * <p>Company: 無業游民</p>
     *
     * 
    @author 非凡DZ
     * 
    @version 1.0
     
    */

    public class FrameCalculate extends JFrame {
        
    private String front = "", behind = ""//分別用于記錄加減乘除運算符之前,之后輸入的內容
        private String op; //用于記錄運算符
        private String re;//用于存儲運算結果的字符串格式
        private boolean flag = false//用于記錄是否按下了運算符
        private boolean flag1 = false;//用于判斷是否輸入了點運算符
        private double result;//用于存儲運算結果
        private boolean flag2 = false;//用于判斷是否輸入了數字
        private boolean flag3 = false;//用于判斷是否按下了等號運算符
        JPanel contentPane;
        JTextField txtResult 
    = new JTextField();
        JButton btnNull 
    = new JButton();
        JButton btnFour 
    = new JButton();
        JButton btnFive 
    = new JButton();
        JButton btnSix 
    = new JButton();
        JButton btnDecrease 
    = new JButton();
        JButton btnBegin 
    = new JButton();
        JButton btnOne 
    = new JButton();
        JButton btnTwo 
    = new JButton();
        JButton btnThree 
    = new JButton();
        JButton btnMultiply 
    = new JButton();
        JButton btnCancel 
    = new JButton();
        JButton btnZero 
    = new JButton();
        JButton btnMinus 
    = new JButton();
        JButton btnPoint 
    = new JButton();
        JButton btnDivide 
    = new JButton();
        JButton btnEqual 
    = new JButton();
        JButton btnIncrease 
    = new JButton();
        JButton btnSeven 
    = new JButton();
        JButton btnEight 
    = new JButton();
        JButton btnNine 
    = new JButton();
        
    public FrameCalculate() {
            
    try {
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                jbInit();
            }
     catch (Exception exception) {
                exception.printStackTrace();
            }

        }


        
    /**
         * Component initialization.
         *
         * 
    @throws java.lang.Exception
         
    */

        
    private void jbInit() throws Exception {
            contentPane 
    = (JPanel) getContentPane();
            contentPane.setLayout(
    null);
            
    this.setResizable(false);
            setSize(
    new Dimension(400300));
            setTitle(
    "計算器");
            txtResult.setEnabled(
    false);
            txtResult.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            txtResult.setEditable(
    false);
            txtResult.setText(
    "0");
            txtResult.setHorizontalAlignment(SwingConstants.RIGHT);
            txtResult.setBounds(
    new Rectangle(331931034));
            btnNull.setBounds(
    new Rectangle(298704637));
            btnNull.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 12));
            btnNull.setText(
    "");
            btnNull.addActionListener(
    new FrameCalculate_btnNull_actionAdapter(this));
            btnFour.setBounds(
    new Rectangle(331204637));
            btnFour.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnFour.setText(
    "4");
            btnFive.setBounds(
    new Rectangle(1011204637));
            btnFive.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnFive.setText(
    "5");
            btnSix.setBounds(
    new Rectangle(1671194637));
            btnSix.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnSix.setText(
    "6");
            btnDecrease.setBounds(
    new Rectangle(2341204637));
            btnDecrease.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnDecrease.setText(
    "-");
            btnBegin.setBounds(
    new Rectangle(2981214637));
            btnBegin.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 15));
            btnBegin.setText(
    "C");
            btnBegin.addActionListener(
    new FrameCalculate_btnBegin_actionAdapter(this));
            btnOne.setBounds(
    new Rectangle(331724637));
            btnOne.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnOne.setText(
    "1");
            btnTwo.setBounds(
    new Rectangle(1011724637));
            btnTwo.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnTwo.setText(
    "2");
            btnThree.setBounds(
    new Rectangle(1671724637));
            btnThree.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnThree.setText(
    "3");
            btnMultiply.setBounds(
    new Rectangle(2341724637));
            btnMultiply.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnMultiply.setText(
    "*");
            btnCancel.setBounds(
    new Rectangle(2981724637));
            btnCancel.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 12));
            btnCancel.setText(
    "");
            btnCancel.addActionListener(
    new FrameCalculate_btnCancel_actionAdapter(this));
            btnZero.setBounds(
    new Rectangle(332224637));
            btnZero.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnZero.setText(
    "0");
            
    //加載數字0-9的監聽事件
            btnZero.addActionListener(new FrameCalculate_btnZero_actionAdapter(this));
            btnOne.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnTwo.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnThree.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnFour.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnFive.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnSix.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnSeven.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnEight.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnNine.addActionListener(
    new FrameCalculate_btnZero_actionAdapter(this));
            btnMinus.setBounds(
    new Rectangle(1012224637));
            btnMinus.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 10));
            btnMinus.setText(
    "+/-");
            btnMinus.addActionListener(
    new FrameCalculate_btnMinus_actionAdapter(this));
            btnPoint.setBounds(
    new Rectangle(1672224637));
            btnPoint.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 30));
            btnPoint.setHorizontalTextPosition(SwingConstants.CENTER);
            btnPoint.setText(
    ".");
            btnPoint.addActionListener(
    new FrameCalculate_btnPoint_actionAdapter(this));
            btnDivide.setBounds(
    new Rectangle(2342224637));
            btnDivide.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnDivide.setText(
    "/");
            btnEqual.setBounds(
    new Rectangle(2982224637));
            btnEqual.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnEqual.setText(
    "=");
            btnEqual.addActionListener(
    new FrameCalculate_btnEqual_actionAdapter(this));
            btnIncrease.setBounds(
    new Rectangle(234704637));
            btnIncrease.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnIncrease.setText(
    "+");
            
    //加載加減乘除運算符的監聽事件
            btnIncrease.addActionListener(new
                                          FrameCalculate_btnIncrease_actionAdapter(
    this));
            btnDecrease.addActionListener(
    new
                                          FrameCalculate_btnIncrease_actionAdapter(
    this));
            btnMultiply.addActionListener(
    new
                                          FrameCalculate_btnIncrease_actionAdapter(
    this));
            btnDivide.addActionListener(
    new
                                        FrameCalculate_btnIncrease_actionAdapter(
    this));
            btnSeven.setBounds(
    new Rectangle(33704637));
            btnSeven.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnSeven.setText(
    "7");
            btnEight.setBounds(
    new Rectangle(101704637));
            btnEight.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnEight.setText(
    "8");
            btnNine.setBounds(
    new Rectangle(167704637));
            btnNine.setFont(
    new java.awt.Font("Dialog", Font.PLAIN, 20));
            btnNine.setText(
    "9");
            contentPane.add(btnFive);
            contentPane.add(btnSix);
            contentPane.add(btnDecrease);
            contentPane.add(btnBegin);
            contentPane.add(btnOne);
            contentPane.add(btnTwo);
            contentPane.add(btnThree);
            contentPane.add(btnMultiply);
            contentPane.add(btnCancel);
            contentPane.add(btnMinus);
            contentPane.add(btnPoint);
            contentPane.add(btnDivide);
            contentPane.add(btnEqual);
            contentPane.add(btnEight);
            contentPane.add(btnNine);
            contentPane.add(btnFour);
            contentPane.add(btnSeven);
            contentPane.add(btnIncrease);
            contentPane.add(btnNull);
            contentPane.add(txtResult);
            contentPane.add(btnZero);
        }


        
    public void btnNull_actionPerformed(ActionEvent e) {
            
    try {
                Runtime.getRuntime().exec(
    "calc.exe"); //調用系統的計算器
            }
     catch (IOException ex) {
            }

        }


        
    public void btnZero_actionPerformed(ActionEvent e) {
            
    if (flag) //如果剛剛按下了運算符
                txtResult.setText("");
                
    if (flag1) {//判斷之前是否輸入了點運算符
                    txtResult.setText("0." + e.getActionCommand());
                    flag1 
    = false;
                }
     else {
                    txtResult.setText(e.getActionCommand());
                }

                flag2 
    = true;
            }
     else {
                
    int num = txtResult.getText().indexOf(".");
                
    if (num < 0 && !txtResult.getText().equals("0")) {
                    txtResult.setText(txtResult.getText() 
    + e.getActionCommand());
                }
     else if (num < 0 && txtResult.getText().equals("0")) {
                    txtResult.setText(e.getActionCommand());
                }
     else if (num >= 0 && txtResult.getText().equals("0")) {
                    txtResult.setText(
    "0." + e.getActionCommand());
                }
     else if (num >= 0 && !txtResult.getText().equals("0")) {
                    txtResult.setText(txtResult.getText() 
    + e.getActionCommand());
                }

            }

            flag 
    = false;
            flag3
    =false;
        }


        
    public void btnIncrease_actionPerformed(ActionEvent e) {
            
    if(flag3){
                txtResult.setText(txtResult.getText());
                op 
    = e.getActionCommand(); //得到剛剛按下的運算符
                front = txtResult.getText(); //記錄加減乘除運算符之前輸入的內容
            }

            
    else if (flag2) {
                ActionEvent ee 
    = new ActionEvent("qq"1"pp");
                btnEqual_actionPerformed(ee);
                op 
    = e.getActionCommand(); //得到剛剛按下的運算符
                front = re;
                flag2 
    = false;
            }
     else {
                front 
    = txtResult.getText(); //記錄加減乘除運算符之前輸入的內容
                op = e.getActionCommand(); //得到剛剛按下的運算符
            }

            flag3
    =false;
            flag 
    = true//記錄已經按下了加減乘除運算符的其中一個
        }


        
    public void btnEqual_actionPerformed(ActionEvent e) {
            
    if(!flag3)//未曾按下等于運算符
                behind = txtResult.getText();
            
    else
                front 
    = re;
            
    try {
                
    double a1 = Double.parseDouble(front);
                
    double b1 = Double.parseDouble(behind);
                
    if (op == "+"{
                    result 
    = a1 + b1;
                }
     else if (op == "-"{
                    result 
    = a1 - b1;
                }
     else if (op == "*"{
                    result 
    = a1 * b1;
                }
     else {
                    result 
    = a1 / b1;
                }

                Double r 
    = new Double(result);
                re 
    = r.toString(result);
                txtResult.setText(re);
                }
     catch (ArithmeticException ce) {
                    txtResult.setText(
    "除數不能為零");
                }
     catch (Exception ee) {
                }

                
    if (!flag3)
                    flag3 
    = true;
        }


        
    public void btnPoint_actionPerformed(ActionEvent e) {
            
    int num=txtResult.getText().indexOf(".");
            
    if(num<0 && !flag)
                txtResult.setText(txtResult.getText()
    +e.getActionCommand());
            
    if(flag)
                flag1
    =true;
        }


        
    public void btnBegin_actionPerformed(ActionEvent e) {
            flag
    =false;
            flag1
    =false;
            flag2
    =false;
            flag3
    =false;
            front
    ="";
            behind
    ="";
            re
    ="";
            txtResult.setText(
    "0");
        }


        
    public void btnMinus_actionPerformed(ActionEvent e) {//取反運算符事件處理
            if(txtResult.getText().equals("0")){//如果文本框內容為0
                txtResult.setText(txtResult.getText());
            }
    else if(txtResult.getText().indexOf("-")>=0){//若文本框中含有負號
                String a=txtResult.getText().replaceAll("-","");
                txtResult.setText(a);
            }
    else if(flag){
                txtResult.setText(
    "0");
            }
    else{
                txtResult.setText(
    "-"+txtResult.getText());
            }

        }


        
    public void btnCancel_actionPerformed(ActionEvent e) {//退格事件處理方法
            String str=txtResult.getText();
            
    if(str.length() == 1){//如文本框中只剩下最后一個字符,將文本框內容置為0
                txtResult.setText("0");
            }

            
    if(str.length()>1){
                str
    =str.substring(0,str.length()-1);
                txtResult.setText(str);
            }

        }


        
    public static void main(String[] args){
            FrameCalculate fc 
    = new FrameCalculate();
            fc.setSize(
    400,310);
            fc.setLocation(
    200,150);
            fc.setVisible(
    true);
        }

    }



    class FrameCalculate_btnCancel_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnCancel_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnCancel_actionPerformed(e);
        }

    }



    class FrameCalculate_btnMinus_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnMinus_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnMinus_actionPerformed(e);
        }

    }



    class FrameCalculate_btnBegin_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnBegin_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnBegin_actionPerformed(e);
        }

    }



    class FrameCalculate_btnPoint_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnPoint_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnPoint_actionPerformed(e);
        }

    }



    class FrameCalculate_btnEqual_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnEqual_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnEqual_actionPerformed(e);
        }

    }



    class FrameCalculate_btnIncrease_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnIncrease_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnIncrease_actionPerformed(e);
        }

    }



    class FrameCalculate_btnZero_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnZero_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnZero_actionPerformed(e);
        }

    }



    class FrameCalculate_btnNull_actionAdapter implements ActionListener {
        
    private FrameCalculate adaptee;
        FrameCalculate_btnNull_actionAdapter(FrameCalculate adaptee) 
    {
            
    this.adaptee = adaptee;
        }


        
    public void actionPerformed(ActionEvent e) {
            adaptee.btnNull_actionPerformed(e);
        }

    }

    點擊下載源代碼:/Files/daizhenghenry/calculate.rar

    posted on 2008-03-06 14:28 非凡DZ 閱讀(609) 評論(0)  編輯  收藏 所屬分類: J2SE

    <2008年3月>
    2425262728291
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類(19)

    隨筆檔案(19)

    友情鏈接

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲欧洲中文日韩久久AV乱码| 手机看片久久国产免费| 四虎精品免费永久免费视频| 亚洲国产av美女网站| 亚洲中文字幕在线第六区| 四虎永久成人免费影院域名| 久久青草国产免费观看| 一级做a爰黑人又硬又粗免费看51社区国产精品视 | 老司机午夜性生免费福利| 亚洲婷婷综合色高清在线| 中文字幕专区在线亚洲| 国产精品jizz在线观看免费| 国产va免费精品观看精品 | 国产成人精品男人免费| 老司机在线免费视频| 182tv免费观看在线视频| 国产在线国偷精品免费看| 免费国产高清毛不卡片基地| 亚洲另类图片另类电影| 亚洲综合无码一区二区三区| 亚洲AV永久无码精品一百度影院| 中文字幕无码视频手机免费看| 美女被爆羞羞网站免费| 最新亚洲精品国偷自产在线| 亚洲欧洲自拍拍偷综合| 亚洲精品91在线| 91亚洲国产成人精品下载| 久久99国产亚洲精品观看| 亚洲精品乱码久久久久66| 亚洲精品中文字幕无码蜜桃| 久久亚洲精品无码观看不卡| 一本色道久久综合亚洲精品高清| 亚洲免费福利在线视频| 无码区日韩特区永久免费系列 | 亚洲免费人成视频观看| 114级毛片免费观看| 91精品免费不卡在线观看| 日韩精品久久久久久免费 | 国产亚洲av人片在线观看| 国产亚洲精品拍拍拍拍拍| 亚洲精品国产精品乱码不卡|