import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
public class LittleCalculator implements ActionListener,WindowListener{
Frame f;
TextField t;
Button b[];
String name[]={"7","8","9","sqrt","CE","4","5","6","%","1/x","1","2","3","*","/","0",".","=","-","+"};
Panel p1=new Panel();
Panel p2=new Panel(); //創建2個面板,用於放文本、按鈕
Double num1;
Double num2;
String tmp;
Double num;
int i;
public void tt(){
f=new Frame("計算器");
t=new TextField(24);
Button[] b=new Button[name.length];
f.addWindowListener(this);//關閉窗口的功能
MenuBar m=new MenuBar();
Menu me1=new Menu("查看(V)");
Menu me2=new Menu("編輯(E)");
Menu me3=new Menu("幫助(H)");
MenuItem mt1=new MenuItem("控制檯");
MenuItem mt2=new MenuItem("控制檯2");
MenuItem mt3=new MenuItem("控制檯3");//設置菜單的功能
mt1.addActionListener(this);
mt2.addActionListener(this);
mt3.addActionListener(this);
me1.add(mt1);
me2.add(mt2);
me3.add(mt3);
m.add(me1);
m.add(me2);
m.add(me3);
f.setMenuBar(m);
p1.add(t);
f.add(p1,BorderLayout.NORTH);
p2.setLayout(new GridLayout(4,5,2,2));
for(int i=0;i<name.length;i++){
b[i]=new Button(name[i]);//這裏是按鈕的佈局
p2.add(b[i]);
}
f.add(p2,BorderLayout.CENTER);
for(int i=0;i<name.length;i++){
b[i].addActionListener(this);}
f.setBackground(Color.blue);
f.setSize(200,200);
f.setVisible(true);
}
public void windowClosing(WindowEvent e)
{System.exit(0);}
public void windowClosed(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}//實現關閉窗口
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void actionPerformed(ActionEvent e){//實現消息映射
if(e.getActionCommand().equals("0")||e.getActionCommand().equals("1")||e.getActionCommand().equals("2")||e.getActionCommand().equals("3")||e.getActionCommand().equals("4")||e.getActionCommand().equals("5")||e.getActionCommand().equals("6")||e.getActionCommand().equals("7")||e.getActionCommand().equals("8")||e.getActionCommand().equals("9"))
t.setText(t.getText()+e.getActionCommand());//將標籤中的數字放入文本框中,並且是累加的,不然就只能存放一位數
if(e.getActionCommand().equals("CE"))
{
t.setText(""); //如果按鈕是CE,那麼就講文本中內容置空
}
if(e.getActionCommand().equals("+")){
tmp=t.getText();
num=Double.parseDouble(tmp);//如果爲+,把之前文本框中的字符串轉換成爲數字,然後將文本框清空,以備存放下一個數字
t.setText("");//-、*、/同理
i=0; //做一個標記
}
if(e.getActionCommand().equals("-")){
tmp=t.getText();
num=Double.parseDouble(tmp);
t.setText("");
i=1;
}
if(e.getActionCommand().equals("*")){
tmp=t.getText();
num=Double.parseDouble(tmp);
t.setText("");
i=2;
}
if(e.getActionCommand().equals("/")){
tmp=t.getText();
num=Double.parseDouble(tmp);
t.setText("");
i=3;
}
if(e.getActionCommand().equals(".")){
t.setText(t.getText()+".");
}
//下面幾個就是單目運算了啦~
if(e.getActionCommand().equals("sqrt")){
tmp=t.getText();
num1=Double.parseDouble(tmp);//把字符串轉成數字計算,再把計算出來的數字轉成字符串顯示到文本框
num2=Math.sqrt(num1);//%、1/x同理
t.setText(Double.toString(num2));
}
if(e.getActionCommand().equals("%")){
tmp=t.getText();
num1=Double.parseDouble(tmp);
num2=num1/100;
t.setText(Double.toString(num2));
}
if(e.getActionCommand().equals("1/x")){
tmp=t.getText();
num1=Double.parseDouble(tmp);
num2=1/num1;
t.setText(Double.toString(num2));
}
//最後是等於號
if(e.getActionCommand().equals("=")||e.getActionCommand().equals("Enter")){
tmp=t.getText();
double num_after=Double.parseDouble(tmp);//這是把等於號之前,+號之後的字符串轉換成數字,因爲在+號的時候已經將前面內容清空了
if(i==0){
t.setText(Double.toString(num+num_after));
}
if(i==1){
t.setText(Double.toString(num-num_after));
}
if(i==2){
t.setText(Double.toString(num*num_after));
}
if(i==3){
if(num_after==0){
t.setText("");
System.out.println("除數不能爲零!");
}
else{
t.setText(Double.toString(num/num_after));
}
}
}
}
public static void main(String args[]){
LittleCalculator p=new LittleCalculator();
p.tt();
}
}