package homework_4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class Calculator extends Jframe
{
double date[]=new double[5]; //暂存数字
double t=0,num=0,error=1;
int flag=1,i=0,k=1,z=1;
int j=1;//次方
String q; //符号
String sum=""; //结果
String s=""; //计算式
JPanel Jp1=new JPanel(); //设置面板
JPanel Jp2=new JPanel();
JPanel Jp3=new JPanel();
//设置数字按钮
private JButton j1=new JButton("1");
private JButton j2=new JButton("2");
private JButton j3=new JButton("3");
private JButton j4=new JButton("4");
private JButton j5=new JButton("5");
private JButton j6=new JButton("6");
private JButton j7=new JButton("7");
private JButton j8=new JButton("8");
private JButton j9=new JButton("9");
private JButton j0=new JButton("0");
private JButton jo=new JButton(".");
//设置加减乘除等于按钮
private JButton jadd=new JButton("+");
private JButton jf=new JButton("-");
private JButton jch=new JButton("*");
private JButton jchu=new JButton("/");
private JButton je=new JButton("=");
private JButton jsin=new JButton("sin");
private JButton jcos=new JButton("cos");
private JButton jln=new JButton("ln");
private JButton jsqrt=new JButton("sqrt");
private JButton jxy=new JButton("x^y");
private JButton jac=new JButton("清除");
private JButton jb=new JButton("%");
//设置输入输出文本框
JTextField input=new JTextField(21);
JTextField output=new JTextField(10);
JLabel lab=new JLabel("=");
//添加侦听事件
class Calculate implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if(str=="+"||str=="-"||str=="*"||str=="/")
{
flag=0;
i++;
j=1; //次方再从1开始
z=1;
s=s+str;
sum=s;
q=str;
input.setText(s);
}
else if(str=="=")
{
if(q=="+")
num=date[0]+date[1];
else if(q=="-")
num=date[0]-date[1];
else if(q=="*")
num=date[0]*date[1];
else if(q=="/")
{
if(date[1]!=0)
num=date[0]/date[1];
else
error=0; //除数是0
}
if(k==1&&error==1) //如果是整数运算
sum=""+(int)num;
else if(k==1&&error==0)
sum="Math error";
else if(k==0&&error==1)//如果是浮点数运算
sum=""+(double)num;
else if(k==0&&error==0)
sum="Math error";
output.setText(sum); //输出结果
}
else if(str=="清除") //清除
{
s="";
sum="";
date[0]=0;
date[1]=0;
i=0;
error=1;
k=1;
input.setText(s);
output.setText(s);
}
else //如果是数字
{
flag=1;
s=s+str;
sum=s;
if(flag==1&&z==1&&str!=".")
{
date[i]=date[i]*10+Integer.parseInt(str);
}
else if(flag==1&&z==0&&str!=".") //出现"."后的运算
{
date[i]=date[i]+Integer.parseInt(str)*Math.pow(0.1, j);
j++; //次方
}
else if(flag==1&&str==".")
{
z=0; //记录出现了"."
k=0; //记录为浮点数运算
}
input.setText(s);
}
}
}
//设置布局
public Calculator()
{
setTitle("My Calculator"); //设置标题
Jp1.add(input); //添加文本框进入面板Jp1
Jp1.add(lab);
Jp1.add(output);
//添加文本框,放置在第一列
input.setHorizontalAlignment(JTextField.LEFT); //设置对齐方式
input.setText("请输入两个整数或浮点数的运算:");
input.setFont(new Font("谐体",Font.BOLD|Font.ITALIC,12));
output.setHorizontalAlignment(JTextField.LEFT); //设置对齐方式
output.setFont(new Font("谐体",Font.BOLD|Font.ITALIC,12));
//添加按钮进入面板Jp2、Jp3
Jp2.add(j1);Jp2.add(j2);Jp2.add(j3);Jp2.add(j4);Jp2.add(j5);Jp2.add(j6);Jp2.add(j7);Jp2.add(j8);Jp2.add(j9);Jp2.add(j0);Jp2.add(jo);Jp2.add(je);
Jp3.add(jadd);Jp3.add(jf);Jp3.add(jch);Jp3.add(jac);Jp3.add(jchu);Jp3.add(jcos);Jp3.add(jln);Jp3.add(jxy);Jp3.add(jsqrt);Jp3.add(jsin);
Jp3.add(jb);
jac.setFont(new Font("谐体",Font.BOLD|Font.ITALIC,13));
//设置面板布局
Jp1.setLayout(new FlowLayout());
Jp2.setLayout(new GridLayout(3,4));
Jp3.setLayout(new GridLayout(3,4));
this.add(Jp1,BorderLayout.NORTH);
this.add(Jp2,BorderLayout.WEST);
this.add(Jp3,BorderLayout.EAST);
ActionListener listener=new Calculate(); //初始化事件侦听者
j1.addActionListener(listener);j6.addActionListener(listener);j7.addActionListener(listener);j8.addActionListener(listener);j9.addActionListener(listener);
j2.addActionListener(listener);j3.addActionListener(listener);j4.addActionListener(listener);j5.addActionListener(listener);j0.addActionListener(listener);
jo.addActionListener(listener);jadd.addActionListener(listener);jf.addActionListener(listener);jch.addActionListener(listener);jac.addActionListener(listener);
jcos.addActionListener(listener);jsin.addActionListener(listener);jln.addActionListener(listener);jxy.addActionListener(listener);jsqrt.addActionListener(listener);
jchu.addActionListener(listener);je.addActionListener(listener); //添加侦听事件
}
public static void main(String[] args)
{
Calculator frame=new Calculator();
frame.setSize(422,350);
frame.setVisible(true);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)