0% found this document useful (0 votes)
65 views15 pages

Java Swing

Swing is a GUI widget toolkit that is used to build desktop applications and provides platform-independent components like buttons, text fields, labels etc. It is built on top of the Abstract Window Toolkit (AWT) and provides lightweight components. Some commonly used Swing components include JButton, JTextField, JTextArea. Swing also provides different layout managers like BorderLayout, FlowLayout etc to organize components.

Uploaded by

rajeshchoudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views15 pages

Java Swing

Swing is a GUI widget toolkit that is used to build desktop applications and provides platform-independent components like buttons, text fields, labels etc. It is built on top of the Abstract Window Toolkit (AWT) and provides lightweight components. Some commonly used Swing components include JButton, JTextField, JTextArea. Swing also provides different layout managers like BorderLayout, FlowLayout etc to organize components.

Uploaded by

rajeshchoudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Java Swing

• Swing is a part of Java Foundation Classes (JFC)


that is used to create window-based applications.

•It is built on the top of AWT (Abstract Windowing


Toolkit) API and entirely written in java.

•Unlike AWT, Java Swing provides platform-


independent and lightweight components
The javax.swing package provides classes for

java swing API such as


• JButton,
• JTextField,
• JTextArea,
• JRadioButton,
• JCheckbox,
• JMenu,
• JColorChooser
Hierarchy of Java Swing classes
Swing Components
• JButton
• JLabel
• JTextField
• JCheckBox
• JRadioButton
• JComboBox
• JList
• JTable
Swing Layout Managers

• What are layout managers?


• Types of layout managers in Swing:
– BorderLayout
– FlowLayout
– GridLayout
– GridBagLayout
– BoxLayout
– CardLayout
Examples

import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


b.setBounds(130,100,100, 40);//x axis, y axis, width, height

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Output
import javax.swing.*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame

JButton b=new JButton("click");//creating instance of JButton


b.setBounds(130,100,100, 40);

f.add(b);//adding button in JFrame

f.setSize(400,500);//400 width and 500 height


f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}

public static void main(String[] args) {


new Simple();
}
}
import javax.swing.*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
b.setBounds(130,100,100, 40);

add(b);//adding button on frame


setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}
Java JButton
The JButton class is used to create a labeled button that has platform independent implementation.
The application result in some action when the button is pushed. It inherits AbstractButton class.

public class JButton extends AbstractButton implements Accessible


Commonly used Constructors

Constructor Description
JButton() It creates a button with no text and icon.
JButton(String s) It creates a button with the specified text.
JButton(Icon i) It creates a button with the specified icon object.
Commonly used Methods of AbstractButton class

Methods Description
void setText(String s) It is used to set specified text on button
String getText() It is used to return the text of the button.
void setEnabled(boolean b) It is used to enable or disable the button.
void setIcon(Icon b) It is used to set the specified Icon on the button.
Icon getIcon() It is used to get the Icon of the button.
void setMnemonic(int a) It is used to set the mnemonic on the button.
void It is used to add the action listener to this object.
addActionListener(ActionList
ener a)
Java Button Example
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
• Java JButton Example with ActionListener
• import java.awt.event.*;
• import javax.swing.*;
• public class ButtonExample {
• public static void main(String[] args) {
• JFrame f=new JFrame("Button Example");
• final JTextField tf=new JTextField();
• tf.setBounds(50,50, 150,20);
• JButton b=new JButton("Click Here");
• b.setBounds(50,100,95,30);
• b.addActionListener(new ActionListener(){
• public void actionPerformed(ActionEvent e){
• tf.setText("Welcome to Javatpoint.");
• }
• });
• f.add(b);f.add(tf);
• f.setSize(400,400);
• f.setLayout(null);
• f.setVisible(true);
• }
• }
import javax.swing.*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");

JButton b=new JButton(new ImageIcon("D:\\


icon.png"));
b.setBounds(100,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_C
LOSE);
}
public static void main(String[] args) {
new ButtonExample();
}
}

You might also like