import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class sina extends WindowAdapter
{
public static void main(String []args)
{
(new sina()).chuangkou();
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
JTextField t=new JTextField(25);
JButton b=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("关闭");
JButton b10=new JButton("清空");
JButton b11=new JButton("9");
JButton b12=new JButton("0");
JButton b0=new JButton("=");
JButton b13=new JButton("+");
JButton b14=new JButton("-");
JButton b15=new JButton("*");
JButton b16=new JButton("/");
JButton b17=new JButton("说明");
JPanel p2=new JPanel();
JPanel p1=new JPanel();
public void chuangkou()
{
JFrame f=new JFrame("小小计算器");
f.addWindowListener(this);
f.setLocation(350,200);
f.setBackground(Color.green);
f.setSize(300,300);
f.setLayout(new BorderLayout());
p2.setLayout(new GridLayout(6,3,1,1));
b9.setBackground(Color.yellow);
//b0.setBackground(Color.blue);
b9.addActionListener(new close());
b.addActionListener(new zhong());
b2.addActionListener(new zhong());
b3.addActionListener(new zhong());
b4.addActionListener(new zhong());
b5.addActionListener(new zhong());
b6.addActionListener(new zhong());
b7.addActionListener(new zhong());
b8.addActionListener(new zhong());
b10.addActionListener(new zhong());
b11.addActionListener(new zhong());
b12.addActionListener(new zhong());
b0.addActionListener(new zhong());
b13.addActionListener(new zhong());
b14.addActionListener(new zhong());
b15.addActionListener(new zhong());
b16.addActionListener(new zhong());
b17.addActionListener(new zhong());
f.add(p1,"North");
f.add("Center",p2);
p1.add(t);
p2.add(b);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(b14);
p2.add(b15);
p2.add(b16);
p2.add(b17);
p2.add(b9);
p2.add(b10);
p2.add(b0);
f.setVisible(true);
}
class zhong implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton tt=(JButton)e.getSource();
if (tt.getLabel()=="=")
{
int b;
double c = 0.0;
String di,de;
String string=t.getText();
b=t.getText().indexOf("+");
char op [] = {'+','-','*','/'};
char inputOp;
int i=0;
for(;i<op.length;i++)
{
b=t.getText().indexOf(op[i]);
if(b!=-1)
break;
}
di=string.substring(0,b);
de=string.substring((b+1));
System.out.println(di+" "+de);
switch(op[i])
{
case '+':
c=Double.parseDouble(di)+Double.parseDouble(de);
break;
case '-':
c=Double.parseDouble(di)-Double.parseDouble(de);
break;
case '*':
c=Double.parseDouble(di)*Double.parseDouble(de);
break;
case '/':
c=Double.parseDouble(di)/Double.parseDouble(de);
break;
default :
System.out.println("something is wrong...");
}
String aa=String.valueOf(c);
t.setText(aa);
}
else
t.setText(t.getText()+tt.getLabel());
if(tt.getLabel()=="说明")
t.setText("先清空再使用 开发者:明志");
if(tt.getLabel()=="清空")
t.setText("");
}
}
}
class close implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
}