Vous êtes sur la page 1sur 29

UNIT 3

EVENT-DRIVEN PROGRAMMING
( For Reference purpose Only)

MISSION VISION CORE VALUES


CHRIST is a nurturing ground for an individual’s Excellence and Service Faith in God | Moral Uprightness
holistic development to make effective contribution to Love of Fellow Beings
the society in a dynamic environment Social Responsibility | Pursuit of Excellence
CHRIST
Deemed to be University
Components

Excellence and Service


CHRIST
Deemed to be University

Button
● used to create a labeled button.

To create a Button:
● JButton b=new JButton("Click Here");

Components should be added in the Containers


● f.add(b); where f = frame variable.

Excellence and Service


CHRIST
Deemed to be University

import javax.swing.*;
public class Frame {
public static void main(String[] args) {
JFrame f=new JFrame();
JButton b=new JButton("Click Here");
f.add(b);
f.setTitle("Welcome");
f.setLocation(200,200);
f.setSize(400,400);
f.setResizable(false);
f.setVisible(true);
}
}
Note : BUTTON in Single Frame.

Excellence and Service


CHRIST
Deemed to be University
import javax.swing.JFrame;
import javax.swing.*;
public class Frame {
public static void main(String[] args) {
JFrame f=new JFrame();
JButton b=new JButton("Click Here");
b.setBackground(Color.red);
f.add(b);
f.setTitle("Welcome");
f.setLocation(200,200);
f.setSize(400,400);
f.setResizable(false);
f.setVisible(true);
}
}

Excellence and Service


CHRIST
Deemed to be University

Methods:

● setBounds(50,50,75,60); = Sets the Boundary value for the button

Excellence and Service


CHRIST
Deemed to be University

Adding Pictures in the button


