0% found this document useful (0 votes)
60 views13 pages

Lecture-3.1.6

This document discusses Swing components like JButton and JFrame. It provides details on commonly used JButton constructors and methods, and shows an example of adding an action listener to a JButton. It also describes the JFrame class and shows a basic example of how to create a JFrame with a JPanel containing a JLabel and JButton. The document concludes with references for further reading.

Uploaded by

gdgd
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)
60 views13 pages

Lecture-3.1.6

This document discusses Swing components like JButton and JFrame. It provides details on commonly used JButton constructors and methods, and shows an example of adding an action listener to a JButton. It also describes the JFrame class and shows a basic example of how to create a JFrame with a JPanel containing a JLabel and JButton. The document concludes with references for further reading.

Uploaded by

gdgd
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/ 13

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

You might also like