问题描述
- 程序问题出在小数点构造方法及调用,输入负数出不来,其他的正确
-
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()
{ 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));
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");
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();
if(command.equals("0"))||
command.equals("1"))||
command.equals("2"))||
command.equals("3"))||
command.equals("4"))||
command.equals("5"))||
command.equals("6"))||
command.equals("7"))||
command.equals("8"))||
command.equals("9"))||
command.equals("."))
{ int number=Integer.parseInt(command);
calc.numberPressed(number); }
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();
redisplay();
}
private void redisplay()
{ display.setText(""+calc.getDisplayValue()); }
}//要用到的类
public class Calculator
{ private CalcEngine engine;
private UserGUI gui;
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
{ private double number;double result;
final double pi=3.1415926535;
char flag;
boolean isNumber;
public CalcEngine()
{ number=0;
result=0;
flag=' ';
isNumber=true;
}
public double getDisplayValue()
{ return number; }
public void numberPressed(double number)
{ if(isNumber&&this.number!=0)
{ double str1=Double.toDouble(number);
double str2=Double.toDouble(this.number);
this.number=Double.parseDouble(str2+str1);
}
else
{ this.number=number; }
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 dian()
{ equals();
this.flag='.'; }
public double equals()
{ switch(flag)
{ case '+': result=result+number; break;
case '-': result=result-number; break;
case '*': result=result*number; break;
case '/': result=result/number; break;
case '√': result=Math.sqrt( number); break;
case '%': result=result%number; break;
case 's': result= Math.sin(number*pi/180);
break;
case '.': number=(double) number; break; //这里有错
case ' ': result= number; break;
}
return result;
flag=' ';
isNumber=false;
}
public void clear()
{ number=0; result=0; }
public String getTitle()
{ return " calclation example"; }
}