import javax.swing.JFrame;
import javax.swing.*;
public class Frame {
public static void main(String[] args) {
JFrame f=new JFrame("Welcome");
JButton b=new JButton(new
ImageIcon("E:\ \ 2019-2020\ \ PP\ \ Unit
3\ \ Xavier Sir\ \ Button\ \ deepika.jpg"));
f.add(b);
f.setLocation(200,200);
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
}
} Excellence and Service
CHRIST
Deemed to be University

public class Frame {


public static void main(String[] args)
{
JFrame f=new JFrame("Welcome");
JButton b=new JButton(new
ImageIcon("E:\ \ 2019-2020\ \ PP\ \ Unit
3\ \ Xavier Sir\ \ Button\ \ deepika.jpg"));
b.setBounds(100,100,200,200);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT
_ON_CLOSE);
}
}
Excellence and Service
CHRIST
Deemed to be University

Java JButton
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.

It is used to set the specified Icon on the


void setIcon(Icon b)
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.

It is used to add the action listener to this


void addActionListener(ActionListener a)
object.

Excellence and Service


CHRIST
Deemed to be University

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.
public class JLabel extends JComponent implements SwingConstants,
Accessible
Constructor Description
Creates a JLabel instance with no image
JLabel()
and with an empty string for the title.
Creates a JLabel instance with the
JLabel(String s)
specified text.
Creates a JLabel instance with the
JLabel(Icon i)
specified image.
Creates a JLabel instance with the
JLabel(String s, Icon i, int
specified text, image, and horizontal
horizontalAlignment)
alignment.
Excellence and Service
CHRIST
Deemed to be University

Java JLabel

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

It defines the single line of text this


void setText(String text)
component will display.

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


alignment) contents along the X axis.

It returns the graphic image that the


Icon getIcon()
label displays.

It returns the alignment of the label's


int getHorizontalAlignment()
contents along the X axis.

Excellence and Service


CHRIST
Deemed to be University

Java JTextField

● The object of a JTextField class is a text component that allows the editing of
a single line text. It inherits JTextComponent class.

Constructor Description

JTextField() Creates a new TextField

Creates a new TextField initialized with


JTextField(String text)
the specified text.

Creates a new TextField initialized with


JTextField(String text, int columns)
the specified text and columns.

Creates a new empty TextField with the


JTextField(int columns)
specified number of columns.

Excellence and Service


CHRIST
Deemed to be University

Java JTextField

Methods Description

It is used to add the specified action


void addActionListener(ActionListener l) listener to receive action events from this
textfield.

It returns the currently set Action for this


Action getAction() ActionEvent source, or null if no Action is
set.

void setFont(Font f) It is used to set the current font.

It is used to remove the specified action


void removeActionListener(ActionListener
listener so that it no longer receives
l)
action events from this textfield.

Excellence and Service


CHRIST
Deemed to be University

Java JTextArea

The object of a JTextArea class is a multi line region that displays text.
It allows the editing of multiple line text.
It inherits JTextComponent class
Constructor Description
Creates a text area that displays no text
JTextArea()
initially.
Creates a text area that displays specified
JTextArea(String s)
text initially.
Creates a text area with the specified
JTextArea(int row, int column) number of rows and columns that
displays no text initially.
Creates a text area with the specified
JTextArea(String s, int row, int
number of rows and columns that
column)
displays specified text.
Excellence and Service
CHRIST
Deemed to be University

Methods Description

It is used to set specified number


void setRows(int rows)
of rows.
It is used to set specified number
void setColumns(int cols)
of columns.
It is used to set the specified
void setFont(Font f)
font.

It is used to insert the specified


void insert(String s, int position)
text on the specified position.

It is used to append the given


void append(String s)
text to the end of the document.

Excellence and Service


CHRIST
Deemed to be University

Java JPasswordField

● The object of a JPasswordField class is a text component specialized for


password entry.
● It allows the editing of a single line of text. It inherits JTextField class.
Constructor Description

Constructs a new JPasswordField, with a


JPasswordField() default document, null starting text string,
and 0 column width.

Constructs a new empty JPasswordField


JPasswordField(int columns)
with the specified number of columns.

Constructs a new JPasswordField


JPasswordField(String text)
initialized with the specified text.
Construct a new JPasswordField
JPasswordField(String text, int columns) initialized with the specified text and
columns.

Excellence and Service


CHRIST
Deemed to be University

Java JCheckBox

● The JCheckBox class is used to create a checkbox. It is used to turn an option on


(true) or off (false).
● Clicking on a CheckBox changes its state from "on" to "off" or from "off" to "on “. It
inherits JToggleButton class.
Constructor Description

Creates an initially unselected check box


JJCheckBox()
button with no text, no icon.

Creates an initially unselected check box


JChechBox(String s)
with text.

Creates a check box with text and specifies


JCheckBox(String text, boolean selected)
whether or not it is initially selected.

Creates a check box where properties are


JCheckBox(Action a)
taken from the Action supplied.
Excellence and Service
CHRIST
Deemed to be University

Methods Description

AccessibleContext It is used to get the AccessibleContext


getAccessibleContext() associated with this JCheckBox.

It returns a string representation of this


protected String paramString()
JCheckBox.

Excellence and Service


CHRIST
Deemed to be University

Java JRadioButton

● The JRadioButton class is used to create a radio button.


● It is used to choose one option from multiple options. It is widely used in exam
systems or quiz.

Constructor Description

Creates an unselected radio button with


JRadioButton()
no text.

Creates an unselected radio button with


JRadioButton(String s)
specified text.

Creates a radio button with the specified


JRadioButton(String s, boolean selected)
text and selected status.

Excellence and Service


CHRIST
Deemed to be University

Methods Description

It is used to set specified text on


void setText(String s)
button.
It is used to return the text of the
String getText()
button.
It is used to enable or disable the
void setEnabled(boolean b)
button.
It is used to set the specified Icon on
void setIcon(Icon b)
the button.
It is used to get the Icon of the
Icon getIcon()
button.
It is used to set the mnemonic on the
void setMnemonic(int a)
button.
void It is used to add the action listener to
addActionListener(ActionListener a) this object.

Excellence and Service


CHRIST
Deemed to be University

Java JComboBox

● The object of Choice class is used to show popup menu of choices.


● Choice selected by user is shown on the top of a menu. It inherits
JComponent class.

Constructor Description

Creates a JComboBox with a default data


JComboBox()
model.

Creates a JComboBox that contains the


JComboBox(Object[] items)
elements in the specified array.

Creates a JComboBox that contains the


JComboBox(Vector<?> items)
elements in the specified Vector.

Excellence and Service


CHRIST
Deemed to be University

Methods Description

void addItem(Object anObject) It is used to add an item to the item list.

void removeItem(Object anObject) It is used to delete an item to the item list.

It is used to remove all the items from the


void removeAllItems()
list.

It is used to determine whether the


void setEditable(boolean b)
JComboBox is editable.

void addActionListener(ActionListener a) It is used to add the ActionListener.

void addItemListener(ItemListener i) It is used to add the ItemListener.

Excellence and Service


CHRIST
Deemed to be University

Java JList

● The object of JList class represents a list of text items. The list of text items
can be set up so that the user can choose either one item or multiple items. It
inherits JComponent class.

Constructor Description

Creates a JList with an empty, read-only,


JList()
model.

Creates a JList that displays the elements


JList(ary[] listData)
in the specified array.

Creates a JList that displays elements


JList(ListModel<ary> dataModel)
from the specified, non-null, model.

Excellence and Service


CHRIST
Deemed to be University

Methods Description

Void It is used to add a listener to the list,


addListSelectionListener(ListSelectio to be notified each time a change to
nListener listener) the selection occurs.

It is used to return the smallest


int getSelectedIndex()
selected cell index.

It is used to return the data model


ListModel getModel() that holds a list of items displayed by
the JList component.

It is used to create a read-only


void setListData(Object[] listData)
ListModel from an array of objects.

Excellence and Service


CHRIST
Deemed to be University

Java JScrollBar

● The object of JScrollbar class is used to add horizontal and vertical scrollbar.
It is an implementation of a scrollbar. It inherits JComponent class.

Constructor Description

Creates a vertical scrollbar with the initial


JScrollBar()
values.

Creates a scrollbar with the specified


JScrollBar(int orientation)
orientation and the initial values.

Creates a scrollbar with the specified


JScrollBar(int orientation, int value, int
orientation, value, extent, minimum, and
extent, int min, int max)
maximum.

Excellence and Service


CHRIST
Deemed to be University

Java JMenuBar, JMenu and JMenuItem

● The JMenuBar class is used to display menubar on the window or frame. It


may have several menus.
● The object of JMenu class is a pull down menu component which is displayed
from the menu bar. It inherits the JMenuItem class.
● The object of JMenuItem class adds a simple labeled menu item. The items
used in a menu must belong to the JMenuItem or any of its subclass.

Excellence and Service


CHRIST
Deemed to be University

Java JMenuBar, JMenu and JMenuItem

JMenuBar class declaration


public class JMenuBar extends JComponent implements MenuElement, Accessi
ble

JMenu class declaration


public class JMenu extends JMenuItem implements MenuElement, Accessible

JMenuItem class declaration


public class JMenuItem extends AbstractButton implements Accessible, MenuEle
ment

Excellence and Service


CHRIST
Deemed to be University

Java JPanel

● The JPanel is a simplest container class. It provides space in which an


application can attach any other component. It inherits the JComponents
class.
● It doesn't have title bar.

It is used to create a new JPanel with a


JPanel()
double buffer and a flow layout.

It is used to create a new JPanel with


JPanel(boolean isDoubleBuffered) FlowLayout and the specified buffering
strategy.

It is used to create a new JPanel with the


JPanel(LayoutManager layout)
specified layout manager.

Excellence and Service


CHRIST
Deemed to be University

Assignment

● Java JScrollPane
● Java JOptionPane
● Java JDialog
● Java JComponent

Excellence and Service

Vous aimerez peut-être aussi