1. 程式人生 > >用JAVA製作一個簡易的計算器

用JAVA製作一個簡易的計算器

題目:

  編寫一個模擬計算器的程式。在面板中新增一個文字框(顯示按鍵及運算結果)、

     10個數字按鈕(0~9)4個運算按鈕(加、減、乘、除)、一個等號按鈕、一個清除按鈕,

  要求將按鍵和結果顯示在文字框中。

程式碼過程展示:

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Exercise1 extends JFrame implements ActionListener{


private JPanel p1 = new JPanel(); //建立面板 
  private JPanel p2 = new JPanel(); //建立面板 
  private JTextField t1;   //文字框1用來顯示輸入資訊  
  StringBuffer str;//輸入的字串
  JButton[] b=new JButton[10];
  JButton  b1,b2,b3,b4,b5,b6;//16個按鈕
   double x,y;
  int n;
public Exercise1() {

super("假隊長的大目標"); 
   setSize(350,300);  //設定視窗大小
     setLocationRelativeTo(null); //顯示到中間        
      Container c = getContentPane(); //建立內容面板物件 
   
      t1 = new JTextField(25); 
      t1.setEditable(false); //只能顯示,不能編輯 
      
      p2.add(t1);   //新增文字框到面板 

      p2.setLayout(new GridLayout(3,2)); //把面扳佈局為4行1列 

      str=new StringBuffer(); 
      //例項化各個按鈕  
      for(int i=0;i<10;i++)  //分別為陣列中0~9號的按鈕設定標籤,並註冊監聽器 
      { 
        String s=""+i; 
        b[i]= new JButton(s);   
        b[i].addActionListener(this);   
      }         
       b1=new JButton("+");
       b2=new JButton("-");
       b3=new JButton("*");
       b4=new JButton("/");    
       b5=new JButton("=");
       b6=new JButton("delete");    
        
      //新增到面板
      p1.add(b[7]);
      p1.add(b[8]);
      p1.add(b[9]);
      p1.add(b1);
      p1.add(b[4]);
      p1.add(b[5]);
      p1.add(b[6]); 
      p1.add(b2);
      p1.add(b[1]);
      p1.add(b[2]);
      p1.add(b[3]);    
      p1.add(b3);
      p1.add(b[0]);
      p1.add(b5);
      p1.add(b6);
      p1.add(b4);     
      p1.setLayout(new GridLayout(4,5,10,10)); 
      
      //註冊監聽器 
         
      b1.addActionListener(this);
      b2.addActionListener(this);
      b3.addActionListener(this);
      b4.addActionListener(this);
      b5.addActionListener(this);
      b6.addActionListener(this);
   
      //將內容新增到面板上然後新增到容器裡
      c.add(p2); 
      c.add(p1); 
      c.setLayout(new FlowLayout()); //設定為順序佈局 
      //設定視窗關閉動作 
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設定視窗關閉動作 
      setVisible(true);  //設定為可見 
      setResizable(false); //禁止調整框架大小 
    
}

public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
Exercise1 calculate=new Exercise1();
}


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

if(e.getSource()==b6){
       t1.setText("0");//清零
       t1.setHorizontalAlignment(JTextField.RIGHT);//右對齊
       str.setLength(0);
}
           
              //Double.parseDouble   將字串轉化為double型別
             //t1.getText().trim()   獲取字元儲存後並且清除
            else if (e.getSource()==b1)//單擊加號按鈕獲得x的值並清空y的值 
    { 
      x=Double.parseDouble(t1.getText().trim()); 
      str.setLength(0); 
      y=0d; 
      n=0;
}else if(e.getSource()==b2)//減法運算
{
x=Double.parseDouble(t1.getText().trim()); 
      str.setLength(0); 
      y=0d;
      n=1;
}else if(e.getSource()==b3)//乘法運算
{
x=Double.parseDouble(t1.getText().trim()); 
      str.setLength(0); 
      y=0d;
      n=2;
}else if(e.getSource()==b4)//除法運算
{
x=Double.parseDouble(t1.getText().trim()); 
      str.setLength(0); 
      y=0d;
      n=3;
}else if(e.getSource()==b5)//等號
{
str.setLength(0);
switch(n){
case 0:t1.setText(""+(x+y));break;
case 1:t1.setText(""+(x-y));break;
case 2:t1.setText(""+(x*y));break;
case 3:t1.setText(""+(x/y));break;
}
}else{
if(e.getSource()==b[0])
{
if(t1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作  
        {} 
        else
t1.setText(str.append(e.getActionCommand()).toString());
t1.setHorizontalAlignment(JTextField.RIGHT); 
y=Double.parseDouble(t1.getText().trim());
}
else 
        { 
        t1.setText(str.append(e.getActionCommand()).toString());  
        t1.setHorizontalAlignment(JTextField.RIGHT); 
        y=Double.parseDouble(t1.getText().trim()); 
           }
}
}

}

總結:程式碼有點冗長,但是真正的看懂之後其實並不複雜。當然了,這還只是一個簡易的模擬計算器,

也可以在其中加入其他功能。比如說加入指數運算,冪運算,開方運算,或者為了使介面美觀,

再加入一個結果文字框,上面顯示輸入的數字,下面顯示出結果。當然了說這麼多,還是要靠讀者自己去鑽研。