本文為大家分享了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(""+(xy));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()); } } } }總結:代碼有點冗長,但是真正的看懂之後其實並不復雜。當然了,這還只是一個簡易的模擬計算器,
也可以在其中加入其他功能。比如說加入指數運算,冪運算,開方運算,或者為了使界面美觀,
再加入一個結果文本框,上面顯示輸入的數字,下面顯示出結果。當然了說這麼多,還是要靠讀者自己去鑽研。
以上全部為本篇文章的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。