简单计算器..
自己边学边弄的程序...在代码和结构思想上有什么可以改正的请告诉下..import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class cal extends JFrame implements ActionListener
{
private JButton[] buttons;
private int i,k;
private boolean point;
private boolean yunsuan;
private int m;
private double j,p;
private String str;
private JTextField text;
public cal() {
super("MZP计算器");
this.setSize(220,180);
this.setLocation(400,300);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
i=0;
k=1;
point=false;
yunsuan=false;
buttons=new JButton[20];
text=new JTextField("0");
text.setHorizontalAlignment(JTextField.RIGHT);
text.setEditable(false);
this.add(text,"North");
JPanel panel=new JPanel(new GridLayout(5,4));
this.add(panel,"Center");
buttons[0]=new JButton("sqrt");
buttons[1]=new JButton("+/-");
buttons[2]=new JButton("CE");
buttons[3]=new JButton("C");
buttons[4]=new JButton("7");
buttons[5]=new JButton("8");
buttons[6]=new JButton("9");
buttons[7]=new JButton("/");
buttons[8]=new JButton("4");
buttons[9]=new JButton("5");
buttons[10]=new JButton("6");
buttons[11]=new JButton("*");
buttons[12]=new JButton("1");
buttons[13]=new JButton("2");
buttons[14]=new JButton("3");
buttons[15]=new JButton("-");
buttons[16]=new JButton("0");
buttons[17]=new JButton(".");
buttons[18]=new JButton("=");
buttons[19]=new JButton("+");
for(int j=0;j<20;j++)
{
panel.add(buttons[j]);
buttons[j].addActionListener(this);
}
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttons[0])
{
text.setText(""+Math.sqrt(Double.parseDouble(text.getText())));
point=true;
}
if(e.getSource()==buttons[1])
{
if(i%2==0)
{
text.setText("-"+text.getText());
i=1;
}
else
{
str=text.getText();
str=str.substring(1);
text.setText(str);
i=0;
}
}
if(e.getSource()==buttons[2]||e.getSource()==buttons[3])
{
text.setText("0");
}
if(e.getSource()==buttons[4]||e.getSource()==buttons[5]||e.getSource()==buttons[6]||e.getSource()==buttons[8]||e.getSource()==buttons[9]||e.getSource()==buttons[10]||
e.getSource()==buttons[12]||e.getSource()==buttons[13]||e.getSource()==buttons[14]||e.getSource()==buttons[16])
{
str=text.getText()+e.getActionCommand();
if(str.charAt(0)=='0'&&str.charAt(1)!='.'&&str.length()>2)
str=str.substring(1);
if(!yunsuan)
{
str=e.getActionCommand();
yunsuan=true;
}
text.setText(str);
}
if(e.getSource()==buttons[7]||e.getSource()==buttons[11]||e.getSource()==buttons[15]||e.getSource()==buttons[18]||e.getSource()==buttons[19])
{
if(k==1)
{
yunsuan=true;
j=Double.parseDouble(text.getText());
if(e.getSource()==buttons[7])
m=0;
if(e.getSource()==buttons[11])
m=1;
if(e.getSource()==buttons[15])
m=2;
if(e.getSource()==buttons[19])
m=3;
text.setText(" ");
k++;
}
else
{
str=text.getText();
if(str.equals(" "))
p=j;
else
p=Double.parseDouble(text.getText());
switch(m)
{
case 0:j=j/p;break;
case 1:j=j*p;break;
case 2:j=j-p;break;
case 3:j=j+p;break;
}
text.setText(""+j);
k=1;
point=true;
yunsuan=false;
}
}
if(e.getSource()==buttons[17])
if(!point)
{
text.setText(text.getText()+".");
point=true;
}
}
public static void main(String args[])
{
new cal();
}
}