/**
作者:wwj
時間:2012/4/13
功能:實現一個計算器應用程序實驗要求:編寫一個模擬計算器的應用程序,使用面板和網格布局,
添加一個文本框,10個數字按鈕(0~9),4個加減乘除按鈕,
一個等號按鈕,一個清除按鈕,一個求平方根按鈕,一個退格按鈕,
要求將計算公式和結果顯示在文本框中,實現效果如下圖所示,
源程序保存為Ex5_2.java。**/import javax.swing.*;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.Color;public class Ex5_2 extends JFrame implements ActionListener
{private JPanel p1 = new JPanel(); //創建面板private JPanel p2 = new JPanel();private JTextField t1; //文本框1用來顯示輸入信息private JTextField t2; //文本框2用來顯示結果信息private JLabel label; //標簽信息StringBuffer str; //顯示屏所顯示的字符串 double x,y; //x和y都是運算數 int z; //Z表示單擊了那一個運算符.0表示"+",1表示"-",2表示"*",3表示"/" private JButton b[] = new JButton[12]; //創建一個有12個按鈕的數組private JButton b1,b2,b3,b4,b5,b6,b7,b8; //算術功能按鈕public Ex5_2(){super("簡易計算器"); //窗口名稱Container c = getContentPane(); //創建內容面板對象t1 = new JTextField(30);t1.setEditable(false); //只能顯示,不能編輯t2 = new JTextField(30);t2.setEditable(false); //只能顯示,不能編輯label = new JLabel("歡迎使用小巫版計算器^_^o~ 努力!");label.setForeground(Color.blue);//創建一個空字符串緩沖區 str=new StringBuffer(); p2.add(label); //添加標簽到面板p2.add(t2); //添加文本框到面板p2.add(t1); //添加文本框到面板p2.setLayout(new GridLayout(4,1)); //把面扳布局為4行1列for(int i=0;i<10;i++) //分別為數組中0~9號的按鈕設置標簽,并注冊監聽器{String s=""+i;b[i]= new JButton(s); b[i].addActionListener(this); }//實例化各個按鈕b[10]= new JButton("-/+"); b[11]= new JButton(".");b1= new JButton("/"); b2= new JButton("Back");b3= new JButton("*"); b4= new JButton("C");b5= new JButton("+"); b6= new JButton("Sqrt");b7= new JButton("-"); b8= new JButton("=");//設置按鈕前景色for(int i=0;i<12;i++){b[i].setForeground(Color.blue);}b1.setForeground(Color.red); b3.setForeground(Color.red);b5.setForeground(Color.red); b7.setForeground(Color.red);b2.setForeground(Color.blue); b4.setForeground(Color.blue);b6.setForeground(Color.red); b8.setForeground(Color.blue);//添加到面板p1.add(b[7]); p1.add(b[8]); p1.add(b[9]); p1.add(b1); p1.add(b2);p1.add(b[4]); p1.add(b[5]); p1.add(b[6]); p1.add(b3); p1.add(b4);p1.add(b[1]); p1.add(b[2]); p1.add(b[3]); p1.add(b5); p1.add(b6);p1.add(b[0]); p1.add(b[10]); p1.add(b[11]);p1.add(b7);p1.add(b8);p1.setLayout(new GridLayout(4,5,5,5));//注冊監聽器b[10].addActionListener(this); b[11].addActionListener(this);b1.addActionListener(this); b2.addActionListener(this);b3.addActionListener(this); b4.addActionListener(this);b5.addActionListener(this); b6.addActionListener(this);b7.addActionListener(this); b8.addActionListener(this);//將面板添加到內容面板c.add(p2);c.add(p1);c.setLayout(new FlowLayout()); //設置為順序布局setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置窗口關閉動作setVisible(true); //設置為可見setResizable(false); //禁止調整框架大小setSize(400,300); //設置窗口大小}//主方法實現創建一個窗口public static void main(String[] args){ Ex5_2 f = new Ex5_2(); }//按鈕的事件處理public void actionPerformed(ActionEvent e){try{if(e.getSource()==b4) //選擇"C"清零{t1.setText("0"); //把文本框清零t1.setHorizontalAlignment(JTextField.RIGHT); //文本對齊右邊str.setLength(0); //清空字符串緩沖區以準備接收新的輸入運算數}else if(e.getSource()==b[10])//單擊"+/-"選擇輸入的運算數是正數還是負數 {x=Double.parseDouble(t1.getText().trim());//trim函數作用是去掉字符串中的空格t1.setText(""+(-x));t1.setHorizontalAlignment(JTextField.RIGHT);}else if (e.getSource()==b5)//單擊加號按鈕獲得x的值和z的值并清空y的值{x=Double.parseDouble(t1.getText().trim());str.setLength(0);y=0d;z=0;}else if(e.getSource()==b7)//單擊減號按鈕獲得x的值和z的值并清空y的值 {x=Double.parseDouble(t1.getText().trim());str.setLength(0);y=0d;z=1;}else if(e.getSource()==b3)//單擊乘號按鈕獲得x的值和z的值并清空y的值 {x=Double.parseDouble(t1.getText().trim());str.setLength(0);y=0d;z=2;}else if(e.getSource()==b1)//單擊除號按鈕獲得x的值和z的值并清空y的值 {x=Double.parseDouble(t1.getText().trim());str.setLength(0);y=0d;z=3;}else if(e.getSource()==b8)//單擊等號按鈕輸出計算結果 {str.setLength(0);switch(z){case 0: t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;case 1: t1.setText(""+(x-y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;case 2: t1.setText(""+(x*y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;case 3: t1.setText(""+(x/y)); t1.setHorizontalAlignment(JTextField.RIGHT);break;}}else if(e.getSource()==b[11])//單擊"."按鈕輸入小數 {if(t1.getText().trim().indexOf('.')!=-1)//判斷字符串中是否已經包含了小數點{}else //如果沒有小數點{if(t1.getText().trim().equals("0"))//如果初時顯示為0 {t1.setText(str.append(e.getActionCommand()).toString());t1.setHorizontalAlignment(JTextField.RIGHT);}else if(t1.getText().trim().equals(""))//如果初時顯示為空則不做任何操作{}else { t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT);}}y=0d;}else if(e.getSource()==b6) //求平方根 {x=Double.parseDouble(t1.getText().trim()); if(x<0){t1.setText("數字格式異常");t1.setHorizontalAlignment(JTextField.RIGHT);}else{t1.setText(""+Math.sqrt(x));t1.setHorizontalAlignment(JTextField.RIGHT);}str.setLength(0);y=0d;}else{if(e.getSource()==b[0])//如果選擇的是"0"這個數字鍵 {if(t1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作 {}elset1.setText(str.append(e.getActionCommand()).toString());t1.setHorizontalAlignment(JTextField.RIGHT);y=Double.parseDouble(t1.getText().trim()); }else if (e.getSource()==b2) //選擇的是back鍵{if(!t1.getText().trim().equals("0"))//如果顯示屏顯示的不是零 {if(str.length()!=1) { t1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字符串越界異常 t1.setHorizontalAlignment(JTextField.RIGHT);}else { t1.setText("0"); t1.setHorizontalAlignment(JTextField.RIGHT);str.setLength(0); } } y=Double.parseDouble(t1.getText().trim()); }else {t1.setText(str.append(e.getActionCommand()).toString()); t1.setHorizontalAlignment(JTextField.RIGHT);y=Double.parseDouble(t1.getText().trim());}}}catch(NumberFormatException e1){ t1.setText("數字格式異常");t1.setHorizontalAlignment(JTextField.RIGHT); } catch(StringIndexOutOfBoundsException e1){t1.setText("字符串索引越界");t1.setHorizontalAlignment(JTextField.RIGHT);} }
}
轉載于:https://www.cnblogs.com/wwj9413/archive/2012/04/14/2638615.html