In this section, you will learn how to sum of a number using swing. JTextField, JButton is a component of GUI.
Brief description of the Component used in this application :
1. JFrame is used to creating java
standalone application, you must provide GUI for a user. The most common way of
creating a frame is,
using single argument constructor of the JFrame class. The argument of the constructor is the title of the window or frame.
JFrame f = new JFrame("Calculate");
2. JTextField is a lightweight component that allows the editing of a single line of text.
JTextField has a method to establish the string
used as the command string for the action event that gets fired. The java.awt.TextField used the text of the field as the command string
or the ActionEvent. JTextField will use the command string set with the setActionCommand method if not null, otherwise it will use the
text of the field as a compatibility with java.awt.TextField.
final JTextField text1 = new JTextField(20);
final JTextField text2 = new JTextField(20);
3. JButton with a String as a label, and then drop it in a window. Events are normally handled just as with a Button: you attach an
ActionListener via the addActionListener method.
JButton button = new JButton("Calculate");
Step: Create a file "Calculate.java" to sum of a number using Swing.
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial