0% found this document useful (0 votes)
21 views19 pages

Javappt Raji

The JButton, JFrame, and JLabel classes are commonly used GUI components in Java Swing. JButton creates a platform-independent button that performs an action when clicked. JFrame acts as the main application window that other components can be added to. JLabel displays read-only text in a container. These classes make it easy to build graphical user interfaces in Java.

Uploaded by

Kaleeswari
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)
21 views19 pages

Javappt Raji

The JButton, JFrame, and JLabel classes are commonly used GUI components in Java Swing. JButton creates a platform-independent button that performs an action when clicked. JFrame acts as the main application window that other components can be added to. JLabel displays read-only text in a container. These classes make it easy to build graphical user interfaces in Java.

Uploaded by

Kaleeswari
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/ 19

TOPIC:

JBUTTON,JFRAME,JLABEL

BY,
P.RAJESWARI-I MSC(IT)
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


J button class declared as,
javax.swing.JButton class

Syntax:

public class JButton extends AbstractButton implements Accessible
Commonly used constructor
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.
Comonly used methods
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.

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.
Example program

import javax.swing.*;    
public class Button
{  
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);   
}  
}  
Output:

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.
Nested class

Modifier and Class Description
Type

protected class JFrame.Accessible Thisclass


JFrame implements
accessibility
support for the
JFrame class.
Commonly used for constructor
Constructor  Description

JFrame() It constructs a new frame that is initially


invisible.

JFrame(GraphicsConfiguration gc) It creates a Frame in the specified


GraphicsConfiguration of a screen device
and a blank title.

JFrame(String title) It creates a new, initially invisible Frame


with the specified title.

JFrame(String title, GraphicsConfiguration It creates a JFrame with the specified title


gc) and the specified GraphicsConfiguration of
a screen device.
Example program
import java.awt.FlowLayout;  

import javax.swing.JButton;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JPanel;  
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);  
    }  
}  
Output:

Java JLabel


 The object of JLabel class is a component for placing
text in a container.
 It is used to display a single line of read only text.
 The text can be changed by an application but a user
cannot edit it directly.
 It inherits JComponent class.
JLabel class declaration


Jlabel class declared as,
javax.swing.JLabel class
Syntax:
public class JLabel extends JComponent implements SwingConst
ants, Accessible  
Comonly used constructor
Constructor  Description

JLabel() Creates a JLabel instance with no image and


with an empty string for the title.

JLabel(String s) Creates a JLabel instance with the specified


text.

JLabel(Icon i) Creates a JLabel instance with the specified


image.

JLabel(String s, Icon i, int Creates a JLabel instance with the specified


horizontalAlignment) text, image, and horizontal alignment.
Comonly used methods

Methods Description

String getText() t returns the text string that a label displays.

void setText(String text) It defines the single line of text this


component will display.

void setHorizontalAlignment(int alignment) It sets the alignment of the label's contents


along the X axis.

Icon getIcon() It returns the graphic image that the label


displays.

int getHorizontalAlignment() It returns the alignment of the label's contents


along the X axis.
EXAMPLE PROGRAM

import javax.swing.*;  

class LabelExample  
{  
public static void main(String args[])  
    {  
    JFrame f= new JFrame("Label Example");  
    JLabel l1,l2;  
    l1=new JLabel("First Label.");  
    l1.setBounds(50,50, 100,30);  
    l2=new JLabel("Second Label.");  
    l2.setBounds(50,100, 100,30);  
    f.add(l1); f.add(l2);  
    f.setSize(300,300);  
    f.setLayout(null);  
    f.setVisible(true);  
    }  
    }  
OUTPUT:

THANKYOU!

You might also like