In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to achieve a simple calculator interface with java swing". In daily operation, I believe many people have doubts about how to achieve a simple calculator interface with java swing. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve a simple calculator interface with java swing". Next, please follow the editor to study!
Knowledge applied:
1. The use of common top-level container JFrame classes
two。 The use of commonly used intermediate container JPanel class
3. The implementation of single-line text box the use of JTextField class
4. The implementation class of the button the use of the JButton class
5. Use of Border layout Manager
6. Use of Grid layout Manager
Basic ideas:
1. Create 1 window F1
two。 Create 1 text box T1
3. Create the inner panel p3 and save the text box T1
4. Create 16 buttons
5. Create inner panel p2, set to grid layout, to save 16 buttons
6. Create the outer panel p1, set it as the border layout, save p3 in the north and p2 in the middle.
Here is the specific code implementation, which can be run directly
Import javax.swing.JFrame; / / Top-level container (frame) import javax.swing.JPanel; / / the most commonly used panel import javax.swing.JTextField; / / single-line text box implementation class import javax.swing.JButton; / / Button import javax.swing.border.EmptyBorder; / / you need to use the EmptyBorder class to set the border property import javax.swing.SwingConstants of the panel / / SwingConstants is a collection of constants commonly used to locate (position) or orient (orient) components on the screen import java.awt.*; / / layout manager class where public class calculator {public static void main (String [] args) {JFrame F1 = new JFrame ("calculator"); / / create a top-level container (window) f1.setSize (250,300) / / set the window size JPanel p1 = new JPanel (new BorderLayout (5p5)); / / create a panel / / inside the new BorderLayout (5Power5) is to set the layout of the panel (border layout) / / where the first 5 represents the spacing between the upper and lower controls, and the second represents the spacing between the left and right controls (new EmptyBorder). / / set the boundary distance to the border boundary / / the setBorder function of the panel, which is used to set the inward contraction width of the edge of the panel / / the EmtyBorder inside indicates that the upper, left, lower and right sides of the panel shrink inward by 5 pixels JTextField T1 = new JTextField (); / / create a single-line text box t1.setColumns (10) / / the number of columns used to set the text box Columns column t1.setHorizontalAlignment (SwingConstants.RIGHT); / / set the text box right alignment / / Horizontal horizontal Alignment alignment remember two words JPanel p2 = new JPanel (new GridLayout) / / create a second panel / / in which the layout is set to grid layout, (the first 4 represents a row of 4 controls) / / the second 4 represents a column of 4 controls, the first 5 indicates an interval of 5 between upper and lower controls, and the second 5 indicates an interval of 5 (pixels) JButton b1 = new JButton ("7") / / create 16 buttons JButton b2 = new JButton ("8"); JButton b3 = new JButton ("9"); JButton b4 = new JButton ("/"); JButton b5 = new JButton ("4"); JButton b6 = new JButton ("5"); JButton b7 = new JButton ("6"); JButton b8 = new JButton ("*") JButton B9 = new JButton ("1"); JButton B10 = new JButton ("2"); JButton B11 = new JButton ("3"); JButton B12 = new JButton ("-"); JButton B13 = new JButton ("0"); JButton B14 = new JButton ("."); JButton B15 = new JButton ("="); JButton B16 = new JButton ("+") P2.add (b1); / / add all 16 buttons to panel p2 of the grid layout: p2.add (b2); p2.add (b3); p2.add (b4); p2.add (b5); p2.add (b6); p2.add (b7); p2.add (b8); p2.add (b9) P2.add (b10); p2.add (b11); p2.add (b12); p2.add (b13); p2.add (b14); p2.add (b15); p2.add (b16); JPanel p3 = new JPanel (); / / create a third panel to save the previous text box T1 p3.add (T1) / / add text box T1 to panel p3 p1.add (p3MagneBorderLayout.North); / / add p3 to the northern p1.add of p1 as the border layout panel (p2BorderLayout.CENTER); / / add p2 to the middle f1.add (p1) of the panel; / / add p1 panel to window f1.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) / / set f1.setVisible (true) when closed; / / set visible}}
Running result:
At this point, the study on "how to implement a simple calculator interface with java swing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.