import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.lang.Double;
import java.text.*;
import java.awt.event.*;
import javax.swing.*;
public class CalculatorFrame extends Frame implements ActionListener
{
private TextField text;//text_input;
public boolean firstWrite = true;
public boolean firstCalculate = true;
public JPanel cPanel1, cPanel2, cPanel3, cPanel4;
private Button button_0,button_1,button_2,button_3,button_4,button_5,button_6,button_7,button_8,
button_9,button_jia,button_jian,button_cheng,button_chu,button_cancel,button_deng;
public CalculatorFrame()
{
JFrame jf = new JFrame("简易计算器");
//super("简易计算器");
cPanel1 = new JPanel();
cPanel2 = new JPanel(new GridLayout(3,3));
cPanel3 = new JPanel(new GridLayout(1,3));
cPanel4 = new JPanel(new GridLayout(4,1));
//this.setLayout(new GridLayout(1,1));
text=new TextField(30);
text.setEditable(false);
text.setBackground (Color.white);
cPanel1.add(text);
//this.add(text_input);
//this.setLayout(new GridLayout(4,4));
button_0=new Button("0");
button_0.setBackground ( Color.getColor ("#000ffff") );
button_1=new Button("1");
button_2=new Button("2");
button_3=new Button("3");
button_4=new Button("4");
button_5=new Button("5");
button_6=new Button("6");
button_7=new Button("7");
button_8=new Button("8");
button_9=new Button("9");
button_jia=new Button("+");
button_jian=new Button("-");
button_cheng=new Button("*");
button_chu=new Button("/");
button_cancel=new Button("c");
button_deng=new Button("=");
cPanel3.add(button_0);
cPanel2.add(button_1);
cPanel2.add(button_2);
cPanel2.add(button_3);
cPanel2.add(button_4);
cPanel2.add(button_5);
cPanel2.add(button_6);
cPanel2.add(button_7);
cPanel2.add(button_8);
cPanel2.add(button_9);
cPanel4.add(button_jia);
cPanel4.add(button_jian);
cPanel4.add(button_cheng);
cPanel4.add(button_chu);
cPanel3.add(button_cancel);
cPanel3.add(button_deng);
button_0.addActionListener(this);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_6.addActionListener(this);
button_7.addActionListener(this);
button_8.addActionListener(this);
button_9.addActionListener(this);
button_jia.addActionListener(this);
button_jian.addActionListener(this);
button_cheng.addActionListener(this);
button_chu.addActionListener(this);
button_cancel.addActionListener(this);
button_deng.addActionListener(this);
jf.addWindowListener(new WinClose());
jf.getContentPane ().add (cPanel1, "North");
jf.getContentPane ().add (cPanel2, "Center");
jf.getContentPane ().add (cPanel3, "South");
jf.getContentPane ().add (cPanel4, "East");
cPanel1.setSize (20,40);
cPanel2.setSize (210,210);
cPanel3.setSize (70,280);
cPanel4.setSize (210,70);
jf.setSize (400,400);
jf.setLocation (300,300);
jf.setVisible (true);
}
public void result(ActionEvent e)
{
int cheng = 0;
int chu = 0;
int jia = 0;
int jian = 0;
double result=0.0;
String showResult = null;
/*----
boolean one = true;
int whichSymbol = 1;
int m = saveCommand.size();
for(int i= 0;i < m;i++)
{
switch(saveCommand.get(i))
{
case '0':if(one)numFir=0;else numSec[I]=0; break;
case '1':if(one)numFir[I]=1;else numSec[I]=1; break;
case '2':if(one)numFir[I]=2;else numSec[I]=2; break;
case '3':if(one)numFir[I]=3;else numSec[I]=3; break;
case '4':if(one)numFir[I]=4;else numSec[I]=4; break;
case '5':if(one)numFir[I]=5;else numSec[I]=5; break;
case '6':if(one)numFir[I]=6;else numSec[I]=6; break;
case '7':if(one)numFir[I]=7;else numSec[I]=7; break;
case '8':if(one)numFir[I]=8;else numSec[I]=8; break;
case '9':if(one)numFir[I]=9;else numSec[I]=9; break;
case '*':whichSymbol=4; break;
case '/':whichSymbol=3; break;
case '+':whichSymbol=2; break;
case '-':whichSymbol=1; break;
default :break;
}
}
---*/
String commandArray = text.getText();
cheng = commandArray.indexOf('*');
chu = commandArray.indexOf('/');
jia = commandArray.indexOf('+');
jian = commandArray.indexOf('-');
if(0<cheng && cheng<8)
{
double firNum = Double.parseDouble(commandArray.substring(0,cheng));
double sndNum = Double.parseDouble(commandArray.substring(cheng+1));
result = firNum * sndNum;
showResult = String.valueOf(result);
if(showResult.length()>6)
showResult.substring(0,6);
}
if(0<chu && chu<8)
{
double firNum = Double.parseDouble(commandArray.substring(0,chu));
double sndNum = Double.parseDouble(commandArray.substring(chu+1));
result = firNum / sndNum;
//result = Math.round(result);
DecimalFormat decimal = new DecimalFormat("#.######");
//String piStr = decimal.format(pi);
//showResult = String.valueOf(result);
showResult = decimal.format(result);
}
if(0<jia && jia<8)
{
double firNum = Double.parseDouble(commandArray.substring(0,jia));
double sndNum = Double.parseDouble(commandArray.substring(jia+1));
result = firNum + sndNum;
showResult = String.valueOf(result);
if(showResult.length()>6)
showResult.substring(0,6);
}
if(0<jian && jian<8)
{
double firNum = Double.parseDouble(commandArray.substring(0,jian));
double sndNum = Double.parseDouble(commandArray.substring(jian+1));
result = firNum - sndNum;
showResult = String.valueOf(result);
if(showResult.length()>6)
showResult.substring(0,6);
}
//if(Math.abs(result)>0.0000001&&Math.abs(result)<10000000)
//{
firstCalculate = true;
firstWrite = true;
text.setText(""+showResult);
//}
//else
//{
//firstWrite = false;
//text.setText("Error");
//}
}
public void actionPerformed(ActionEvent e)
{
int i=0;
ArrayList saveCommand=new ArrayList();
if(e.getSource()==button_cancel)
text.setText("");
if(e.getSource() == button_deng)
{
this.result(e);
}
if(e.getSource() == button_0)
{
//saveCommand.add("0");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_1)
{
//saveCommand.add("1");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_2)
{
//saveCommand.add("2");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_3)
{
//saveCommand.add("3");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_4)
{
//saveCommand.add("4");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_5)
{
//saveCommand.add("5");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_6)
{
//saveCommand.add("6");
if(firstWrite)
{
text.setText(""+e.getActionCommand());
firstWrite = false;
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_7)
{
//saveCommand.add("7");
if(firstWrite)
{
firstWrite = false;
text.setText(""+e.getActionCommand());
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_8)
{
//saveCommand.add("8");
if(firstWrite)
{
firstWrite = false;
text.setText(""+e.getActionCommand());
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_9)
{
//saveCommand.add("9");
if(firstWrite)
{
firstWrite = false;
text.setText(""+e.getActionCommand());
}
else
text.setText(text.getText()+e.getActionCommand());
}
if(e.getSource() == button_jia)
{
//saveCommand.add("+");
if(firstCalculate)
{
text.setText(text.getText()+e.getActionCommand());
}
else
{
this.result (e);
text.setText(text.getText()+e.getActionCommand());
}
firstWrite = false;
firstCalculate = false;
}
if(e.getSource() == button_jian)
{
//saveCommand.add("-");
if(firstCalculate)
{
text.setText(text.getText()+e.getActionCommand());
}
else
{
this.result (e);
text.setText(text.getText()+e.getActionCommand());
}
firstWrite = false;
firstCalculate = false;
}
if(e.getSource() == button_cheng)
{
//saveCommand.add("*");
if(firstCalculate)
{
text.setText(text.getText()+e.getActionCommand());
}
else
{
this.result (e);
text.setText(text.getText()+e.getActionCommand());
}
firstWrite = false;
firstCalculate = false;
}
if(e.getSource() == button_chu)
{
//saveCommand.add("/");
if(firstCalculate)
{
text.setText(text.getText()+e.getActionCommand());
}
else
{
this.result (e);
text.setText(text.getText()+e.getActionCommand());
}
firstWrite = false;
firstCalculate = false;
}
}
public static void main(String arg[])
{
new CalculatorFrame();
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
};