Import Java.awt.BorderLayout;
Import Java.awt.Color;
Import Java.awt.GridLayout;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JTextField;
/**
- A calculator, similar to the Standard Edition features and interface of the Windows Accessory's own calculator. However, keyboard operation is not supported.
*/
public class Calculator extends JFrame implements ActionListener {
The display name of the key on the/** calculator */private final string[] keys = {"7", "8", "9", "/", "sqrt", "4", "5", "6", "*", "%", "1", "2", "3", "-", "1/x", "0", "+/-", ".", "+", "="}; The display name of the function key on the/** calculator */private final string[] COMMAND = {"Backspace", "CE", "C"}; /** Calculator to the left of the display name of M */private final string[] m = {"", "MC", "MR", "MS", "m+"}; /** Calculator Key button */private JButton keys[] = new Jbutton[keys.length]; /** the function button on the calculator */private JButton commands[] = new Jbutton[command.length]; /** Calculator to the left of the M button */private JButton m[] = new Jbutton[m.length]; /** Calculation result text box */private JTextField Resulttext = new JTextField ("0"); Flag whether the user presses the first number of the entire expression, or the first number after the operator is private boolean firstdigit = true; The intermediate result of the calculation. Private double resultnum = 0.0; The operator of the current operation is private String operator = "="; Whether the operation is legal private Boolean operatevalidflag = true; /** * constructor */public Calculator () {super (); Initialize Calculator init (); Set the background color of the calculator this.setbackground (Color.light_gray); This.settitle ("calculator"); On the screen (500, 300) sitThe indicator shows the calculator this.setlocation (500, 300); Don't change the size of the calculator this.setresizable (false); Make the components of the calculator fit This.pack (); }/** * Initialize calculator */private void init () {///text box contents are in right alignment resulttext.sethorizontalalignment (jtextfield.right); modifying result text box Resulttext.seteditable (false) is not allowed; Sets the text box background color to white resulttext.setbackground (color.white); Initialize the button on the Calculator key, place the key in an artboard JPanel Calckeyspanel = new JPanel (); With Grid layout, 4 rows, 5 columns of grid, the horizontal direction between the grid is 3 pixels, the vertical direction interval is 3 pixels calckeyspanel.setlayout (New GridLayout (4, 5, 3, 3)); for (int i = 0; i < keys.length; i++) {keys[i] = new JButton (keys[i]); Calckeyspanel.add (Keys[i]); Keys[i].setforeground (Color.Blue); }//The operator key is marked in red, other keys are indicated by Blue Keys[3].setforeground (color.red); Keys[8].setforeground (color.red); Keys[13].setforeground (color.red); Keys[18].setforeground (color.red); Keys[19].setforeground (color.red); The initialization function keys are marked in red. Place function keys within an artboard JPanel Commandspanel = new JpanEl (); With grid layout, 1 rows, 3 columns of grid, the horizontal direction between the grid is 3 pixels, the vertical direction interval is 3 pixels commandspanel.setlayout (New GridLayout (1, 3, 3, 3)); for (int i = 0; i < command.length; i++) {commands[i] = new JButton (Command[i]); Commandspanel.add (Commands[i]); Commands[i].setforeground (color.red); }//Initialize M key, mark with red, put M key in an artboard JPanel Calmspanel = new JPanel (); With the Grid layout manager, 5 rows, 1 columns of grid, the horizontal direction between the grid is 3 pixels, the vertical direction interval is 3 pixels calmspanel.setlayout (New GridLayout (5, 1, 3, 3)); for (int i = 0; i < m.length; i++) {M[i] = new JButton (M[i]); Calmspanel.add (M[i]); M[i].setforeground (color.red); }//Under the overall layout of the calculator, place the Calckeys and command artboard in the middle of the calculator,//Place the text box in the north, and place the calms artboard in the west of the calculator. Create a new large artboard, place the command and Calckeys artboard created above within the artboard JPanel Panel1 = new JPanel (); The artboard uses the boundary layout manager, where the horizontal and vertical intervals between components in the artboard are 3 pixels panel1.setlayout (New BorderLayout (3, 3)); Panel1.add ("North", Commandspanel); Panel1.add ("Center", Calckeyspanel); Create an artboard to place a text box JPanel top= new JPanel (); Top.setlayout (New BorderLayout ()); Top.add ("Center", Resulttext); Overall layout Getcontentpane (). setlayout (New BorderLayout (3, 5)); Getcontentpane (). Add ("North", top); Getcontentpane (). Add ("Center", Panel1); Getcontentpane (). Add ("West", Calmspanel); Add event listeners for each button//both use the same event listener, which is this object. Declarations in this class have implements ActionListener for (int i = 0; i < keys.length; i++) {Keys[i].addactionlistener (this); } for (int i = 0; i < command.length; i++) {Commands[i].addactionlistener (this); } for (int i = 0; i < m.length; i++) {M[i].addactionlistener (this); } }
Java Advanced Program Design Final Job "3"