preface
The most important thing about learning a programming language is practice. Many friends stay in the basic stage after learning the programming language, only understand a lot of theoretical knowledge, but do not know the practice. So, today we are going to make a small calculator to review the knowledge we have learned. At the same time, this is also the final homework of many college computer majors. You should try to do it and fully understand it.
start
This experiment will use Java Swing graphics components for development
Main knowledge points
- Java Swing interface programming
- Calculator operation logic implementation
I. Project creation
- Open Eclipse and select New -> Java Project from the File menu
2. Enter the Project name Calculator in the Project Name column and click Finish to create the Calculator
3. Create a package package
4. Right-click on the created project directory SRC and choose New -> Class to create a Class
5. In the New class dialog box, fill in the class name Calculator and click Finish
Second, the code part
If you are not familiar with Swing, you can go to the links below to learn the basics of getting started
c.biancheng.net/swing/
1. Write the implementation of response logic
Defines the operands and operators that are currently pressed. Result stores the result of the operation
Define status switch
Define a JButton variable to hold the keys pressed, and define a Vector to hold all the inputs
- UI Component creation
Create main forms, buttons, and text boxes
3. Set the equivalent key in the constructor, add UI components into the container, design the event handler, implement the calculation logic and write the code of the window closing event
Setting of equivalent keys (i.e. you can use the corresponding keyboard key instead of clicking on it)
Textbox alignment
UI components are added to the container
Event handler
Number key processing logic
Operators handle logic
Clear key processing logic
Equals key processing logic
Decimal point processing logic
Register listeners
The window closed
Calculation logic
Iii. Running projects
The effect is as follows
Iv. Project Summary
The practice how to use the Swing to the realization of the graphical interface programming and some calculation logic, also only realize the realization of the function of the basic calculator, interested can add a few things on their own, such as the realization of the operator precedence (requires the realization of the data structure), further improve the ability of thinking and programming.
The source code for this project has been written and packaged. CodeVald background reply calculator is available for collection.
Have a harvest of friends remember to like the collection and attention oh. Ballpoint pen.