Javapet版的计算器!!
近日做了一个简单的计算器,希望对一些初学的朋友有帮助,当然也希望前辈们能指点指点,使我的程序能得到优化!共同学习,共同进步,共同迈向......!
package Counter;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Counter extends JFrame
{
StringBuffer str1=null;
StringBuffer str2=null;
StringBuffer str3=null;
StringBuffer str4=null;
Button[] B=new Button[27];
JTextField JText=null;
TextField Text=null;
int flag=5,v=0,z=0,m=0,q=0,w=0;
boolean bool=false;
public Counter(String str)
{
super(str);
this.setLayout(null);
Container Container_=this.getContentPane();
str1=new StringBuffer();
str2=new StringBuffer();
str3=new StringBuffer();
str4=new StringBuffer();
this.pet(Container_);
}
public void pet(Container Container_)
{
//创建布局管理器
GridLayout GridLayout1=new GridLayout(1,3,5,5);
GridLayout GridLayout2=new GridLayout(4,1,5,5);
GridLayout GridLayout3=new GridLayout(4,5,5,5);
//实例化JPanel
JPanel JP1=new JPanel();
JPanel JP2=new JPanel();
JPanel JP3=new JPanel();
JPanel JP4=new JPanel();
JPanel JP5=new JPanel();
//创建菜单栏
MenuBar Menu_Bar=new MenuBar();
Menu Menu_a=new Menu("编辑");
Menu Menu_b=new Menu("帮助");
MenuItem Menu_Item_a=new MenuItem("复制");
MenuItem Menu_Item_b=new MenuItem("粘贴");
MenuItem Menu_Item_c=new MenuItem("关于计算机");
Menu_a.add(Menu_Item_a);
Menu_a.add(Menu_Item_b);
Menu_b.add(Menu_Item_c);
Menu_Bar.add(Menu_a);
Menu_Bar.add(Menu_b);
this.setMenuBar(Menu_Bar);
//创建布置数值显示屏和记忆屏
JText=new JTextField("0.",21);
JText.setBackground(Color.white);
JText.setHorizontalAlignment(JTextField.RIGHT );
JText.setEditable(false);
Text=new TextField(1);
Text.setBackground(Color.white);
Text.setEnabled(false);
JP4.add(JText);
JP5.add(Text);
JP4.setBounds(2,0,250,30);
JP5.setBounds(26,30,0,0);
//创建按钮并设置文本颜色
B[0]=new Button("MC");
B[1]=new Button("MR");
B[2]=new Button("MS");
B[3]=new Button("M+");
B[4]=new Button("Backspace");
B[5]=new Button("CE");
B[6]=new Button("C");
B[7]=new Button("7");
B[8]=new Button("8");
B[9]=new Button("9");
B[10]=new Button("/");
B[11]=new Button("sqrt");
B[12]=new Button("4");
B[13]=new Button("5");
B[14]=new Button("6");
B[15]=new Button("*");
B[16]=new Button("%");
B[17]=new Button("1");
B[18]=new Button("2");
B[19]=new Button("3");
B[20]=new Button("-");
B[21]=new Button("1/x");
B[22]=new Button("0");
B[23]=new Button("+/-");
B[24]=new Button(".");
B[25]=new Button("+");
B[26]=new Button("=");
for(int i=0;i<12;i++)
{
B[i].setForeground(Color.red);
}
for(int i=12;i<26;i++)
{
B[i].setForeground(Color.blue);
}
//布置JP1
JP1.setLayout(GridLayout1);
JP1.add(B[4]);
JP1.add(B[5]);
JP1.add(B[6]);
JP1.setBounds(50,33,197,27);
//布置JP2
JP2.setLayout(GridLayout2);
JP2.add(B[0]);
JP2.add(B[1]);
JP2.add(B[2]);
JP2.add(B[3]);
JP2.setBounds(9,65,35,120);
//布置JP3
JP3.setLayout(GridLayout3);
for(int i=7;i<27;i++)
{
JP3.add(B[i]);
}
JP3.setBounds(50,65,195,120);
//添加窗口监听器
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
//添加按钮监听器
for(int i=0;i<27;i++)
{
B[i].addActionListener(new monitor(this));
}
//添加菜单监听器
Menu_Item_a.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
str4.append(JText.getText());
}
});
Menu_Item_b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JText.setText(str4.toString());
str4.setLength(0);
}
});
Menu_Item_c.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JDialog JD=new JDialog();
JD.setSize(150,200);
JD.setLocation(350,300);
JD.getContentPane().add(new JLabel("共同学习交流,彼此进步!"));
JD.setVisible(true);
}
});
//容器整合
Container_.add(JP1);
Container_.add(JP2);
Container_.add(JP3);
Container_.add(JP4);
Container_.add(JP5);
}
public void paintComponent(Graphics g)
{
Graphics2D g1=(Graphics2D)g;
g1.setColor(Color.white);
}
public static void main(String[] args)
{
Counter C=new Counter("计算器(Javapet制作)");
C.setSize(260,246);
C.setLocation(200,200);
C.setResizable(false);
C.setVisible(true);
}
}
class monitor implements ActionListener
{
Counter C;
Double t=0.0;
Double x=0.0,y=0.0;
public monitor(Counter C)
{
this.C=C;
}
public void actionPerformed(ActionEvent e)
{
try
{
if(e.getSource()==C.B[0])
{
C.str1.setLength(0);
C.str3.setLength(0);
C.Text.setText("");
C.q=1;
}
if(e.getSource()==C.B[1])
{
C.str1.setLength(0);
if(C.q!=0)
{
C.JText.setText("0.");
}
else
{
C.JText.setText(C.str3.toString());
}
}
else if(e.getSource()==C.B[2])
{
if(C.w!=0)
{
C.str3.setLength(0);
}
C.str3.append(C.JText.getText());
C.Text.setText(" "+"M");
C.str1.setLength(0);
C.w=1;
}
else if(e.getSource()==C.B[3])
{
if(C.w!=0)
{
C.str3.setLength(0);
}
C.str3.append(C.JText.getText());
C.Text.setText(" "+"M");
C.str1.setLength(0);
C.w=1;
}
else if(e.getSource()==C.B[4])
{
if(!C.bool)
{
C.str1.delete(C.str1.length()-1,C.str1.length());
C.JText.setText(C.str1.toString());
}
else
{
C.str2.delete(C.str2.length()-1,C.str2.length());
C.JText.setText(C.str2.toString());
}
}
else if(e.getSource()==C.B[5])
{
C.str1.setLength(0);
C.str2.setLength(0);
C.JText.setText("0.");
C.bool=false;
C.v=0;
C.z=0;
C.q=0;
C.w=0;
}
else if(e.getSource()==C.B[6])
{
C.str1.setLength(0);
C.str2.setLength(0);
C.JText.setText("0.");
C.bool=false;
C.v=0;
C.z=0;
C.q=0;
C.w=0;
}
else if(e.getSource()==C.B[7])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"7");
C.str1.append("7");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"7");
C.str2.append("7");
}
}
}
else if(e.getSource()==C.B[8])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"8");
C.str1.append("8");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"8");
C.str2.append("8");
}
}
}
else if(e.getSource()==C.B[9])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"9");
C.str1.append("9");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"9");
C.str2.append("9");
}
}
}
else if(e.getSource()==C.B[10])
{
if(C.str2.length()!=0&&C.str1.length()!=0)
{
switch(C.flag)
{
case 1:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x+y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 2:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x-y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 3:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x*y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 4:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x/y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
}
}
C.flag=4;
C.z=0;
C.m=0;
if(C.str1.length()!=0)
{
C.bool=true;
}
}
else if(e.getSource()==C.B[11])
{
if(!C.bool)
{
String t=String.valueOf(Math.sqrt(Double.parseDouble(C.str1.toString())));
C.str1.setLength(0);
C.str1.append(t);
C.JText.setText(C.str1.toString());
}
else
{
String t=String.valueOf(Math.sqrt(Double.parseDouble(C.str2.toString())));
C.str2.setLength(0);
C.str2.append(t);
C.JText.setText(C.str2.toString());
}
}
else if(e.getSource()==C.B[12])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"4");
C.str1.append("4");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"4");
C.str2.append("4");
}
}
}
else if(e.getSource()==C.B[13])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"5");
C.str1.append("5");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"5");
C.str2.append("5");
}
}
}
else if(e.getSource()==C.B[14])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"6");
C.str1.append("6");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"6");
C.str2.append("6");
}
}
}
else if(e.getSource()==C.B[15])
{
if(C.str2.length()!=0&&C.str1.length()!=0)
{
switch(C.flag)
{
case 1:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x+y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 2:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x-y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 3:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x*y;
System.out.println(t);
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 4:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x/y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
}
}
C.flag=3;
C.z=0;
C.m=0;
if(C.str1.length()!=0)
{
C.bool=true;
}
}
else if(e.getSource()==C.B[16])
{
if(!C.bool)
{
String t=String.valueOf(0.01*(Double.parseDouble(C.str1.toString())));
C.str1.setLength(0);
C.str1.append(t);
C.JText.setText(C.str1.toString());
}
else
{
String t=String.valueOf(0.01*(Double.parseDouble(C.str2.toString())));
C.str2.setLength(0);
C.str2.append(t);
C.JText.setText(C.str2.toString());
}
}
else if(e.getSource()==C.B[17])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"1");
C.str1.append("1");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"1");
C.str2.append("1");
}
}
}
else if(e.getSource()==C.B[18])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"2");
C.str1.append("2");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"2");
C.str2.append("2");
}
}
}
else if(e.getSource()==C.B[19])
{
if(!C.bool)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"3");
C.str1.append("3");
}
}
else
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"3");
C.str2.append("3");
}
}
}
else if(e.getSource()==C.B[20])
{
if(C.str2.length()!=0&&C.str1.length()!=0)
{
switch(C.flag)
{
case 1:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x+y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 2:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x-y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 3:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x*y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 4:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x/y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
}
}
C.flag=2;
C.z=0;
C.m=0;
if(C.str1.length()!=0)
{
C.bool=true;
}
}
else if(e.getSource()==C.B[21])
{
if(!C.bool)
{
String t=String.valueOf(1/(Double.parseDouble(C.str1.toString())));
C.str1.setLength(0);
C.str1.append(t);
C.JText.setText(C.str1.toString());
}
else
{
String t=String.valueOf(1/(Double.parseDouble(C.str2.toString())));
C.str2.setLength(0);
C.str2.append(t);
C.JText.setText(C.str2.toString());
}
}
else if(e.getSource()==C.B[22])
{
if(!C.bool)
{
if(C.str1.length()!=0)
{
if(C.str1.length()<8)
{
C.JText.setText(C.str1.toString()+"0");
C.str1.append("0");
}
}
else
{
if(C.v!=1)
{
C.v=1;
C.JText.setText("0.");
C.str2.append("0");
}
}
}
else
{
if(C.str2.length()!=0)
{
if(C.str2.length()<8)
{
C.JText.setText(C.str2.toString()+"0");
C.str2.append("0");
}
}
else
{
if(C.flag==4)
{
C.JText.setText("不能除以0");
C.str1.setLength(0);
C.str2.setLength(0);
C.flag=5;
C.bool=false;
}
else if(C.v!=1)
{
C.JText.setText("0.");
C.str2.append("0");
C.v=1;
}
}
}
}
else if(e.getSource()==C.B[23])
{
if(!C.bool)
{
if(C.m!=1)
{
C.str1.insert(0,"-");
C.JText.setText(C.str1.toString());
C.m=1;
}
else
{
C.str1.deleteCharAt(0);
C.JText.setText(C.str1.toString());
C.m=0;
}
}
else
{
if(C.m!=1)
{
C.str2.insert(0,"-");
C.JText.setText(C.str2.toString());
C.m=1;
}
else
{
C.str2.deleteCharAt(0);
C.JText.setText(C.str2.toString());
C.m=0;
}
}
}
else if(e.getSource()==C.B[24])
{
if(!C.bool)
{
if(C.str1.length()!=0&&C.z!=1)
{
C.str1.append(".");
C.z=1;
}
else
{
if(C.z!=1)
{
C.str1.append("0.");
C.JText.setText("0.");
C.z=1;
}
}
}
else
{
if(C.str2.length()!=0&&C.z!=1)
{
C.str2.append(".");
C.z=1;
}
else
{
if(C.z!=1)
{
C.str2.append("0.");
C.JText.setText("0.");
C.z=1;
}
}
}
}
else if(e.getSource()==C.B[25])
{
if(C.str2.length()!=0&&C.str1.length()!=0)
{
switch(C.flag)
{
case 1:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x+y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 2:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x-y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 3:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x*y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
case 4:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x/y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
break;
}
}
C.flag=1;
C.z=0;
C.m=0;
if(C.str1.length()!=0)
{
C.bool=true;
}
}
else if(e.getSource()==C.B[26])
{
if(C.str2.length()!=0&&C.str1.length()!=0)
{
switch(C.flag)
{
case 1:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x+y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
C.z=0;
break;
case 2:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x-y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
C.z=0;
break;
case 3:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x*y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
C.z=0;
break;
case 4:
x=Double.parseDouble(C.str1.toString());
y=Double.parseDouble(C.str2.toString());
t=x/y;
C.str2.setLength(0);
C.JText.setText(String.valueOf(t));
C.str1.setLength(0);
C.str1.append(String.valueOf(t));
C.v=0;
C.z=0;
break;
}
C.bool=false;
C.flag=5;
C.m=0;
C.z=0;
}
}
}
catch(Exception ee)
{
ee.printStackTrace();
}
}
}