In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to realize the addition calculator of the java graphical interface". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to realize the addition calculator of the java graphical interface" can help you solve the problem.
Step one:
First of all, we have to figure out what we're going to do. If you use an addition calculator, it will look like this after it is made.
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, users can put the values they want to add in boxes 1 and 2. Then we click the "calculate" button below, and the results will be displayed in box 3.
Step 2:
Analyze the interface, what should we do. As you can see from the image above, the interface contains seven controls: title, box 1, box 2, box 3, plus sign, equals sign, and a "calculate" button.
At this point, we should already have the corresponding countermeasures in mind:
Box 1, box 2 is used to receive the value dynamically entered by the user, and box 3 is used to display the results of the calculation; we can use three text boxes, in which box 3 does not require user input, and we can set the user non-editable attribute to it.
Title, plus sign and equal sign, these three controls remain the same, we choose the static label to complete.
The "calculate" button, of course, has to be done with a button control, but it's no use if it's just a mediocre button. We need to add an event response to this button so that when we click this button, the result of the calculation will appear in box 3.
The use of the control is selected. We also need analysis, and to implement the interface shown in the figure above, we need several modules.
Two modules:
1) the title is a module.
2) Box 1, Box 2, Box 3, plus sign, equals sign is a module.
This is because box 1, box 2, box 3, plus sign, equals sign are on the same horizontal line, while the title is on another horizontal line. So we need to divide it into two modules. It may feel a little superfluous, but programming is like this, rigid and flexible, the method is fixed, but can achieve a variety of effects.
After divided into two horizontal modules, the matter is not over, we also need to combine the two aquatic modules into a vertical module (arranged from top to bottom).
We will set the position of the button at will, so we will not use it as a module here.
In this way, the preparatory work is complete. Let's start writing code.
Code implementation:
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 ("calculation"); MyWin () {/ * * layout design * * / setTitle ("addition Calculator") SetLayout (null); setBounds (680,300,512,380); setVisible (true); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel myTitle = new JPanel (); myTitle.add (new JLabel ("addition Calculator-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) / * * event listening * * / 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:
1-5 lines, import the package you need to use.
To implement the window interface, we first need a window.
7 ~ 70 lines, defines a window class and implements 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 own event listener).
8-11 lines, text boxes and button controls as member properties of the class, because their state needs to be saved when the program is running, if it is defined in a function, when the function ends, the life cycle of the control will end, and the value in the state of the control can not be saved, we will not be able to complete other operations in the later order. The text box control with parameters is defined to indicate that the text box control can display up to 8-bit characters.
13-63 lines to implement the constructor of the window class.
15 lines to set the window title.
16 lines, set the window layout to the null layout, that is, we need to set the location of each control.
Line 17, setting the initial position and window size when the window program appears on the screen.
Line 18, the settings 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 the fork in the upper right corner of the window is clicked. I set it to EXIT_ON_CLOSE here, that is, click and exit the program.
Line 21, define a myTitle panel (that is, the first module above) to hold our title.
Line 22, adding our title text tag 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 property of the text box that accepts the result as non-editable, 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 line 33 and 34, I set two supports to control the distance between the two modules.
Line 40 to add a box with two panels to the current window.
Line 42, set the location and size of the calculate button in the window.
Line 43 to add the button to the current window.
Lines 46-62, register the listener for the "calculate" button and implement the event response function (convert the strings in the two text boxes into shaping data for addition calculation, and output the results in the text box that receives the results).
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 effect:
This is the end of the introduction of "how to implement the addition Calculator for java graphical Interface". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.