[求助]一个小计算器程序(大家改进下)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class CounterStructure
{
JButton bt[],backspace,ce,c;
double a,b;
String opr,res="0";
JTextField myJTextField;
CounterStructure()
{
JFrame myJFrame=new JFrame("gxppa创建的计算器");
myJFrame.setLayout(new GridLayout(3,1));
myJTextField=new JTextField("0");
myJFrame.add(myJTextField);
JPanel myJPanel2=new JPanel();
GridLayout gr=new GridLayout(3,4,10,10);
myJPanel2.setLayout(gr);
JPanel empty1=new JPanel();
JTextField myname=new JTextField("gxppa制作的计算器,功能有待改进");
myname.setEditable(false);
empty1.add(myname);
JPanel empty2=new JPanel();
JTextField demo=new JTextField("backspace,ce ,c暂时都定为置零操作,每次的结果如果不用须置零");
demo.setEditable(false);
empty2.add(demo);
myJPanel2.add(empty1);
myJPanel2.add(empty2);
JPanel myJPanel3=new JPanel();
myJPanel2.add(myJPanel3);
myJPanel3.setLayout(new GridLayout(1,4));
JPanel empty3=new JPanel();
myJPanel3.add(empty3);
backspace=new JButton("Backspace");
ce=new JButton("CE");
c=new JButton("C");
backspace.addActionListener(new MyJButtonAction());
ce.addActionListener(new MyJButtonAction());
c.addActionListener(new MyJButtonAction());
myJPanel3.add(backspace);
myJPanel3.add(ce);
myJPanel3.add(c);
myJFrame.add(myJPanel2);
JPanel myJPanel=new JPanel();
String str[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
myJPanel.setLayout(new GridLayout(4,5,4,5));
bt=new JButton[str.length];
for(int i=0;i<str.length;i++)
{
bt[i]=new JButton(str[i]);
myJPanel.add(bt[i]);
bt[i].addActionListener(new MyJButtonAction());
}
myJFrame.add(myJPanel);
myJFrame.setSize(400,300);
myJFrame.setVisible(true);
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class MyJButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt[0])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"7");
}
else
{
myJTextField.setText("7");
}
}
if(e.getSource()==bt[1])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"8");
}
else
{
myJTextField.setText("8");
}
}
if(e.getSource()==bt[2])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"9");
}
else
{
myJTextField.setText("9");
}
}
if(e.getSource()==bt[3])
{
a=Double.parseDouble(myJTextField.getText());
opr="/";
myJTextField.setText("/");
}
if(e.getSource()==bt[4])
{
a=Double.parseDouble(myJTextField.getText());
a=Math.sqrt(a);
myJTextField.setText(""+a);
}
if(e.getSource()==bt[5])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"4");
}
else
{
myJTextField.setText("4");
}
}
if(e.getSource()==bt[6])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"5");
}
else
{
myJTextField.setText("5");
}
}
if(e.getSource()==bt[7])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"6");
}
else
{
myJTextField.setText("6");
}
}
if(e.getSource()==bt[8])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("*");
opr="*";
}
if(e.getSource()==bt[9])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("%");
opr="%";
}
if(e.getSource()==bt[10])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"1");
}
else
{
myJTextField.setText("1");
}
}
if(e.getSource()==bt[11])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"2");
}
else
{
myJTextField.setText("2");
}
}
if(e.getSource()==bt[12])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"3");
}
else
{
myJTextField.setText("3");
}
}
if(e.getSource()==bt[13])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("-");
opr="-";
}
if(e.getSource()==bt[14])
{
a=Double.parseDouble(myJTextField.getText());
if(a==0)
{
myJTextField.setText("0不能求倒数");
}
else
{
myJTextField.setText(""+1/a);
}
}
if(e.getSource()==bt[15])
{
myJTextField.setText("0");
}
if(e.getSource()==bt[16])
{
a=Double.parseDouble(myJTextField.getText());
if(a==0)
{
myJTextField.setText(""+a);
}
else
{
a=-a;
myJTextField.setText(""+a);
}
}
if(e.getSource()==bt[17])
{
res=myJTextField.getText()+".";
myJTextField.setText(res);
}
if(e.getSource()==bt[18])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("+");
opr="+";
}
if(e.getSource()==bt[19])
{
b=Double.parseDouble(myJTextField.getText());
if(opr=="/")
{
if(b==0)
{
myJTextField.setText("0不能做被除数");
}
else
{
a=a/b;
myJTextField.setText(""+a);
}
}
if(opr=="%")
{
a=a%b;
myJTextField.setText(""+a);
}
if(opr=="-")
{
a=a-b;
myJTextField.setText(""+a);
}
if(opr=="*")
{
a=a*b;
myJTextField.setText(""+a);
}
if(opr=="+")
{
a=a+b;
myJTextField.setText(""+a);
}
}
if(e.getSource()==backspace)
{
myJTextField.setText("0");
}
if(e.getSource()==ce)
{
myJTextField.setText("0");
}
if(e.getSource()==c)
{
myJTextField.setText("0");
}
}
}
}
class counter
{
public static void main(String[] args)
{
CounterStructure CS=new CounterStructure();
}
}
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class CounterStructure
{
JButton bt[],backspace,ce,c;
double a,b;
String opr,res="0";
JTextField myJTextField;
CounterStructure()
{
JFrame myJFrame=new JFrame("gxppa创建的计算器");
myJFrame.setLayout(new GridLayout(3,1));
myJTextField=new JTextField("0");
myJFrame.add(myJTextField);
JPanel myJPanel2=new JPanel();
GridLayout gr=new GridLayout(3,4,10,10);
myJPanel2.setLayout(gr);
JPanel empty1=new JPanel();
JTextField myname=new JTextField("gxppa制作的计算器,功能有待改进");
myname.setEditable(false);
empty1.add(myname);
JPanel empty2=new JPanel();
JTextField demo=new JTextField("backspace,ce ,c暂时都定为置零操作,每次的结果如果不用须置零");
demo.setEditable(false);
empty2.add(demo);
myJPanel2.add(empty1);
myJPanel2.add(empty2);
JPanel myJPanel3=new JPanel();
myJPanel2.add(myJPanel3);
myJPanel3.setLayout(new GridLayout(1,4));
JPanel empty3=new JPanel();
myJPanel3.add(empty3);
backspace=new JButton("Backspace");
ce=new JButton("CE");
c=new JButton("C");
backspace.addActionListener(new MyJButtonAction());
ce.addActionListener(new MyJButtonAction());
c.addActionListener(new MyJButtonAction());
myJPanel3.add(backspace);
myJPanel3.add(ce);
myJPanel3.add(c);
myJFrame.add(myJPanel2);
JPanel myJPanel=new JPanel();
String str[]={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
myJPanel.setLayout(new GridLayout(4,5,4,5));
bt=new JButton[str.length];
for(int i=0;i<str.length;i++)
{
bt[i]=new JButton(str[i]);
myJPanel.add(bt[i]);
bt[i].addActionListener(new MyJButtonAction());
}
myJFrame.add(myJPanel);
myJFrame.setSize(400,300);
myJFrame.setVisible(true);
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class MyJButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt[0])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"7");
}
else
{
myJTextField.setText("7");
}
}
if(e.getSource()==bt[1])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"8");
}
else
{
myJTextField.setText("8");
}
}
if(e.getSource()==bt[2])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"9");
}
else
{
myJTextField.setText("9");
}
}
if(e.getSource()==bt[3])
{
a=Double.parseDouble(myJTextField.getText());
opr="/";
myJTextField.setText("/");
}
if(e.getSource()==bt[4])
{
a=Double.parseDouble(myJTextField.getText());
a=Math.sqrt(a);
myJTextField.setText(""+a);
}
if(e.getSource()==bt[5])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"4");
}
else
{
myJTextField.setText("4");
}
}
if(e.getSource()==bt[6])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"5");
}
else
{
myJTextField.setText("5");
}
}
if(e.getSource()==bt[7])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"6");
}
else
{
myJTextField.setText("6");
}
}
if(e.getSource()==bt[8])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("*");
opr="*";
}
if(e.getSource()==bt[9])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("%");
opr="%";
}
if(e.getSource()==bt[10])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"1");
}
else
{
myJTextField.setText("1");
}
}
if(e.getSource()==bt[11])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"2");
}
else
{
myJTextField.setText("2");
}
}
if(e.getSource()==bt[12])
{
res=myJTextField.getText();
if(res.indexOf('.')!=-1)
{
myJTextField.setText(res+"3");
}
else
{
myJTextField.setText("3");
}
}
if(e.getSource()==bt[13])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("-");
opr="-";
}
if(e.getSource()==bt[14])
{
a=Double.parseDouble(myJTextField.getText());
if(a==0)
{
myJTextField.setText("0不能求倒数");
}
else
{
myJTextField.setText(""+1/a);
}
}
if(e.getSource()==bt[15])
{
myJTextField.setText("0");
}
if(e.getSource()==bt[16])
{
a=Double.parseDouble(myJTextField.getText());
if(a==0)
{
myJTextField.setText(""+a);
}
else
{
a=-a;
myJTextField.setText(""+a);
}
}
if(e.getSource()==bt[17])
{
res=myJTextField.getText()+".";
myJTextField.setText(res);
}
if(e.getSource()==bt[18])
{
a=Double.parseDouble(myJTextField.getText());
myJTextField.setText("+");
opr="+";
}
if(e.getSource()==bt[19])
{
b=Double.parseDouble(myJTextField.getText());
if(opr=="/")
{
if(b==0)
{
myJTextField.setText("0不能做被除数");
}
else
{
a=a/b;
myJTextField.setText(""+a);
}
}
if(opr=="%")
{
a=a%b;
myJTextField.setText(""+a);
}
if(opr=="-")
{
a=a-b;
myJTextField.setText(""+a);
}
if(opr=="*")
{
a=a*b;
myJTextField.setText(""+a);
}
if(opr=="+")
{
a=a+b;
myJTextField.setText(""+a);
}
}
if(e.getSource()==backspace)
{
myJTextField.setText("0");
}
if(e.getSource()==ce)
{
myJTextField.setText("0");
}
if(e.getSource()==c)
{
myJTextField.setText("0");
}
}
}
}
class counter
{
public static void main(String[] args)
{
CounterStructure CS=new CounterStructure();
}
}
现在还不能进行多位数的运算,并且小数点可以多次输入,大家给看看,怎么改进。
----------------解决方案--------------------------------------------------------
无能为力,看见了头大了
----------------解决方案--------------------------------------------------------