import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSQ1 extends JFrame implements ActionListener
{
private String name[] =
{"0","1", "2","+","3","4","5","-","6","7","8","*","9",".","=","/"};
private String extend[] =
{"x^2","ln","back","sin","cos","tan","√ ̄","off","清空"};
String s="";
int flag=0;
double x;
final double pi = 3.14159265358979323846;
private JButton button[] = new JButton[name.length];
private JButton button1[] = new JButton[extend.length];
JLabel dis_show = new JLabel("结果:");
JTextField window = new JTextField(12);
public JSQ1()
{
super("小小超级计算机");
setSize(200,300);
Container c = getContentPane();
c.setLayout(new GridLayout(2,1,0,0));
JPanel result = new JPanel();
result.add(dis_show);
result.add(window);
for(int i= 0;i<extend.length;i++)
{ button1[i] = new JButton(extend[i]);
result.add(button1[i]);
}
JPanel border = new JPanel();
for(int i= 0;i<name.length;i++)
{
button[i] = new JButton(name[i]);
border.add(button[i]);
}
c.add(result);
c.add(border);
for(int i= 0;i<name.length;i++)
{
button[i].addActionListener(this);
}
for(int i= 0;i<extend.length;i++)
{
button1[i].addActionListener(this);
}
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button[3])
{
x=Double.parseDouble(s);
flag=1;
window.setText("");
s="";
}
else if (e.getSource()==button[7])
{
x=Double.parseDouble(s);
flag=2;
window.setText("");
s=""; }
else if (e.getSource()==button[11])
{
x=Double.parseDouble(s);
flag=3;
window.setText("");
s="";
}
else if (e.getSource()==button[15])
{
x=Double.parseDouble(s);
flag=4;
window.setText("");
s=""; }
else if (e.getSource()==button[14])
{
switch(flag)
{
case 1:
{
x=x+Double.parseDouble(s);
String s=String.valueOf(x);
window.setText(s);
break;
}
case 2:
{
x=x-Double.parseDouble(s);
String s=String.valueOf(x);
window.setText(s);
break;
}
case 3:
{
x=x*Double.parseDouble(s);
String s=Double.toString(x);
window.setText(s);
break; }
case 4: {
if(Double.parseDouble(s)==0)
{
window.setText("除数不能为0");
break;
}
x=x/Double.parseDouble(s);
String s=String.valueOf(x);
window.setText(s);
break;
}
}
}
else if(e.getSource()==button1[2])
{
StringBuffer str = new StringBuffer(window.getText());
int n=str.length();
int m=n-1;
s = String.valueOf(str.delete(m,n));
window.setText(s); }
else if(e.getSource()==button1[8])
{
window.setText("");
s="";
flag=0;
}
else if(e.getSource()==button1[0])
{ x=Double.parseDouble(s);
x=x*x;
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[1])
{ x=Math.log(Double.parseDouble(s));
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[3])
{ x=Math.sin(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[6])
{ x=Math.sqrt(Double.parseDouble(s));
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[4])
{ x=Math.cos(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[5])
{
x=Math.tan(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s); }
else if(e.getSource()==button1[7])
{ System.exit(0);
} else
{
s=s+e.getActionCommand();
window.setText(s);
} }
public static void main(String[] args)
{
JSQ1 bun = new JSQ1();
bun.addWindowListener(new WindowAdapter()
{
public void windowListener(WindowEvent e)
{System.exit(0); } }); }}
看这个可以不???????????????