5. Write a calculator program
"Red part for Answer"
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Public class Calculator Extendsjframe implements ActionListener {
JTextField result; Displays the numbers and results of the input
int calculate_type = 0;//0, no operations; 1, 2, 3, 4 represent subtraction respectively
Calculator () {
JPanel JP;
JButton JB;
JP = new JPanel ();
jp.setlayout (New BorderLayout ());
//Create a text bar, do not allow editing, add to the top of the window
Result = new JTextField ();
result.seteditable (false);
Jp.add (Result,borderlayout.north);
///Window Center Add Number button, empty, backspace, decimal button
//Because borderlayout divides the windows into five areas in the top and bottom, each region can only add one component
//So add Keypanel to the middle area and add a number button to the Keypanel to implement the container nesting
JPanel Keypanel = new JPanel ();
keypanel.setlayout (New GridLayout (5,3));
for (int i=1;i<=9;i++) {
JB = new JButton ("" +i);
Jb.addactionlistener (this);
Keypanel.add (JB);
}
JB = new JButton ("0");
Keypanel.add (JB);
Jb.addactionlistener (this);
JB = new JButton ("Empty");
Keypanel.add (JB);
Jb.addactionlistener (this);
JB = new JButton ("backspace");
Keypanel.add (JB);
Jb.addactionlistener (this);
JB = new JButton (".");
Jb.addactionlistener (this);
Keypanel.add (JB);
JB = new JButton ("=");
Jb.addactionlistener (this);
Keypanel.add (JB);
Jp.add (keypanel,borderlayout.center);
JPanel Operatorpanel = new JPanel ();
operatorpanel.setlayout (Newgridlayout (4,1));
JB = new JButton ("+");
Jb.addactionlistener (this);
Operatorpanel.add (JB);
JB = new JButton ("-");
Jb.addactionlistener (this);
Operatorpanel.add (JB);
JB = new JButton ("*");
Jb.addactionlistener (this);
Operatorpanel.add (JB);
JB = new JButton ("/");
Jb.addactionlistener (this);
Operatorpanel.add (JB);
Jp.add (operatorpanel,borderlayout.east);
//Add JPanel container to form
Setcontentpane (JP);
//Set the title, size, visibility, and close action of the form
settitle ("calculator");
setSize (340,260);
setvisible (true);
//Settings window closes, program exits
setdefaultcloseoperation (jframe.exit_on_close);
}
Public void actionperformed (ActionEvent e) {
String cmd = E.getactioncommand ();
String C = result.gettext ();
if (cmd.equals ("Empty")) {
Result.settext ("");
}
Else if (Cmd.compareto ("0") >=0 && Cmd.compareto ("9") <= 0) {
Result.settext (c + cmd);
}
Else if (cmd.equals ("+") | | cmd.equals ("-") | | cmd.equals ("*") | | cmd.equals ("/")) {
if (Calculate_type = = 0) {//The first click operator since the last calculation is completed
Result.settext (c + cmd);
Judgecalculatetype (cmd);//Judge the type of calculation
}
else{//Since the last time the calculation is complete, the second click Operator, triggering the calculation
calculate ();
Result.settext (Result.gettext () + cmd);
Judgecalculatetype (cmd);//Judge the type of calculation
}
}
Else if (cmd.equals ("=")) {//click = for calculation
try{
calculate ();
}
catch (Exception exp) {
Joptionpane.showmessagedialog (This, "Input error, please check output or restart program");
}
}
}
//Judge the type of calculation
private void Judgecalculatetype (String cmd) {
if (cmd.equals ("+"))
calculate_type = 1;//record operation type
Else if (cmd.equals ("-"))
calculate_type = 2;
Else if (cmd.equals ("*"))
calculate_type = 3;
Else
calculate_type = 4;
}
//Complete the calculation function
private void Calculate () {
double rt,op1,op2;
String C = result.gettext ();
int pos;
char ops[] = new char[]{' + ', '-', ' * ', '/'};
//Find operator Location
pos = C.indexof (ops[calculate_type-1]);
//The operator position is the dividing point, separating out two count to be counted
OP1 = double.parsedouble (c.substring (0, POS));
OP2 = double.parsedouble (c.substring (pos + 1));
switch (calculate_type) {
Case 1:
RT = Op1 + op2;
Break ;
Case 2:
RT = OP1-OP2;
Break ;
Case 3:
RT = OP1 * OP2;
Break ;
Case 4:
RT = OP1/OP2;
Break ;
Default:
rt = 0;
}
Result.settext ("" + RT); Show calculation results
calculate_type = 0;//prepare to record the next calculation type
}
Public static void Main (string[] args) {
new Calculator ();
}
}