程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> windows計算器java程序

windows計算器java程序

編輯:關於JAVA

 

package 計算器程序;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class myWindows extends JFrame implements ActionListener
{
       boolean tag=true;  //判斷輸入的數據是否為整數,初始為整數。
       boolean signal=true;//符號,即正數或負數,初始為正數。
    JTextField text1;
    public myWindows(int w,int h)
    {
        setTitle("計算器程序");
        this.setBounds(100,100,350,300);
        Container con = getContentPane();
        con.setLayout(new GridLayout(3,1));
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(2,1));
        text1 = new JTextField(150);
        text1.setHorizontalAlignment(text1.RIGHT);
        text1.setText("0.");
        JPanel p1_1  = new JPanel();
        p1_1.setLayout(new GridLayout(1,4));
        JButton btn_BackSpace = new JButton("BackSpace");
        btn_BackSpace.setForeground(Color.red);
        btn_BackSpace.addActionListener(this);
        JButton btn_CE = new JButton("CE");
        btn_CE.setForeground(Color.red);
        JButton btn_C = new JButton("C");
        btn_C.setForeground(Color.red);
        btn_C.addActionListener(this);
        p1.add(text1);
        p1_1.add(new JLabel());
        p1_1.add(btn_BackSpace);
        p1_1.add(btn_CE);
        p1_1.add(btn_C);
        p1.add(p1_1);       

        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(2,1));
        JPanel p2_1 = new JPanel();
        p2_1.setLayout(new GridLayout(1,6));
        JButton btn_MC = new JButton("MC");
        btn_MC.setForeground(Color.red);
        JButton btn_7 = new JButton("7");
        btn_7.setForeground(Color.BLUE);
        btn_7.addActionListener(this);
        JButton btn_8 = new JButton("8");
        btn_8.setForeground(Color.BLUE);
        btn_8.addActionListener(this);
        JButton btn_9 = new JButton("9");
        btn_9.setForeground(Color.BLUE);
        btn_9.addActionListener(this);
        JButton btn_wind= new JButton("/");
        btn_wind.setForeground(Color.red);
        JButton btn_sqrt = new JButton("sqrt");
        btn_sqrt.setForeground(Color.BLUE);
        p2_1.add(btn_MC); p2_1.add(btn_7);
        p2_1.add(btn_8); p2_1.add(btn_9);
        p2_1.add(btn_wind);p2_1.add(btn_sqrt);
        JPanel p2_2 = new JPanel();
        p2_2.setLayout(new GridLayout(1,6));
        JButton btn_MR = new JButton("MR");
        btn_MR.setForeground(Color.red);
        JButton btn_4 = new JButton("4");
        btn_4.setForeground(Color.BLUE);
        btn_4.addActionListener(this);
        JButton btn_5 = new JButton("5");
        btn_5.setForeground(Color.BLUE);
        btn_5.addActionListener(this);
        JButton btn_6 = new JButton("6");
        btn_6.setForeground(Color.BLUE);
        btn_6.addActionListener(this);
        JButton btn_star= new JButton("*");
        btn_star.setForeground(Color.red);
        JButton btn_mod = new JButton("%");
        btn_mod.setForeground(Color.BLUE);
        p2_2.add(btn_MR);
        p2_2.add(btn_4); p2_2.add(btn_5);
        p2_2.add(btn_6);
        p2_2.add(btn_star);p2_2.add(btn_mod);
        p2.add(p2_1);p2.add(p2_2);
       
        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(2,1));
        JPanel p3_1 = new JPanel();
        p3_1.setLayout(new GridLayout(1,6));
        JButton btn_MS = new JButton("MS");
        btn_MS.setForeground(Color.red);
        JButton btn_1 = new JButton("1");
        btn_1.setForeground(Color.BLUE);
        btn_1.addActionListener(this);
        JButton btn_2 = new JButton("2");
        btn_2.setForeground(Color.BLUE);
        btn_2.addActionListener(this);
        JButton btn_3 = new JButton("3");
        btn_3.setForeground(Color.BLUE);
        btn_3.addActionListener(this);
        JButton btn_sub= new JButton("-");
        btn_sub.setForeground(Color.red);
        JButton btn_reverse = new JButton("1/x");
        btn_reverse.setForeground(Color.BLUE);
        p3_1.add(btn_MS); p3_1.add(btn_1);
        p3_1.add(btn_2); p3_1.add(btn_3);
        p3_1.add(btn_sub);p3_1.add(btn_reverse);
        JPanel p3_2 = new JPanel();
        p3_2.setLayout(new GridLayout(1,6));
        JButton btn_Madd = new JButton("M+");
        btn_Madd.setForeground(Color.red);
        JButton btn_0= new JButton("0");
        btn_0.setForeground(Color.BLUE);
        btn_0.addActionListener(this);
        JButton btn_singal = new JButton("+/-");
        btn_singal.setForeground(Color.BLUE);
        btn_singal.addActionListener(this);
        JButton btn_dot = new JButton(".");
        btn_dot.addActionListener(this);
        JButton btn_add= new JButton("+");
        btn_add.setForeground(Color.red);
        JButton btn_equal = new JButton("=");
        btn_equal.setForeground(Color.red);
        p3_2.add(btn_Madd);
        p3_2.add(btn_0); p3_2.add(btn_singal);
        p3_2.add(btn_dot);
        p3_2.add(btn_add);p3_2.add(btn_equal);
        p3.add(p3_1);p3.add(p3_2);
        con.add(p1);con.add(p2);con.add(p3);
        setVisible(true);
    }
    public void press_dot()
    {
        tag=false;
    }

    public void press_C()
    {
        text1.setText("0.");
        tag=true;
    }

    public void press_BackSpace()
    {
        String orginStr = text1.getText();
        if(orginStr.equals("0."))
            text1.setText("0.");
        else
        {//是小數,即小數點後面有數字的情形
            if(orginStr.indexOf(".")<orginStr.length()-1)
                text1.setText(orginStr.substring(0,orginStr.length()-1));
            else
            {              
                if(orginStr.length()==2)
                    text1.setText("0.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf(".")-1)+".");
                    tag=true;
                }
            }
        }
    }

     public void press_0()
    {
         String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("0.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"0.");
                }
            }
            else
            {
                text1.setText(orginStr+"0");
            }
    }

    public void press_1()
    {
         String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("1.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"1.");
                }
            }
            else
            {
                text1.setText(orginStr+"1");
            }
    }

     public void press_2()
    {
         String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("2.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"2.");
                }
            }
            else
            {
                text1.setText(orginStr+"2");
            }
    }

      public void press_3()
    {
         String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("3.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"3.");
                }
            }
            else
            {
                text1.setText(orginStr+"3");
            }
    }

       public void press_4()
     {
         String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("4.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"4.");
                }
            }
            else
            {
                text1.setText(orginStr+"4");
            }
     }

        public void press_5()
        {
            String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("5.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"5.");
                }
            }
            else
            {
                text1.setText(orginStr+"5");
            }
        }

         public void press_6()
        {
             String orginStr=text1.getText();
             if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("6.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"6.");
                }
            }
            else
            {
                text1.setText(orginStr+"6");
            }
        }

          public void press_7()
        {
            String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("7.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"7.");
                }
            }
            else
            {
                text1.setText(orginStr+"7");
            }
        }

          public void press_8()
         {
            String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("8.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"8.");
                }
            }
            else
            {
                text1.setText(orginStr+"8");
            }
        }

           public void press_9()
          {
            String orginStr=text1.getText();
            if(tag)
            {
                if( orginStr.equals("0."))
                    text1.setText("9.");
                else
                {
                    text1.setText(orginStr.substring(0,orginStr.indexOf("."))+"9.");
                }
            }
            else
            {
                text1.setText(orginStr+"9");
            }
        }

           public void press_singal()
           {
               String orginStr=text1.getText();
               if(signal)
               {                   
                    text1.setText("-"+orginStr);
                    signal=false;
               }
               else
               {
                   text1.setText(orginStr.substring(1,orginStr.length()));
                   signal=true;
               }
           }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("C"))
            this.press_C();
        if(e.getActionCommand().equals("BackSpace"))
            this.press_BackSpace();
        if(e.getActionCommand().equals("."))
            this.press_dot();
        if(e.getActionCommand().equals("0"))
            this.press_0();
        if(e.getActionCommand().equals("1"))       
            this.press_1();
         if(e.getActionCommand().equals("2"))
            this.press_2();
         if(e.getActionCommand().equals("3"))
            this.press_3();
         if(e.getActionCommand().equals("4"))
            this.press_4();
         if(e.getActionCommand().equals("5"))
            this.press_5();
         if(e.getActionCommand().equals("6"))
            this.press_6();
         if(e.getActionCommand().equals("7"))
            this.press_7();
         if(e.getActionCommand().equals("8"))
            this.press_8();
         if(e.getActionCommand().equals("9"))
            this.press_9();
        if(e.getActionCommand().equals("+/-"))
            this.press_singal();


    }
}

public class Calculator {

 
    public static void main(String[] args)
    {
        new myWindows(200,200);
    }

}
 

*
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved