INSTITUTE : UIE
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programming (20CST-218)
TOPIC OF PRESENTATION:
Swing components – JApplet, JButton, JFrame etc
(Continued....)
DISCOVER . LEARN . EMPOWER
Lecture Objectives
In this lecture, we will discuss:
• Swing components – JApplet,
JButton, JFrame etc
(Continued....)
2
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.
JButton class declaration
Let's see the declaration for javax.swing.JButton 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 addActionListener(ActionListener a): It is used to add the action
listener to this object.
Java JButton Example with ActionListener
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample { b.addActionListener(new ActionListener(){
public static void main(String[] args) { public void actionPerformed(ActionEvent e){
JFrame f=new JFrame("Button Example"); tf.setText("Welcome to Javatpoint.");
}
final JTextField tf=new JTextField(); });
tf.setBounds(50,50, 150,20); f.add(b);f.add(tf);
JButton b=new JButton("Click Here"); f.setSize(400,400);
b.setBounds(50,100,95,30); f.setLayout(null);
f.setVisible(true);
}
}
Java Jframe
The javax.swing.JFrame class is a type of container which inherits the
java.awt.Frame class. JFrame works like the main window where components like
labels, buttons, textfields are added to create a GUI.
Unlike Frame, JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.
JFrame Example
public class JFrameExample {
public static void main(String s[]) {
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("JFrame By Example");
JButton button = new JButton();
button.setText("Button");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(200, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
MCQ
JButton class inherits
Button
ComboButton
AbstractButton
None
10
Summary:
In this session, you were able to :
• Learn about Swing components – JApplet, JButton,
JFrame etc
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.
Video Lectures :
https://youtu.be/FY3g4gGPhio
Reference Links:
https://www.javatpoint.com/java-jframe
https://www.javatpoint.com/java-jbutton
https://www.javatpoint.com/java-swing
http://zetcode.com/javaswing/basicswingcomponents/
THANK YOU