Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Swing to realize simple Calculator Interface in Java

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Swing to achieve a simple calculator interface in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Swing to achieve a simple calculator interface in Java".

Calculator used to be equivalent to an artifact, but now with the power of the computer, the calculator is gradually declining and gradually integrated into the computer. The well-known calculator interface is mostly divided into two interfaces, namely, the display area and the keyboard area. The display area can use a text box component, and the keyboard area needs to be done with a lot of buttons, using the grid layout manager.

(1) create a new CalculatorDemo class that inherits from JFrame, and add constructors and main methods to this class.

Import java.awt.BorderLayout;import java.awt.GridLayout;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.border.EmptyBorder;public class CalculatorDemo extends JFrame {private JPanel contentPane; / / content panel private JTextField textField; / / text box public CalculatorDemo () {} / / Construction method public static void main (String [] args) {CalculatorDemo frame=new CalculatorDemo (); frame.setVisible (true);}}

(2) in the construction method, set the content of the window and complete the main part of the computer. Add a JTextField component to the north of the boundary to make the display area above.

Public CalculatorDemo () {setTitle ("Calculator"); / / set the title of the form setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / set the action setBounds when the form exits (100,100,250,200); / / set the position and size of the form contentPane=new JPanel (); / / create the content panel contentPane.setBorder (new EmptyBorder) / / set the border contentPane.setLayout of the panel (new BorderLayout (0J0)); / / set the content panel to the border layout setContentPane (contentPane); / / apply the content panel JPanel panel1=new JPanel (); / / create a new panel to save the text box contentPane.add (panel1,BorderLayout.NORTH); / / place the panel in the northern textField=new JTextField of the border layout () / / create a new text box textField.setHorizontalAlignment (SwingConstants.RIGHT); / / use right-aligned panel1.add (textField) for text in the text box; / / add the text box to the panel textField.setColumns (18); / / set the number of columns in the text box to 18}

(3) fill the content area with the middle of the border, and set the layout manager of panel2 for grid layout management.

JPanel panel2=new JPanel (); / / New panel is used to save button contentPane.add (panel2, BorderLayout.CENTER); / / place panel in the center of the border layout panel2.setLayout (new GridLayout (4meme 4meme 5)); / / Panel layout JButton button01=new JButton using grid 4X4 ("7"); / / New button panel2.add (button01); / / apply button JButton button02=new JButton ("8"); / / New button panel2.add (button02) / / apply button JButton button03=new JButton ("9"); / / New button panel2.add (button03); / / apply button JButton button04=new JButton ("+"); / / New button panel2.add (button04); / / apply button JButton button05=new JButton ("4"); / / New button panel2.add (button05); / / apply button JButton button06=new JButton ("5"); / / New button panel2.add (button06) / / apply button JButton button07=new JButton ("6"); / / New button panel2.add (button07); / / apply button JButton button08=new JButton ("-"); / / New button panel2.add (button08); / / apply button JButton button09=new JButton ("3"); / / New button panel2.add (button09); / / apply button JButton button10=new JButton ("2"); / / New button panel2.add (button10) / / apply button JButton button11=new JButton ("1"); / / New button panel2.add (button11); / / apply button JButton button12=new JButton ("*"); / / New button panel2.add (button12); / / apply button JButton button13=new JButton ("0"); / / New button panel2.add (button13); / / apply button JButton button14=new JButton ("."); / / New button panel2.add (button14) / / apply button JButton button15=new JButton ("="); / / New button panel2.add (button15); / / apply button JButton button16=new JButton ("/"); / / New button panel2.add (button16) / / Application button Thank you for your reading, the above is the content of "how to use Swing to achieve a simple calculator interface in Java". After the study of this article, I believe you have a deeper understanding of how to use Swing to achieve a simple calculator interface in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report