java實現多功能科學計算器(包括進制轉換,三角函數,四則運算等)

qwe.jpg
qwe.jpg

多功能科學計算器(包括進制轉換,三角函數,四則運算等)

Basic Framework

屏幕快照 2017-12-06 08.45.28.png
屏幕快照 2017-12-06 08.45.28.png

calculator.java

package myCalculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class calculator extends Frame implements ActionListener, WindowListener
{
    private Container container;
    private GridBagLayout layout;
    private GridBagConstraints constraints; 
    private JTextField displayField;         //計算結果顯示區
    private String lastCommand;           //保存+,-,*,/,=命令0
    private double result;               //保存計算結果
    private boolean start;           //判斷是否為數字的開始
    private JMenuBar menubar;
    private JMenuItem m_exit;
    private JMenuItem m2_ejz;
    private JMenuItem m2_bjz;
    private Dialog dialog;
    private Label label_dialog;
    private JButton button_sqrt;
    private JButton button_plusminus;
    private JButton button_CE;
    private JButton button_cancel;
    private JButton button_1;
    private JButton button_2;
    private JButton button_3;
    private JButton button_4;
    private JButton button_5;
    private JButton button_6;
    private JButton button_7;
    private JButton button_8;
    private JButton button_9;
    private JButton button_0;
    private JButton button_plus;
    private JButton button_minus;
    private JButton button_multiply;
    private JButton button_divide;
    private JButton button_point;
    private JButton button_equal;
    private JButton button_log;
    private JButton button_tan;
    private JButton button_cos;
    private JButton button_sin;
    private JButton button_exp;
    
    public calculator()       //構造方法設置布局、為按鈕注冊事件監聽器
    {
        super( "My Calculator" );
        this.setLocation( 350,150 );
        this.setSize( 450,400 );
        this.setResizable( true );
        this.setLayout( new GridLayout( 7,1 ) );
        this.addmyMenu();                   //調用成員方法添加菜單
        displayField = new JTextField( 30 );
        this.add( displayField );
        displayField.setEditable( true );

        start = true;
        result = 0;
        lastCommand = "=";

        JPanel panel0 = new JPanel();
        panel0.setLayout( new GridLayout( 1,4,4,4 ) );
        
        
        JPanel panel1 = new JPanel();
        panel1.setLayout( new GridLayout( 1,5,4,4 ) );
        this.add( panel1 );
        button_sqrt = new JButton( "sqrt" );//根號
        button_plusminus = new JButton( "+/-" );
        button_exp = new JButton( "exp" );//底數e的n次冪
        button_CE = new JButton( "退位");
        button_cancel = new JButton( "c" );//清除

        JPanel panel2 = new  JPanel();
        panel2.setLayout( new GridLayout( 1,5,4,4 ) );
        this.add( panel2 );
        button_7 = new JButton( "7" );
        button_8 = new JButton( "8" );
        button_9 = new JButton( "9" );
        button_log = new JButton( "log" );//對數
        button_divide = new JButton( "/" );//除

        JPanel panel3 = new JPanel();
        panel3.setLayout( new GridLayout(1,5,4,4) );
        this.add( panel3 );
        button_4 = new JButton( "4" );
        button_5 = new JButton( "5" );
        button_6 = new JButton( "6" );
        button_tan = new JButton( "tan" );//正切
        button_multiply = new JButton( "*" );//乘法

        JPanel panel4=new  JPanel();
        panel4.setLayout( new GridLayout( 1,5,4,4 ) );
        this.add(panel4);
        button_1 = new JButton( "1" );
        button_2 = new JButton( "2" );
        button_3 = new JButton( "3" );
        button_cos = new JButton( "cos");//余弦
        button_minus = new JButton( "-" );

        JPanel panel5 = new  JPanel();
        panel5.setLayout( new GridLayout( 1,5,4,4 ) );
        this.add( panel5 ); 
        button_0 = new JButton( "0" );
        button_point=new JButton( "." );
        button_equal = new JButton( "=" );
        button_sin = new JButton( "sin" );//正弦
        button_plus = new JButton( "+" );

        panel1.add( button_sqrt );
        panel1.add( button_plusminus );
        panel1.add( button_exp );
        panel1.add( button_CE );
        panel1.add( button_cancel );
        panel2.add( button_7 );
        panel2.add( button_8 );
        panel2.add( button_9 );
        panel2.add( button_log );
        panel2.add( button_divide );
        panel3.add( button_4 );
        panel3.add( button_5 );
        panel3.add( button_6 );
        panel3.add( button_tan );
        panel3.add( button_multiply );
        panel4.add( button_1 );
        panel4.add( button_2 ); 
        panel4.add( button_3 );
        panel4.add( button_cos );
        panel4.add( button_minus );
        panel5.add( button_0 );
        panel5.add( button_point );
        panel5.add( button_equal );
        panel5.add( button_sin );
        panel5.add( button_plus) ;

        button_sqrt.addActionListener( this );
        button_plusminus.addActionListener( this );
        button_exp.addActionListener( this );
        button_CE.addActionListener( this );
        button_cancel.addActionListener( this );
        button_7.addActionListener( this );
        button_8.addActionListener( this );
        button_9.addActionListener( this );
        button_log.addActionListener( this );
        button_divide.addActionListener( this );
        button_4.addActionListener( this );
        button_5.addActionListener( this );
        button_6.addActionListener( this );
        button_tan.addActionListener( this );
        button_multiply.addActionListener( this );
        button_1.addActionListener( this );
        button_2.addActionListener( this );
        button_3.addActionListener( this );
        button_cos.addActionListener( this );
        button_minus.addActionListener( this );
        button_0.addActionListener( this );
        button_point.addActionListener( this );
        button_equal.addActionListener( this );
        button_sin.addActionListener( this );
        button_plus.addActionListener( this );
                
        this.addWindowListener( new WinClose() );      //注冊窗口監聽器
        this.setVisible( true );
    }
    
    private void addmyMenu()        //菜單的添加
    {   
        JMenuBar menubar = new JMenuBar(); 
        this.add( menubar );
        JMenu m1 = new JMenu( "選項" );
        JMenu m2 = new JMenu( "進制轉換" );

        JMenuItem m1_exit = new JMenuItem( "退出" );
        m1_exit.addActionListener( this );
        JMenuItem m2_ejz = new JMenuItem( "二進制" );
        m2_ejz.addActionListener( this );
        JMenuItem m2_bjz = new JMenuItem("八進制");
        m2_bjz.addActionListener( this );
        JMenuItem m2_sljz = new JMenuItem("十六進制");
        m2_sljz.addActionListener( this );

        JMenu m3 = new JMenu( "幫助" ); 
        JMenuItem m3_Help = new JMenuItem( "用法" ); 
        m3_Help.addActionListener( this ); 

        dialog = new Dialog( this, "提示" , true );     //模式窗口
        dialog.setSize( 240,80 );
        label_dialog = new Label("", Label.CENTER );   //標簽的字符串為空,居中對齊
        dialog.add( label_dialog ); 
        dialog.addWindowListener( this );          //為對話框注冊窗口事件監聽器
        
        m1.add( m1_exit );  
        menubar.add( m1 );
        m2.add( m2_ejz );
        m2.add( m2_bjz );
        m2.add( m2_sljz );
        menubar.add( m2 );
        m3.add( m3_Help ); 
        menubar.add( m3 );  
    }

    public void actionPerformed(ActionEvent e)       //按鈕的單擊事件處理方法
    {
        if(
                e.getSource().equals( button_1 )||e.getSource().equals( button_2 )|| 
                e.getSource().equals( button_3 )||e.getSource().equals( button_4 )||
                e.getSource().equals( button_5 )|| e.getSource().equals( button_6 )||
                e.getSource().equals( button_7 )|| e.getSource().equals( button_8 )||
                e.getSource().equals( button_9 ) ||e.getSource().equals( button_0 )||
                e.getSource().equals( button_point )||e.getSource().equals( button_plusminus )||    
                e.getSource().equals( button_cancel )||e.getSource().equals( button_CE )
          )
        {      //非運算符的處理方法
            String input = e.getActionCommand();
            
            if ( start )
            {
                displayField.setText("");
                start = false;
                if( input.equals( "+/-" ) )
                    displayField.setText( displayField.getText()+ "-" ); 
            }
            if( !input.equals( "+/-" ) )
            {
                String str = displayField.getText();
                if( input.equals( "退格" ) )          //退格鍵的實現方法
                {       
                    if( str.length() > 0 )
                        displayField.setText( str.substring( 0,str.length() - 1 ) );
                }
                else if( input.equals( "C" ) )         //清零鍵的實現方法
                {
                    displayField.setText( "0" );
                    start = true;
                }   
                else
                    displayField.setText( displayField.getText() + input );
            }
        }
        else if ( e.getActionCommand() == "二進制" )   //二進制的轉換
        {
            int n = Integer.parseInt( displayField.getText() );
            displayField.setText( Integer.toBinaryString( n ) );
        }
        else if ( e.getActionCommand() == "八進制" )    //八進制的轉換
        {
            int n = Integer.parseInt( displayField.getText() );
            displayField.setText( Integer.toOctalString( n ) );
        }
        else if ( e.getActionCommand() == "十六進制" )    //十六進制的轉換
        {
            int n = Integer.parseInt( displayField.getText() );
            displayField.setText( Integer.toHexString( n ) );
        }

        else if ( e.getActionCommand() == "退出" )      //選項中退出的處理方法
        {
            System.exit( 0 );
        }
        else if ( e.getActionCommand() == "用法" )      //按下'幫助'菜單欄中用法的處理方法
        {   
            label_dialog.setText( "sqrt,exp等鍵是先輸運算符再輸數字\n" ); 
            dialog.setLocation( 400,250 );
            dialog.setVisible( true );  
        }
        else        //各運算符的識別
        {
            String command = e.getActionCommand();        
            if( start )
            {
                lastCommand = command;
            }
            else
            {
                calculate( Double.parseDouble( displayField.getText() ) );
                lastCommand = command;
                start = true;
            }
         }
    }
    
    public void calculate( double x )          //各運算符的具體運算方法
    {
        double d = 0;
        if ( lastCommand.equals( "+" ) ) 
            result += x;    
        else if (lastCommand.equals( "-" ) ) 
            result -= x;
        else if ( lastCommand.equals( "*" ) ) 
            result *= x;   
        else if ( lastCommand.equals( "/" ) ) 
            result /= x;
        else if ( lastCommand.equals( "=" ) ) 
            result = x;
        else if ( lastCommand.equals( "sqrt" ) ) 
        {
            d = Math.sqrt( x );
            result = d;
        }
        else if ( lastCommand.equals( "exp" ) )
        {
            d = Math.exp( x );
            result = d;
        }
        else if ( lastCommand.equals( "log" ) )
        {
            d = Math.log( x );
            result = d;
        }
        else if ( lastCommand.equals( "tan" ) )
        {
            d = Math.tan(x);
            result = d;
        }
        else if ( lastCommand.equals( "cos" ) )
        {
            d = Math.cos( x );
            result = d;
        }
        else if ( lastCommand.equals( "sin" ) )
        {
            d = Math.sin( x );
            result = d;
        }
        displayField.setText( ""+ result );
     }   
    
    public void windowClosing( WindowEvent e )
    {
        if( e.getSource() == dialog )
            dialog.setVisible( false );           //隱藏對話框
        else
            System.exit( 0 ); 
    }

    public void windowOpened( WindowEvent e )         {  }
    public void windowActivated( WindowEvent e )      {  }
    public void windowDeactivated( WindowEvent e )    {  }
    public void windowClosed( WindowEvent e )         {  }
    public void windowIconified( WindowEvent e )      {  }
    public void windowDeiconified( WindowEvent e )    {  }
        
    public static void main( String args[] )          
    {
        calculator calculator = new calculator();
    }
}

