Java簡易計算機

爲了完成Java作業,隨便寫了個計算器(只能簡單加減乘除)

先上圖

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 * 
 * @author 田園小菜籽
 *
 */
public class Count extends JApplet implements ActionListener
{
    private JTextField textField = new JTextField("請輸入",12);
    String operator = "";      //操作
    String input = "";        //輸入的 式子
    boolean flag =  true;
    public void init()     //重寫Applet裏邊的init方法
    {
        textField.setFont(new Font("宋體",Font.PLAIN,50));//設置字體格式
        textField.setEditable(false);           //設置文本框不可更改
        Container C = getContentPane();
        JButton b[] = new JButton[17];
        JPanel panel = new JPanel();
        JPanel panel1 = new JPanel();
        panel1.add(textField);
        panel.setLayout(new GridLayout(4, 4,5,5));
        panel1.setLayout(new FlowLayout(3));
        C.add(panel,BorderLayout.CENTER);
        C.add(panel1,BorderLayout.NORTH);
        String name[]={"7","8","9","+","4","5","6","-","1","2","3","*",".","0","=","/","C"};//設置按鈕
        for(int i=0;i<17;i++)//添加按鈕
        {
            b[i] = new JButton(name[i]);
            b[i].setBackground(new Color(192,192,192));
            b[i].setForeground(Color.BLACK);  //設置按鍵顏色
            if(i%4==3)
                b[i].setForeground(Color.RED);//設置按鍵顏色
            b[i].setFont(new Font("宋體",Font.PLAIN,16));//設置字體格式
            panel.add(b[i]);//將按鍵添加到界面
            b[i].addActionListener(this);
        }
           panel1.add(b[16]);
        b[16].setPreferredSize(new Dimension(65,65));


        b[13].setForeground(Color.RED);
    }
    public void actionPerformed(ActionEvent e)
    {
        int cnt = 0;
        String actionCommand = e.getActionCommand();
        if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))
            input +=" "+actionCommand+" ";//設置輸入,把輸入的樣式改成 需要的樣子
        else if(actionCommand.equals("C"))
            input = "";
        else if(actionCommand.equals("="))//當監聽到等號時,則處理 input
        {
            input+= "="+compute(input);
            textField.setText(input);
            input="";
            cnt = 1;
        }
        else
            input += actionCommand;//數字爲了避免多位數的輸入 不需要加空格
        if(cnt==0)
            textField.setText(input);
    }
    /*使用棧對數據進行計算*/
    private String compute(String input)//
    {
        String str[];
        str = input.split(" ");
        Stack<Double> s = new Stack<Double>();
        double m = Double.parseDouble(str[0]);
        s.push(m);
        for(int i=1;i<str.length;i++)
        {
            if(i%2==1)
            {
                if(str[i].compareTo("+")==0)
                {
                    double help = Double.parseDouble(str[i+1]);
                    s.push(help);
                }
                if(str[i].compareTo("-")==0)
                {
                    double help = Double.parseDouble(str[i+1]);
                    s.push(-help);
                }
                if(str[i].compareTo("*")==0)
                {
                    double help = Double.parseDouble(str[i+1]);
                    double ans = s.peek();//取出棧頂元素
                    s.pop();//消棧
                    ans*=help;
                    s.push(ans);
                }
                if(str[i].compareTo("/")==0)
                {
                    double help = Double.parseDouble(str[i+1]);
                    double ans = s.peek();
                    s.pop();
                    ans/=help;
                    s.push(ans);
                }
            }
        }
        double ans = 0d;
        while(!s.isEmpty())
        {
            ans+=s.peek();
            s.pop();
        }
        String result = String.valueOf(ans);
        return result;
    }
    public static void main(String args[])
    {
        JFrame frame = new JFrame("Counter");//創建頂級窗口
        frame.setResizable(false);
        Count applet = new Count();
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        applet.init();     //applet的init方法
        applet.start();    //線程開始
        frame.setSize(400, 450);  //設置窗口大小
        frame.setVisible(true);    //設置窗口可見
    }
}

ps:這個小程序拿來交個小作業還行,先說明本人打註釋的習慣比較差,如要使用請自己標註釋。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章