編寫一個模擬計算器的應用程序,使用面板和網格佈局, 添加一個文本框,10個數字按鈕(0~9),4個加減乘除按鈕, 一個等號按鈕,一個清除按鈕,一個求平方根按鈕,一個退格按鈕, 要求將計算公式和結果顯示在文字方塊中,實現效果如下圖所示。
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 implementsments java.awt.Color; public class Ex5_2 extends JFrame implementsments 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(thisListener(this); b5.addActionListener(this); b6.addActionListener( ); 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(""+(xy)); 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"))//如果顯示器顯示的為零不做操作{} else t1.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);} } }運行效果圖:
一個Java簡易計算器程式設計就這麼完成了,希望這篇文章可以對大家寫計算器有所啟發,這只是一個簡易的計算器,大家還可以繼續發揮,完善計算器的功能。