class WinClose implements WindowListener
{
    public void windowClosing( WindowEvent e )    //單擊窗口關閉按鈕時觸發并執行實現窗口監聽器接口中的方法
    {
        System.exit( 0 );          //結束程序運行
    }
    public void windowOpened( WindowEvent e ){ }
    public void windowActivated( WindowEvent e ){}
    public void windowDeactivated( WindowEvent e){ }
    public void windowClosed( WindowEvent e ){ }
    public void windowIconified( WindowEvent e ){ }
    public void windowDeiconified( WindowEvent e ){ }
}

Running Effect

屏幕快照 2017-12-06 08.48.57.png
屏幕快照 2017-12-06 08.48.57.png

屏幕快照 2017-12-06 08.49.15.png
屏幕快照 2017-12-06 08.49.15.png

Source Download

Please click the address->My Calculator

Summarize

  1. 運用兩個面板的疊加做出界面。
  2. 通過在按鈕的單擊事件處理方法中調用類的成員方法calculate()來進行簡易計算器的各種運算,并正確實現運算功能。
  3. 調用Math包中的方法實現各函數功能。
  4. 添加菜單條,列出‘選項’、‘進制轉換’、‘幫助’等菜單選項,并分別實現‘選項’中‘退出’的功能,通過調用Integer包中的方法實現二進制、八進制、十六進制的轉換,‘幫助’菜單欄中‘用法’的提示對話框。
  5. 整個程序對話框可實現最小化、最大化、關閉。

#個人主頁:www.iooy.com

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,835評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,676評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,730評論 0 380
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,118評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,873評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,266評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,330評論 3 443
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,482評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,036評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,846評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,025評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,575評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,279評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,684評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,953評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,751評論 3 394
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,016評論 2 375

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,789評論 18 139
  • 第2章 基本語法 2.1 概述 基本句法和變量 語句 JavaScript程序的執行單位為行(line),也就是一...
    悟名先生閱讀 4,185評論 0 13
  • 本文檔主要描述系統安裝完一般軟件環境配置,初始狀態為系統剛安裝完成 更新系統 安裝open-vm-tools或者V...
    厝弧閱讀 175評論 0 1
  • 最近哈利波特前傳在影院上映了,于是我又默默的把哈利波特系列電影重新回顧了一遍。作為一個忠實的哈迷,我想自己已經無法...
    a35256f04c99閱讀 4,023評論 4 0