JAVA graphical interface: addition calculator

The SWING component package of JAVA used to develop graphical interface applications is powerful and easy to use. Next, we will use it to write a simple graphical interface applet: addition calculator.

The first step:
First of all, we have to conceive what we want to do. The addition calculator will look like this after making it.
Insert picture description here

As shown in the figure above, the interface of this program has been formed in our minds: there is a title at the top of the program, which is used to explain the name of our program; functionally, the user can put the desired program in box 1 and box 2. The added value, and then we click the "Calculate" button below, and the result will be displayed in box 3.

Step 2:
Analyze the interface, what should we do. As can be seen from the above figure, the interface contains 7 controls: title, box 1, box 2, box 3, plus sign, equal sign, and a "calculate" button.
At this point, we should have the corresponding countermeasures in our minds:
Box 1, Box 2 is used to receive the value dynamically input by the user, Box 3 is used to display the result of the calculation; we can use three text boxes to complete, among them Box 3 does not require user input, we can set user non-editable attributes for it.
The title, plus sign and equal sign, these three controls are always the same, we choose static labels to complete.
The "Calculate" button, of course, has to be completed with a button control, but if it's just a plain button, it's useless. We need to add an event response for calculating addition to this button so that we can click this button When the time, the calculation result will appear in box 3.

The use of the control is selected. We also need to analyze and analyze. To achieve the interface shown in the figure above, we need several modules.

Two modules:
1) The title is one module.
2) Box 1, Box 2, Box 3, plus sign, equal sign is a module.

This is because box 1, box 2, box 3, plus sign, equal sign are on the same horizontal line, and the title is on another horizontal line. So we need to divide it into two modules. (It may feel a bit redundant, but the program design is like this, rigid and flexible, the method is fixed, but it can achieve various effects).
After being divided into two horizontal modules, the matter is not over yet, we still need to combine these two water product modules into a vertical module (arranged from top to bottom).
We will set the position of the button arbitrarily later, so we will not use it as a module here.

In this way, the preparatory work is complete. Write the code by hand.

Code:

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class MyWin extends JFrame implements ActionListener{
    
    
	JTextField rValue = new JTextField(8);
	JTextField lValue = new JTextField(8);
	JTextField result = new JTextField(8);
	JButton calcul = new JButton("计算");
	
	MyWin() {
    
    
		/**布局设计**/
		setTitle("加法计算器");
		setLayout(null);
		setBounds(680, 300, 512, 380);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel myTitle = new JPanel();
		myTitle.add(new JLabel("加法计算器 - v1.0"));
		JPanel myText = new JPanel();
		myText.add(rValue);
		myText.add(new JLabel(" + "));
		myText.add(lValue);
		myText.add(new JLabel(" = "));
		result.setEditable(false);
		result.setBackground(Color.LIGHT_GRAY);
		myText.add(result);
		
		Box myMain = Box.createVerticalBox();
		Component strut1 = Box.createVerticalStrut(20);
		Component strut2 = Box.createVerticalStrut(200);
		myMain.add(strut1);
		myMain.add(myTitle);
		myMain.add(myText);
		myMain.add(strut2);
		myMain.setBounds(0, 0, 512, 380);
		add(myMain);
		
		calcul.setBounds(200, 220, 100, 60);
		add(calcul);
		
		/**事件监听**/
		calcul.addActionListener(new ActionListener() {
    
    

			@Override
			public void actionPerformed(ActionEvent e) {
    
    
				// TODO Auto-generated method stub
				
				try {
    
    
					int rVal = Integer.valueOf(rValue.getText());
					int lVal = Integer.valueOf(lValue.getText());
					int res = rVal + lVal;
					result.setText(String.valueOf(res));
				} catch (Exception e1) {
    
    
					System.out.println(e1);
				}
			}
			
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
    
    
		// TODO Auto-generated method stub

	}
}

In the above code:
Lines 1 to 5, import the required packages.

To implement a window interface, first we need a window.
Lines 7 to 70 define a window class and implement the initialization of the control in its constructor. This window class inherits from the JFrame window class provided by JAVA, and implements the ActionListener event response interface provided by JAVA (we use this window class as our event listener).

Lines 8 to 11, use the text box and button control as the member properties of the class, because their state needs to be saved when the program is running. If you define it in a function, the life cycle of the control will also be when the function ends. At the end, the state of the control has been unable to save its value, and we cannot complete other subsequent operations. Among them, 8, 9, 10 lines of text box control are defined with parameter structure, which means that the text box control can display up to 8 characters.

Line 13 ~ 63, implement the constructor of the window class.

Line 15, set the window title.

Line 16, set the window layout to null layout, that is, we need to set the position of each control.

Line 17, set the initial position and window size when the window program appears on the screen when it is running.

At line 18, the setting window is visible. If set to false, the window is not visible. (You can't see anything).

Line 19, set the event response of the program when you click the cross in the upper right corner of the window. I set it here as EXIT_ON_CLOSE, that is, click to exit the program.

In line 21, define a myTitle panel (that is, the first module above) to store our title.
In line 22, we added our title text label to the mytitle panel.

23 ~ 30, define the myText panel (the second module) and add three text box controls in the class member properties to it. Line 28 sets the attribute of the text box that accepts the result as uneditable, and line 29 sets its color to bright gray.

In lines 32 ~ 39, we put the two panels (two modules) defined above into a vertical box (combining the two modules).
In the 33 and 34 rows, I set up two supports to control the distance between the two modules.

Line 40, add a box with two panels to the current window.

Line 42, set the position of the "Calculate" button in the window and the size of the button.
Line 43, add the button to the current window.

Lines 46 to 62, register a listener for the "Calculate" button and implement an event response function (convert the strings in the two text boxes into plastic data for addition calculation, and output the result in the text box that receives the result).

Test the program in the Test file under the same package.

public class Test {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		MyWin a = new MyWin();
	}

}

running result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/109167364