Use java to create a simple calculator-general Linux technology-Linux programming and kernel information. The following is a detailed description. There are two classes in total. It only completes the +,-, X, and merge operations.
The GUI only uses AWT, which is easy to understand.
Calculator. java
Public class Calculator {
Private String result = "0 ";
Private int op = 0, add = 1, sub = 2, mul = 3, div = 4;
Private double stringToDouble (String x ){
Double y = Double. parseDouble (x );
Return y;
}
Private void operate (String x ){
Double x1 = stringToDouble (x );
Double y = stringToDouble (result );
Switch (op ){
Case 0:
Result = x;
Break;
Case 1:
Result = String. valueOf (y + x1 );
Break;
Case 2:
Result = String. valueOf (y-x1 );
Break;
Case 3:
Result = String. valueOf (y * x1 );
Break;
Case 4:
If (x1! = 0 ){
Result = String. valueOf (y/x1 );
} Else {
Result = "The divisor can't be zero! ";
}
Break;
}
}
Public String opAdd (String x ){
Operate (x );
Op = add;
Return result;
}
Public String opSubtract (String x ){
Operate (x );
Op = sub;
Return result;
}
Public String opMultiply (String x ){
Operate (x );
Op = mul;
Return result;
}
Public String opDivide (String x ){
Operate (x );
Op = div;
Return result;
}
Public String opEquals (String x ){
Operate (x );
Op = 0;
Return result;
}
Public void opClean (){
Op = 0;
Result = "0 ";
}
}
Public CalculatorGUI (){
F = new Frame ("Calculator ");
P1 = new Panel ();
P2 = new Panel ();
B0 = new Button ("0 ");
B1 = new Button ("1 ");
B2 = new Button ("2 ");
B3 = new Button ("3 ");
B4 = new Button ("4 ");
B5 = new Button ("5 ");
B6 = new Button ("6 ");
B7 = new Button ("7 ");
B8 = new Button ("8 ");
B9 = new Button ("9 ");
BPoint = new Button (".");
BAdd = new Button ("+ ");
BDec = new Button ("-");
BMul = new Button ("*");
BDiv = new Button ("/");
BCal = new Button ("= ");
Tf = new TextField (25 );
Tf. setEditable (false );
}
Public void launchFrame (){
F. setSize (220,160 );
F. setResizable (false );
F. addWindowListener (new myWindowListener ());
P1.setLayout (new FlowLayout (FlowLayout. CENTER ));
P1.add (tf );
F. add (p1, BorderLayout. NORTH );
P2.setLayout (new GridLayout (4, 4 ));
B0.addActionListener (new setLabelText_ActionListener ());
B1.addActionListener (new setLabelText_ActionListener ());
B2.addActionListener (new setLabelText_ActionListener ());
B3.addActionListener (new setLabelText_ActionListener ());
B4.addActionListener (new setLabelText_ActionListener ());
B5.addActionListener (new setLabelText_ActionListener ());
B6.addActionListener (new setLabelText_ActionListener ());
B7.addActionListener (new setLabelText_ActionListener ());
B8.addActionListener (new setLabelText_ActionListener ());
B9.addActionListener (new setLabelText_ActionListener ());
BPoint. addActionListener (new setLabelText_ActionListener ());
BAdd. addActionListener (new setOperator_ActionListener ());
BDec. addActionListener (new setOperator_ActionListener ());
BMul. addActionListener (new setOperator_ActionListener ());
BDiv. addActionListener (new setOperator_ActionListener ());
BCal. addActionListener (new setOperator_ActionListener ());
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.