问题描述
- 运行结果出现的问题我不知道怎么改,请会的你们帮忙一下吧,很急!
-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class UserGUI implements ActionListener
{ private CalcEngine calc;
private JFrame frame;
private JTextField display;
private JLabel status;
public UserGUI(CalcEngine engine)
{ calc=engine;
makeFrame();
frame.setVisible(true); }
public void setVisible(boolean visible)
{ frame.setVisible(visible); }
private void makeFrame()
{ setBackground(Color.lightGray);setForeground(Color.blue);
frame=new JFrame(calc.getTitle());
JPanel contentPane=(JPanel)frame.getContentPane();
contentPane.setLayout(new BorderLayout(8,8));
contentPane.setBorder(new EmptyBorder(10,10,10,10));
display=new JTextField();
contentPane.add(display,BorderLayout.NORTH);
JPanel buttonPanel =new JPanel(new GridLayout(0,5));
Button[] b=new Button[10];
for(int i=0;i<=9;i++)b[i]=new Button(Integer.toString(i));
addButton(buttonPanel,"7");
addButton(buttonPanel,"8");
addButton(buttonPanel,"9");
addButton(buttonPanel,"+");
addButton(buttonPanel,"4");
addButton(buttonPanel,"5");
addButton(buttonPanel,"6");
addButton(buttonPanel,"-");
addButton(buttonPanel,"1");
addButton(buttonPanel,"2");
addButton(buttonPanel,"3");
addButton(buttonPanel,"*");
addButton(buttonPanel,"0");
addButton(buttonPanel,".");
addButton(buttonPanel,"C");
addButton(buttonPanel,"/");
addButton(buttonPanel,"=");
addButton(buttonPanel,"sqrt");
addButton(buttonPanel,"%");
addButton(buttonPanel,"sin");
addButton(buttonPanel,"tan");
contentPane.add(buttonPanel,BorderLayout.CENTER);
frame.pack();
}private void addButton(Container panel,String buttonText)
{ JButton button=new JButton(buttonText);
button.addActionListener(this);
panel.add(button); }
public void actionPerformed(ActionEvent event)
{ String command=event.getActionCommand();
for(i=0;i<=9;i++)
if(command.equals(b[i]))||command.equals("."))
{ number1=String.parseString(command);
calc.numberPressed(number1); }
else if(command.equals("+")) calc.plus();
else if(command.equals("-")) calc.minus();
else if(command.equals("*")) calc.mul();
else if(command.equals("/")) calc.div();
else if(command.equals("sqrt")) calc.genhao();
else if(command.equals("%")) calc.quyu();
else if(command.equals("C")) calc.clear();
else if(command.equals("=")) calc.equals();
else if(command.equals("sin")) calc.sinzhi();
else if(command.equals("tan")) calc.tanzhi();
redisplay();
}
private void redisplay()
{ display.setText(""+calc.getDisplayValue()); }
}class Calculator
{ private CalcEngine engine;
private UserGUI gui;
public Button point;
public Calculator()
{ engine=new CalcEngine();
gui=new UserGUI(engine); }public void show()
{ gui.setVisible(true); }
public static void main(String a[])
{ Calculator calc=new Calculator();
calc.show(); }
}
class CalcEngine
{ public String number1,number2;
Double result;
final double pi=3.1415926535;
char flag;
boolean isNumber;
point = new Button(".");
public CalcEngine()
{ number1=' ';
number2=' ';
flag=' ';
isNumber=true;
}
public void doshuru(String s){ number1 += s; UserGUI.contentPane.setText(number1);}
public void doshuru(String s){ number2 += s; UserGUI.contentPane.setText(number2);}
public double getDisplayValue()
{ return result; }public boolean action(Event e, Object o)
{ String s = new String("");
for(int i=0;i<10;i++)
{ if(e.target==b[i]||e.target==point){ if(e.target != point)
{ s = (String)o;
doshuru(s); }
if(e.target == point){ s = (String)o;
if(number1.equals(""))
number1 += "0.";else
dodoshuru(s);}
}
}
}public void numberPressed( )
{ if(isNumber&&this.number1!=0)
{ double str1=(new Double(number1)).doubleValue();
double str2=(new Double(number2)).doubleValue();
double this.number1=Double.parseDouble(str2+str1);
}
else
{ this.number1=number1; }
isNumber=true;
}
public void plus()
{ equals(); this.flag='+'; }
public void minus()
{ equals(); this.flag='-'; }
public void mul()
{ equals(); this.flag='*'; }
public void div()
{ equals(); this.flag='/'; }
public void genhao()
{ equals(); this.flag='√'; }
public void quyu()
{ equals(); this.flag='%'; }
public void sinzhi()
{ equals(); this.flag='s'; }
public void tanzhi()
{ equals(); this.flag='t'; }
public double equals()
{ switch(flag)
{ case '+': result=new Double((str1 + str2)); break;
case '-': result=new Double((str1 - str2)); break;
case '*': result=new Double((str1 * str2)); break;
case '/': result=new Double((str1 / str2)); break;
case '√': result=new Double((sqrt(str1))); break;
case 't': result=new Double((Math.tan(str1*pi/180))); break;
case 's': result=new Double((Math.sin(str1*pi/180))); break;
case '%': result=new Double((str1 % str2)); break;case ' ': result= str1; break;
}
str1=result;
flag=' ';
isNumber=false;
}
public void clear()
{ number=0; result=0; }
public String getTitle()
{ return " calclation example"; }
}