1. 程式人生 > 其它 >用Java實現簡單的計算器

用Java實現簡單的計算器

技術標籤:Java基礎知識練習小示例java計算器swing

用Java實現簡單的計算器

​ 本計算器實現了最簡單的四則運算,介面簡潔,小夥伴可以根據自己的需要再此基礎上進行修改,可拓展性強

效果圖:

在這裡插入圖片描述

程式碼:

public class Counter extends JPanel {
    public Graphics g = null;

    public static void main(String[] args) {
        Counter counter = new Counter();
        counter.run();
    }

    public
void run() { CouterListener couterListener = new CouterListener(); // 窗體標題 JFrame frame = new JFrame("Calculate"); // 佈局:上下左右的間距為10 FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10); frame.setLayout(fl); // 介面的的尺寸 frame.setSize
(270, 400); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(3); // 固定窗體的大小,無法通過滑鼠改變大小 frame.setResizable(false); // 多行文字框 JTextArea field = new JTextArea(); // 文字框的尺寸 field.setPreferredSize(new Dimension(250, 50)); // 設定最外層Frame的行數為2,一行放文字框,一行放按鈕
field.setRows(3); frame.add(field); // 背景顏色(可根據自己要求進行修改) frame.setBackground(Color.GRAY); couterListener.setJText(field); // 按鈕佈局,可以根據需要進行修改 String BtArray[] = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", ".", "0", "=", "/" }; // 按鈕一共4行4列 this.setLayout(new GridLayout(4, 4, 10, 10)); frame.add(this); for (int i = 0; i < 16; i++) { JButton button = new JButton(BtArray[i]); // 按鈕的顏色 button.setForeground(Color.DARK_GRAY); button.setPreferredSize(new Dimension(50, 50)); button.setContentAreaFilled(false); this.add(button); // 給按鈕繫結點選事件 button.addActionListener(couterListener); } //新增CE按鈕 JButton ce = new JButton("CE"); ce.setForeground(Color.DARK_GRAY); ce.setPreferredSize(new Dimension(250, 50)); ce.setContentAreaFilled(false); ce.addActionListener(couterListener); frame.add(ce); // 使視窗顯示 frame.setVisible(true); g = frame.getGraphics(); } } /** * 這是監聽類 * * 過程解析 * 以 5 + 3 = 8為例: * 當輸入5的時候 temp = 5 (temp用於接收最新輸入的資料) * 當輸入+的時候 result = temp,也就是說result = 5 (result用於輸出結果) * count = “+” (count會儲存最近的運算子) * 當輸入8的時候 temp = 8 (temp用於接收最新輸入的資料) * 當輸入=的時候 輸出結果是 [result count temp]的結果,也就是 5 + 8的運算結果 */ class CouterListener implements ActionListener { // 文字域物件 private JTextArea JF; public int x = 0; //記錄最新輸入的是否為數字 public boolean isNumber = true; // 結果 public Double resulte = 0.0, temp = 0.0; public String num[] = new String[3]; // 用於儲存等號之前一次的運算子,比如 + - * / public String count; // 設定文字域 public void setJText(JTextArea F) { JF = F; } @Override public void actionPerformed(ActionEvent e) { // 運算子事件:+ - * / 運算子 ArrayList calculateCommands = new ArrayList<String>(); calculateCommands.add("+"); calculateCommands.add("-"); calculateCommands.add("*"); calculateCommands.add("/"); // 獲得當前的命令 String command = e.getActionCommand(); if ("CE".equals(command)) { // 歸零 resulte = temp = 0.0; count = ""; JF.setText(""); isNumber = true; }else if (calculateCommands.contains(command)) { // + - * / JF.setText(JF.getText() + command + "\n"); if (isNumber) { //輸入運算子時,result儲存運算子之前的數 resulte = temp; } // 儲存本次的運算子 count = e.getActionCommand(); isNumber = false; } else if ("=".equals(command)) { // 計算結果 if ("+".equals(count)) { resulte = resulte + temp; JF.setText(resulte + ""); } else if ("-".equals(count)) { resulte = resulte - temp; JF.setText(resulte + ""); } else if ("*".equals(count)) { resulte = resulte * temp; JF.setText(resulte + ""); } else if ("/".equals(count)) { resulte = resulte / temp; JF.setText(resulte + ""); } } else { //輸入的不是 ce + - * / = 的時候,即輸入的是數字的時候 JF.setText(JF.getText() + command); if (!isNumber) { //如果上一次輸入的是字元 //正則表示式:根據換行符[\n]將文字框內字串變成字串陣列 num = JF.getText().split("\\n"); //將運算子後面的(即換行符後面的)數字儲存到temp中去 temp = Double.parseDouble(num[1]); // 將下面註釋開啟就可以知道num和temp的作用了 System.out.println("num: "); for (int i = 0; i < num.length; i++) { System.out.printf("[" + i + "]:\"" +num[i] + "\"\n"); } System.out.println("temp: " + temp); } else { //如果是第一次輸入數字 temp = Double.parseDouble(JF.getText()); } } } }