java计算器源代码

来源:互联网 发布:手机文件查看软件 编辑:程序博客网 时间:2024/05/17 07:25
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSQ 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","cls"};
String s=new String("");
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 JSQ()
{
super("woodpecker小小计算机");
setSize(200,290);
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 {
button1[i] = new JButton(extend[i]);
result.add(button1[i]);
}
JPanel border = new JPanel();
for(int i= 0;i {
button[i] = new JButton(name[i]);
border.add(button[i]);
}
c.add(result);
c.add(border);
for(int i= 0;i {
button[i].addActionListener(this);
}
for(int i= 0;i {
button1[i].addActionListener(this);
}
show();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==button[3])
{
s=window.getText();
x=Double.parseDouble(s);
flag=1;
window.setText("");
s="";
}
else if (e.getSource()==button[7])
{
s=window.getText();
x=Double.parseDouble(s);
flag=2;
window.setText("");
s="";
}
else if (e.getSource()==button[11])
{
s=window.getText();
x=Double.parseDouble(s);
flag=3;
window.setText("");
s="";
}
else if (e.getSource()==button[15])
{
s=window.getText();
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=String.valueOf(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])
{
s=window.getText();
x=Double.parseDouble(s);
x=x*x;
String s=String.valueOf(x);
window.setText(s);
}
else if(e.getSource()==button1[1])
{
s=window.getText();
x=Math.log(Double.parseDouble(s));
String s=String.valueOf(x);
window.setText(s);
}
else if(e.getSource()==button1[3])
{
s=window.getText();
x=Math.sin(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s);
}
else if(e.getSource()==button1[6])
{
s=window.getText();
x=Math.sqrt(Double.parseDouble(s));
String s=String.valueOf(x);
window.setText(s);
}
else if(e.getSource()==button1[4])
{
s=window.getText();
x=Math.cos(Double.parseDouble(s)*pi/180);
String s=String.valueOf(x);
window.setText(s);
}
else if(e.getSource()==button1[5])
{
s=window.getText();
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)
{
JSQ bun = new JSQ();
bun.addWindowListener(new WindowAdapter()
{
public void windowListener(WindowEvent e)
{
System.exit(0);
}
});
}
}
原创粉丝点击