Swing Components in Java :
There are many different types of Swing components that you can use in your Java applications. In this blog post, we’ll take a look at some of the most commonly used components.
Swing components are used to create graphical user interfaces (GUIs) in Java. They are part of the javax.swing
package.
The most commonly used Swing components are buttons, labels, text fields, and text areas.
Buttons are used to trigger an action. They can be configured to perform different actions when clicked.
Labels are used to display text or images. They cannot be clicked.
Text fields are used to input text. They can be single-line or multi-line.
Text areas are used to input or display text. They are usually multi-line.
There are many other types of Swing components, such as progress bars, sliders, and trees.
In this blog post, we’ve only scratched the surface of what Swing components can do. For more information, check out the javax.swing
package documentation.
import java.awt.*; import java.awt.event.*; import javax.swing.*; class Calculator extends JFrame implements ActionListener{ JLabel L1,L2,L3; JTextField t1,t2,t3; JButton btn1,btn2; public Calculator(){ L1 = new JLabel("Enter 1st number"); t1 = new JTextField(20); L2 = new JLabel("Enter 2nd number"); t2 = new JTextField(20); L3 = new JLabel("Result"); t3 = new JTextField(20); btn1= new JButton("+"); btn2 = new JButton("-"); t2 = new JTextField(20); add(L1); add(t1); add(L2); add(t2); add(L3); add(t3); add(btn1); add(btn2); setSize(400,400); setLayout(new FlowLayout(FlowLayout.LEFT)); setVisible(true); btn1.addActionListener(this); btn2.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed (ActionEvent ae){ String s1,s2; s1 =t1.getText(); s2 = t2.getText(); int n1,n2,n3; n1 = Integer.parseInt(s1); n2 = Integer.parseInt(s2); if(ae.getSource()==btn1) n3 = n1+n2; else n3 = n1-n2; t3.setText(String.valueOf(n3)); } public static void main(String[] args) { new Calculator(); } }
Also Read : How To Convert English Number To Nepali Number Using PHP, Python And JavaScript
Output
Leave Your Comment