Import java.awt.event. *;
Import javax.swing. *;
Public class calculator implements ActionListener{
Private double data 1=0.0, data2 = 0.0.
Private string t _ content
Boolean number = false;
//key is used to indicate the button pressed by the user, that is, addition, subtraction, multiplication and division.
//key is 0 for+,1 for-,2 for *, and/
Shortcut key =-1;
The value of //pkey is-1~5.
//pkey is-1, indicating that the = key is pressed for the first time, and pkey is 5, indicating that it is not the first time to press the = key.
//When //pkey is 0, 1, 2,3, it means+,-,*,/
short pkey =- 1;
Frame frame = new frame ("calculator"); //Define a form titled Calculator
TextField TextField = new TextField(30); //Define a computer
//Define the Backspace, ce and c0 buttons.
Button Backspace = New button ("Backspace");
Button ce = new button ("CE");
Button c0 = New button ("c");
//Define the panel, in which the backspace, ce, c0 buttons and the computer blue textfeild are placed in the panel p 1.
//The remaining buttons are placed on panel p2.
Panel p 1 = new Panel();
Panel p2 = new Panel();
//Define the button array on the interface, that is, all buttons except Backspace, ce and C0 buttons.
String name []={"7 ","8 ","9 ","0 ","sqrt ","4 ","5 ","6 ","* ",","1", "2", "3", "
Button bb[]= new button [names.length];
Public static void main(String[] args){
Calculator cal = new calculator ();
cal . go();
}
Public void go(){
frame.setSize(300,200);
frame . set layout(new BorderLayout());
Font fonts = new font ("italics _ GB 23 12", font.plain,12); //The first page
//Set the font and layout management type of the panel p 1, and add the textfeild object to the panel.
P 1.setFont (font);
P 1.setLayout (new GridLayout(2, 1 0, 5,10));
Textfield.setFont (font);
p 1.add(textfield,null);
//backspace, ce, c0 button to register the addActionListener method.
backspace . addactionlistener(this);
ce . addactionlistener(this);
c0 . addactionlistener(this);
//Add Backspace, ce and c0 buttons to the p 1 panel.
P 1.add (backspace, empty);
p 1.add(ce,null);
p 1.add(c0,null);
//Set the layout manager type of panel p2 to GridLayout.
p2.setLayout(new GridLayout(4,5,5,5));
//Register the addActionListener method for the buttons in the button group.
for(int I = 0; I< name. length; i++){
Bb[I]= new button (name [i]);
p2 . add(bb[I]);
bb[i]。 addActionListener(this);
}
//Set the position of panel p 1 and panel p2 in the form.
//Panel p 1 is in the north direction of the form, and Panel p2 is in the south direction of the form.
frame.add(p 1,BorderLayout。 North);
frame.add(p2,BorderLayout。 South);
frame . set visible(true);
//Click the Close button to exit the system.
frame . addwindowlistener(new window adapter(){
public void window closing(window event e){
system . exit(0);
}
});
}
public void action performed(action event AE){
String s;
//Use the getActionCommand () method of the ActionEvent class to find out which button triggered the operation. //Page 2
s = AE . getactioncommand();
//When the user clicks once, a character will be deleted from the calculated column.
//Clear the characters in the calculation bar first, and then add a string other than Best.
If(s.equals ("backspace") & number) {
t _ content = textfield . gettext();
textfield . settext(" ");
for(int I = 0; I<T _ content.length ()-1; i++){
char c = t _ content . charat(I);
textfield . settext(textfield . gettext()+c);
}
}
//The user clicks the CE button, and the calculation field is empty.
if(s.equals("CE")){
textfield . settext(" ");
Number = false;
}
//The user clicked the c button.
if(s . equals(" C "){
textfield . settext(" ");
data 1 = data 2 = 0.0;
Number = false;
}
//The user clicks the sqrt button, and the negative number cannot be squared.
if(s . equals(" sqrt ")& amp; & code) {
t _ content = textfield . gettext();
if(t _ content . charat(0)= '-'){
Textfield.setText ("negative numbers cannot be squared");
Number = false;
} Otherwise {
textfield . settext(double . tostring(Java . lang . math . sqrt(double . parse double(t _ content))));
}
}
//The user clicks the 1/x countdown button, and the denominator cannot be 0 when counting down.
if(s . equals(" 1/x ")& amp; & code) {
t _ content = textfield . gettext();
If(t _ content. equals(" 0 "){// Page 3
Textfield.setText ("Zero cannot be reciprocal");
Number = false;
} Otherwise {
textfield . settext(double . tostring( 1/double . parse double(t _ content)));
}
}
//The user clicked the% button.
if(s . equals(" % " & amp; & code) {
if(data2==0){
t _ content = textfield . gettext();
textfield . settext(t _ content);
} Otherwise {
Boolean g = false;
for(int I = 0; I & lttextfield.getText (). Length (); i++)
If ('.'==textfield.getText (). charAt(i)){
G = true;
Break;
}
//If it is an even number, divide by 100.
if(g=true){
double dt EMP = double . parse double(textfield . gettext());
dt EMP = dt EMP/ 100.0;
textfield . settext(double . tostring(dt EMP));
}
//If it is an int number but divisible by 100, remove the last two zeros.
Otherwise {
if(integer . parse int(textfield . gettext())% 100 = = 0){
int itemp = integer . parse int(textfield . gettext());
itemp/= 100;
textfield . settext(integer . tostring(itemp));
}
//If it is an int number but cannot be divisible by 100, it will be treated as a Double number.
Otherwise {
double dt EMP = double . parse double(textfield . gettext());
dt EMP = dt EMP/ 100.0;
textfield . settext(double . tostring(dt EMP));
}
}
}//Page 4
}
//Handling of positive and negative numbers
if(s . equals("+/-"){
t _ content = textfield . gettext();
if(t _ content . charat(0)= '-'){
textfield . settext(" ");
String content;
for(int I = 1; I<t _ content.length (); i++){
textfield . settext(textfield . gettext()+t _ content . charat(I));
}
} Otherwise {
textfield . settext("-"+t _ content);
}
}
//The button pressed is the+button.
if(s . equals("+"){
Switch (pkey)
Case 0:
data 2+= double . parse double(textfield . gettext());
Break;
Case 1:
data 2+= double . parse double(textfield . gettext());
Break;
Case 2:
data 2+= double . parse double(textfield . gettext());
Break;
Case 3:
if(textfield.getText()。 Equal to ("0 "){
Textfield.setText ("Delimiter cannot be zero");
Number = false;
} Otherwise {
key =- 1;
data 2/= data 1;
textfield . settext(double . tostring(data 2));
Break;
}
Default value:
data 2 = double . parse double(textfield . gettext());
}
pkey = key = 0;
textfield . settext(" ");
}
//Page 5
//The button pressed is the-button.
if(s . equals("-"){
Switch (pkey)
Case 0:
data 2+= double . parse double(textfield . gettext());
Break;
Case 1:
data 2+= double . parse double(textfield . gettext());
Break;
Case 2:
data 2+= double . parse double(textfield . gettext());
Break;
Case 3:
if(textfield.getText()。 Equal to ("0 "){
Textfield.setText ("Delimiter cannot be zero");
Number = false;
} Otherwise {
key =- 1;
data 2/= data 1;
textfield . settext(double . tostring(data 2));
Break;
}
Default value:
data 2 = double . parse double(textfield . gettext());
}
pkey = key = 1;
textfield . settext(" ");
}
//The button pressed is the * button.
if(s . equals(" * "){
Switch (pkey)
Case 0:
data 2+= double . parse double(textfield . gettext());
Break;
Case 1:
data 2+= double . parse double(textfield . gettext());
Break;
Case 2:
data 2+= double . parse double(textfield . gettext());
Break;
Case 3:
If (textfield.gettext()。 Equals(" 0 "){// Page 6
Textfield.setText ("Delimiter cannot be zero");
Number = false;
} Otherwise {
key =- 1;
data 2/= data 1;
textfield . settext(double . tostring(data 2));
Break;
}
Default value:
data 2 = double . parse double(textfield . gettext());
}
pkey = key = 2;
textfield . settext(" ");
}
//The button pressed is the/button.
if(s . equals("/"){
Switch (pkey)
Case 0:
data 2+= double . parse double(textfield . gettext());
Break;
Case 1:
data 2+= double . parse double(textfield . gettext());
Break;
Case 2:
data 2+= double . parse double(textfield . gettext());
Break;
Case 3:
if(textfield.getText()。 Equal to ("0 "){
Textfield.setText ("Delimiter cannot be zero");
Number = false;
} Otherwise {
key =- 1;
data 2/= data 1;
textfield . settext(double . tostring(data 2));
Break;
}
Default value:
data 2 = double . parse double(textfield . gettext());
}
pkey = key = 3;
textfield . settext(" ");
}//Page 7
//The pressed button is =, so find the result.
if(s . equals(" = " & amp; & code) {
//If the = button is not pressed for the first time,
if(pkey==5){
Switch (pkey)
Case 0:
data 2+= data 1;
textfield . settext(double . tostring(data 2));
Break;
Case 1:
data 2-= data 1;
textfield . settext(double . tostring(data 2));
Break;
Case 2:
data 2 * = data 1;
textfield . settext(double . tostring(data 2));
Break;
Case 3:
if(textfield.getText()。 Equal to ("0 "){
Textfield.setText ("Delimiter cannot be zero");
Number = false;
} Otherwise {
data 2/= data 1;
textfield . settext(double . tostring(data 2));
Break;
}
}
}
//If the = button is pressed for the first time,
Otherwise {
data 1 = double . parse double(textfield . gettext());
Switch (key)
Case 0:
key =- 1;
data 2+= double . parse double(textfield . gettext());
textfield . settext(double . tostring(data 2));
Break;
Case 1:
key =- 1;
data 2-= double . parse double(textfield . gettext());
textfield . settext(double . tostring(data 2)); //Page 8
Break;
Case 2:
key =- 1;
data 2 * = double . parse double(textfield . gettext());
textfield . settext(double . tostring(data 2));
Break;
Case 3:
if(textfield.getText()。 Equal to ("0 "){
Textfield.setText ("Delimiter cannot be zero");
Number = false;
} Otherwise {
key =- 1;
data 2/= double . parse double(textfield . gettext());
textfield . settext(double . tostring(data 2));
Break;
}
}
}
pkey = 5;
}
//Numeric button when the button is pressed
Otherwise {
Switch (s.charAt(0)){) (
Case "0":
Case "1":
Case "2":
Case "3":
Case "4":
Case "5":
Case "6":
Case "7":
Case "8":
Case "9":
Number = true;
textfield . settext(textfield . gettext()+s);
Break;
//processing ...
Case ".":
t _ content = textfield . gettext();
Boolean g = false; //Page 9
//Check the data in the computer column.
//If there is a decimal point, it will not be added when the user clicks the button.
for(int I = 0; I<t _ content.length (); i++){
if(t_content.charAt(i)== ' . '){
G = true;
Break;
}
}
//There is already a decimal point in the data.
If (g = = true)
Break;
//There is no decimal point in the data. The user clicks the decimal point and adds a decimal point.
If (g = = false)
textfield.setText(t_content+" . ");
}
}
}
Private string integer (string string) (
// TODO automatically generated method stub
Returns null
}
}