Applet 140505042019-phpapp01

Post on 18-Dec-2014

44 views 0 download

description

 

transcript

INTRODUCTION TO APPLETIMPLEMENT & EXECUTING APPLET WITH

PARAMETERS APPLET LIFE CYCLE

APPLET CLASSGRAPHICS CLASS

AWT & SWING

Presentation by Nyversity

Applet

Applet

Presentation by Nyversity

Applet is a special type of program that is embedded in the webpage to generate the dynamic content.

It runs inside the browser and works at client side. It works at client side so less response time.

Secured.

It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc.

Plug-in is required at client browser to execute applet.

Applets

Presentation by Nyversity

Applets don’t use the main() method, but when they are load, automatically call certain methods (init, start, paint, stop, destroy).

They are embedded inside a web page and executed in browsers.

They cannot read from or write to the files on local computer.

They cannot communicate with other servers on the network.

They cannot run any programs from the local computer.

They are restricted from using libraries from other languages.

How to run an Applet?

Presentation by Nyversity

There are two ways to run an applet By appletViewer toolBy html file.

By Appletviewer

Presentation by Nyversity

import java.applet.*;import java.awt.*;

/*<applet code="helloA" width=200 height=500> </applet>*/

public class helloA extends Applet{

public void paint(Graphics g){

g.setColor(Color.red);g.drawString("Hello....",10,25);

}}Javac helloA.javaappletviewer helloA.java

Presentation by Nyversity

Demo of Run by Appletviewer

By HTML File

Presentation by Nyversity

<html>

<applet code="helloA.class" width=200 height=500></applet>

</html>

Presentation by Nyversity

Demo of Run by HTML file

Applet Life Cycle

Presentation by Nyversity

1. Initialization state

Presentation by Nyversity

Applet enter in initialize state when it is loaded.

In this state called init() method & overrode the code.

This state occurs only ones in applet life cycle.

In this state we done following task. Creating Object Load image or font Set text or background Color Setup initials values

2. Running state

Presentation by Nyversity

Applet enter the running state when the system call the start() method of applet class.

This state occurs automatically after the applet is initialization.

Starting can also occur if the applet is already in “stopped” state.

We may override the start() method to execute the statement.

3. Idle or Stopped state

Presentation by Nyversity

Applet become idle when it is stopped from running.

stopping occur automatically when we leave the current page.

we may override the stop() method to execute the statement.

4. Dead State

Presentation by Nyversity

Applet said to be dead when it is removed from memory.

This state called destroy() automatically when we quite from a browser.

This state occurs only once in applet life cycle.

This state is used to clean up resources.

5. Display State

Presentation by Nyversity

Applet moves to the display state whenever is has perform some output operation on screen.

This happens immediately after the applet enter in to running state.

Almost every applet will have a paint() method.

We must Remember Display state is not part of Applet life cycle.

Presentation by Nyversity

Demo of Applet Life Cycle

Applet Class

Presentation by Nyversity

Applet Class Methods

Presentation by Nyversity

Graphics Class

Presentation by Nyversity

A Graphics object encapsulates a set of methods that can perform graphics output.

Specifically, it allows you to draw lines, ovals, rectangles, strings, images, characters, and arcs.

Graphics class Methods

Presentation by Nyversity

Presentation by Nyversity

Presentation by Nyversity

Demo of Graphics Class

Color class

Presentation by Nyversity

The java.awt.Color class is used to work with colors.

Each instance of this class represents a particular color.

With the help of this class we can draw colored strings, lines, and shapes in an applet.

It is possible to fill a shape such as an oval or rectangle with a color.

We can also set the background and foreground colors of an applet.

The Color class also defines several constants that represent specific colors.

These are black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange pink, red, white, and yellow.

Constructors

Presentation by Nyversity

Color( int red, int green, int blue) These values should be between 0 and 255. Color c=new Color(0,255,0); //Green color

Color(float red, float green, float blue) The value between 0.0 and 1.0

Color(int RGB value) Here the RGBvalue is the mix of red, green

and blue. The red, blue and green stored in 8 bits. 0-7 bits blue color. 8-15 bits for green. 16 to 23 bits for red.

Color Class Methods

Presentation by Nyversity

Presentation by Nyversity

Demo of Color Class

Displaying Text

Presentation by Nyversity

We have seen that drawstring() method of the Graphics class is used to display text.

We can also display strings by using different fonts. This is necessary in many applets.

For example, tables and graphs use various fonts for titles, axes, data and other information.

java.awt.Font

Presentation by Nyversity

A font determines the size and appearance of characters in a string. Information about a font is encapsulated by the java.awt.Font class.

The following is one of its constructors: Font (String name, int style, int ps)

Here, name identifies the font. Some commonly used font names are Serif,

SansSerif, and Monospaced. The style may be BOLD, ITALIC, or PLAIN. The point size of the font is ps.

Presentation by Nyversity

After a font has been created, you may then use it in a graphics context.

This is done by calling the setFont() method of the Grahics class.

This method has the following format: Void setFont (Font font)

Here, font is a Font object. after this method is called, any strings that are output via the drawstring() method are displayed with that font.

For example: g.setFont(new Font("serif",Font.BOLD,36)); g.setColor(Color.black); g.drawString("Font example",5,100);

Presentation by Nyversity

Demo of Font Class

Applet Tag

Presentation by Nyversity

The applet viewer uses the HTML source code at the beginning of a .java file to display an applet.

It is also possible to embed an applet in a web page.

The complete syntax for an applet tag is shown in the following listing.

Optional lines are enclosed in brackets.

Presentation by Nyversity

<applet[codebase=url]Code=clsName[alt=text][name=appName]Width=wpixelsHeight=hpixels[align=alignment][vspace=vspixels][hspace=hspixels]>[<param name=pname1 value=value1>][<param name=pname2 value=value2>]…[<param name=pnameN value=valueN>]</applet>

Presentation by Nyversity

Demo of Applet tags of html

Presentation by Nyversity

Demo of Using thread in Applet

Presentation by Nyversity

Awt

Presentation by Nyversity

Abstract Windowing Toolkit (AWT) is used for GUI programming in java.

Awt hierarchy

Presentation by Nyversity

Presentation by Nyversity

Container: The Container is a component in AWT that can

contain another components like buttons, textfields, labels etc.

The classes that extends Container class are known as container.

Window: The window is the container that have no

borders and menubars. You must use frame, dialog or another window

for creating a window.Panel:

The Panel is the container that doesn't contain title bar and MenuBars.

It can have other components like button, textfield etc.

Presentation by Nyversity

Frame: The Frame is the container that contain title bar and

can have MenuBars. It can have other components like button, textfield etc.

Commonly used Methods of Component class:1)public void add (Component c)2)public void setSize (int width,int height)3)public void setLayout (LayoutManager

m)4)public void setVisible (boolean)

Presentation by Nyversity

Demo of awt Control

Presentation by Nyversity

Swing

Presentation by Nyversity

Java swing provides the multiple platform independent API’s interfaces for interacting between the user and GUI components.

All java swing classes import from the import javax.swing.*; package.

javax package is a java extension package.Java provides an interactive features for

design the GUI’s toolkit or components like: label, button, test boxes, check boxes, combo boxes, panels, sliders etc.

Presentation by Nyversity

Swing components are said lightweight components because they do not containt platform specific code.

Swing components have better GUI than the AWT components.

For example: in swing a button component can display both the caption and also an image on it. Which is not possible in AWT button.

Swing hierarchy

Presentation by Nyversity

JApplet Class

Presentation by Nyversity

The JApplet class is a subclass of the Applet class.

An applet must extend the JApplet class which uses the swing component.

The JApplet provides the support for various panes such as the content pane, the glass pane etc.

To add component in JApplet, you have to call add() method by a content pane object.

Presentation by Nyversity

To obtain a content pane object, following method is used. Container getContentPane() Now to add a component in this pane, the add()

method is used.

Void add (componnentObj) Here, the componentObj is the component which is

to be added to the JApplet.

JComponent Class

Presentation by Nyversity

The JComponent class is a subclass of Container class.

This class have many subclasses such as JFrame, JPanel JLabel, JButton, JRadioButton, JCheckBox JTextField, JPasswordField, JTextArea JScrollBar, JComboBox, JList Menus (JMenuBar, JMenu,JMenuItem)

JLabel

Presentation by Nyversity

JLabel class is used to display a label on the window.

This label can have a text as well as an image also.Constructors:

JLabel (String label) JLabel (Icon i) JLabel (String label, Icon I, int align)

Here, the label is the String which will be displayed as the label.

The align specifies the alignment which can be JLabel.LEFT, JLabel.RIGHT, JLabel.CENTER.

The icon is an interface which is implemented by ImageIcon Class. This class draw an image.

Presentation by Nyversity

Constructors are: ImageIcon (String filename) ImageIcon (URL url)

Here, finename is the name of the imagefile and the url is the path of the file.

Methods of JLabel

Presentation by Nyversity

public String getText() Returns the text string that the label displays.

public void setText(String text) Defines the single line of text this component

will display. If the value of text is null or empty string, nothing is displayed.

public Icon getIcon() Returns the graphic image that the label

displays. public void setIcon(Icon icon)

Defines the icon this component will display. If the value of icon is null, nothing is displayed.

public void setVisible(boolean b) Shows or hides this Window depending on the value of

parameter b.

Presentation by Nyversity

public void setBounds(int x,int y,int width,int height) Moves and resizes this component. The new

location of the top-left corner is specified by x and y, and the new size is specified by width and height.

Parameters: x - the new x-coordinate of this component

y - the new y-coordinate of this component width - the new width of this component height - the new height of this component

public void setEnabled(boolean b)Enables or disables this component, depending on

the value of the parameter b. An enabled component can respond to user input

and generate events. Components are enabled initially by default.

JTextField

Presentation by Nyversity

The JTextField class creates a text field which can accept the user input.

Constructors are: JTextField() JTextField (String str) JTextField (int columns) JTextField (String str, int columns)

The first form creates a default text field which contains no string in it.

The second form str specified the string which will be displayed in the text field

Presentation by Nyversity

The columns parameter specifies the width of the text field. this width does not specifies the limit of the number of characters that can be entered in the text field.

The JTextField class generates an ActionEcent when user presses the enter key after input the data in the text field.

Methods of JTextField

Presentation by Nyversity

public int getColumns() Returns the number of columns in this TextField. Returns: the number of columns >= 0

public void setColumns(int columns) Sets the number of columns in this TextField, and then

invalidate the layout. Parameters: columns - the number of columns >= 0

public void setText(String text) Set the specified text in this JTextField.

public String getText() Return the text of this JtextField.

Presentation by Nyversity

Demo of JLabel and JTextField class

JButton

Presentation by Nyversity

JButton is used to display a text as well as an image on it.

The swin buttons are subclass of the AbstractButton class.

Constructors: public JButton()

Creates a button with no set text or icon public JButton(Icon icon)

Creates a button with an icon. Parameters: icon - the Icon image to display

on the button public JButton(String text)

Creates a button with text. Parameters: text - the text of the button

Presentation by Nyversity

public JButton(String text,Icon icon) Creates a button with initial text and an icon. Parameters: text - the text of the button icon - the Icon image to display on the button

Presentation by Nyversity

Demo of JButton

JRadioButton class

Presentation by Nyversity

The JRadioButton is a subclass of the AbstractButton class.itis used to create radio buttons.

An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user.

Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)

ButtonGroup bg = new ButtonGroup(); bg.add(b1); bg.add(b2); bg.add(b3);

Presentation by Nyversity

A ButtonGroup can be used with any set of objects that inherit from AbstractButton. Typically a button group contains instances of JRadioButton.

public ButtonGroup() Creates a new ButtonGroup.

Void add(AbstractButton obj) Obj is the object of the JRadioButton to be added.

public int getButtonCount() Returns the number of buttons in the group.

Constructors JRadioButton

Presentation by Nyversity

public JRadioButton() public JRadioButton(Icon icon)public

JRadioButton(Icon icon,boolean selected)public JRadioButton(String text)public

JRadioButton(String text,boolean selected)

public JRadioButton(String text,Icon icon) public JRadioButton

(String text,Icon icon,boolean selected)

Methods

Presentation by Nyversity

public String getText() Returns the text string that the JRadioButton displays.

public void setText(String text) Defines the single line of text this component will display.

If the value of text is null or empty string, nothing is displayed.

public Icon getIcon() Returns the graphic image that the JRadionButton

displays.public void setIcon(Icon icon)

Defines the icon this component will display. If the value of icon is null, nothing is displayed.

public void setBounds(int x,int y,int width,int height)public void setVisible(boolean b)public void setEnabled(boolean b)public Boolean isSelected()

Presentation by Nyversity

Demo of JRadioButton

JTextArea class

Presentation by Nyversity

JTextArea class is used to create a multi line text input.

This text does not display any scrollbars but you should use scroll panes if need it.

Constructors public JTextArea() public JTextArea(String text) public JTextArea(int rows,int columns) public JTextArea(String text,int rows,int columns)

Methods of JTextArea

Presentation by Nyversity

public void setLineWrap(boolean wrap) Sets the line-wrapping policy of the text area.

public boolean getLineWrap() Gets the line-wrapping policy of the text area.

public int getLineCount() Determines the number of lines contained in the area.

Returns: the number of lines > 0public void insert(String str,int pos)

Inserts the specified text at the specified position.public void append(String str)

Appends the given text to the end of the document

Presentation by Nyversity

public void replaceRange(String str,int start,int end) Replaces text from the indicated start to end position

with the new text specified.

public int getRows() Returns the number of rows in the TextArea. Returns: the number of rows >= 0

public int getColumns() Returns the number of columns in the TextArea. Returns: number of columns >= 0

public void setFont(Font f) is used to set the specified font.

Presentation by Nyversity

Demo of JTextArea class

JPasswordField Class

Presentation by Nyversity

The JPasswordField creates a text field which display the disks (dots) instead of the actual characters.

Constructors JPasswordField() JPasswordField(int length) JPasswordField(String str) JPasswordField(String str, int length)

Methods of JPasswordField

Presentation by Nyversity

public char getEchoChar() Returns the character to be used for echoing. The

default is '*'.

public void setEchoChar(char c) Sets the echo character for this JPasswordField.

public boolean echoCharIsSet() Returns true if this JPasswordField has a character set

for echoing.

public char[] getPassword() Returns the text contained in this TextComponent.

Presentation by Nyversity

Demo of JPasswordField Class

JCheckBox Class

Presentation by Nyversity

This class is a subclass of the AbstactButton class.

It is used to create a checkbox.

An implementation of a check box -- an item that can be selected or deselected, and which displays its state to the user.

Constructors

Presentation by Nyversity

public JCheckBox()public JCheckBox(Icon icon)public

JCheckBox(Icon icon,boolean selected)public JCheckBox(String text) public

JCheckBox(String text,boolean selected)public JCheckBox(String text,Icon icon)

Methods of CheckBox class

Presentation by Nyversity

public String getText() Returns the text string that the JCheckBox displays.

public void setText(String text) Defines the single line of text this component will

display. If the value of text is null or empty string, nothing is displayed.

public Icon getIcon() Returns the graphic image that the JCheckBox

displays.public void setIcon(Icon icon)

Defines the icon this component will display. If the value of icon is null, nothing is displayed.

Presentation by Nyversity

public void setBounds(int x,int y,int width,int height) Moves and resizes this component. The new location of

the top-left corner is specified by x and y, and the new size is specified by width and height.

public void setVisible(boolean b) Shows or hides this Window depending on the value of

parameter b. public void setEnabled(boolean b)

Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially by default.

public Boolean isSelected() Test for this JCheckBox is selected or not.

Presentation by Nyversity

Demo of JCheckBox

JComboBox class

Presentation by Nyversity

A component that combines a button or editable field and a drop-down list.

The user can select a value from the drop-down list, which appears at the user's request.

If you make the combo box editable, then the combo box includes an editable field into which the user can type a value.

Constructors

Presentation by Nyversity

public JComboBox() Creates a JComboBox with a default data model. The

default data model is an empty list of objects. Use addItem to add items. By default the first item in the data model becomes selected.

public JComboBox(Object[] items) Creates a JComboBox that contains the elements in the

specified array. By default the first item in the array (and therefore the data model) becomes selected.

public JComboBox(Vector<?> items) Creates a JComboBox that contains the elements in the

specified Vector. By default the first item in the vector (and therefore the data model) becomes selected.

Methods of ComboBox

Presentation by Nyversity

public void setEditable(boolean aFlag) Determines whether the JComboBox field is editable.

An editable JComboBox allows the user to type into the field or selected an item from the list to initialize the field, after which it can be edited.

public void setMaximumRowCount(int count) Sets the maximum number of rows the JComboBox

displays. If the number of objects in the model is greater than count, the combo box uses a scrollbar.

count - an integer specifying the maximum number of items to display in the list before using a scrollbar.

Presentation by Nyversity

public int getMaximumRowCount() Returns the maximum number of items the combo box

can display without a scrollbar. Returns: an integer specifying the maximum number

of items that are displayed in the list before using a scrollbar

public void setSelectedItem(Object anObject) Sets the selected item in the combo box display area

to the object in the argument. If anObject is in the list, the display area shows anObject selected.

public Object getSelectedItem() Returns the current selected item.

public void setSelectedIndex(int anIndex) Selects the item at index anIndex.

Presentation by Nyversity

int getSelectedIndex() Returns the first item in the list that matches the given

item. The result is not always defined if the JComboBox allows selected items that are not in the list. Returns -1 if there is no selected item or if the user specified an item which is not in the list.

public void addItem(Object anObject) Adds an item to the item list. This method works only if

the JComboBox uses a mutable data model. public void insertItemAt(Object anObject,int

index) Inserts an item into the item list at a given index. This

method works only if the JComboBox uses a mutable data model.

Parameters: anObject - the Object to add to the list index - an integer specifying the position at which to add

the item

Presentation by Nyversity

public void removeItem(Object anObject) Removes an item from the item list. This method

works only if the JComboBox uses a mutable data model.

Parameters: anObject - the object to remove from the item list.

public void removeItemAt(int anIndex) Removes the item at anIndex This method works

only if the JComboBox uses a mutable data model. Parameters: anIndex - an int specifying the index of

the item to remove, where 0 indicates the first item in the list

Presentation by Nyversity

public void removeAllItems() Removes all items from the item list.

public void setEnabled(boolean b) Enables the combo box so that items can be selected.

public int getItemCount() Returns the number of items in the list. Returns: an integer equal to the number of items in the

list

public Object getItemAt(int index) Returns the list item at the specified index. If index is

out of range (less than zero or greater than or equal to size) it will return null.

Parameters: index - an integer indicating the list position, where the first item starts at zero

Returns: the Object at that list position; or null if out of range

Presentation by Nyversity

Demo of JComboBox

JScrollPane Class

Presentation by Nyversity

A scroll pane is used to display other components or an image in a rectangular area.

This pane have horizontal as well as vertical scroll bars.

Constructors JScrollPane(Component obj) JScrollPane(int vertScrollbar,int horScrollbar) JScrollPane(Component obj,int vertScrollbar,int

horScrollbar

Constants in JScrollPane

Presentation by Nyversity

HORIZONTAL_SCROLLBAR_ALWAYS

HORIZONTAL_SCROLLBAR_AS_NEEDED

VERICAL_SCROLLBAR_ALWAYS

VERTICAL_SCROLLBAR_AS_NEEDED

Methods of JScrollaPane

Presentation by Nyversity

public int getValue() Returns the scrollbar's value.

public void setValue(int value) Sets the scrollbar's value..

public int getMinimum() Returns the minimum value supported by the scrollbar

(usually zero). public void setMinimum(int minimum)

Sets the model's minimum property. public int getMaximum()

The maximum value of the scrollbar is maximum - extent. public void setMaximum(int maximum)

Sets the model's maximum property. public void setEnabled(boolean x)

Enables the component so that the knob position can be changed. When the disabled, the knob position cannot be changed.

Presentation by Nyversity

Demo of JScrollPaneDemo of JScrollPane2

JScrollBar Class

Presentation by Nyversity

JScrollBar class is used to create vertical and horizontal scrollbars.

Constructors: JScrollBar() JScrollBar (int type) JScrollBar (int type, int value, int extent, int min,

int max)

Presentation by Nyversity

JScrollBar (int type, int value, int extent, int min, int max) Here, type specifies that whether the scrollbar is

horizontal or vertical. This is defined by two constants: JScrollBar.HORIZONTAL, JScollBar.VERTICAL

The value specifies the initial value of the scrollbar slider.

The extent is the extent the value of the slider. The min and max specifies the range of the

scrollbar.

Presentation by Nyversity

The scrollbar generates an adjustment event.

The AdjustmentListener interface contains the method adjustmentValueChanged() which is called when scrollbar value is adjusted.

A method of JScrollBar is int getValue(). It returns the value of the scrollbar

Presentation by Nyversity

Demo of JScrollBar

JList class

Presentation by Nyversity

The list class creates a list box which can display more than one items in the combo box at a time.

It lets user to select more than one item from the list.

If the items in the list are too many to display all in the list box, you have to add it in a scroll pane as it does not display it by default.

Constructors for JList

Presentation by Nyversity

public JList() Constructs a JList with an empty, read-only, model.

public JList(Vector<?> listData) Constructs a JList that displays the elements in the

specified Vector.

public JList(Object[] listData) Constructs a JList that displays the elements in the

specified array.

Methods of JList

Presentation by Nyversity

Void setSelectionMode(int selectionMode) The selection mode can be specified by the following

constants defined by the ListSelectionModel. ListSelectionModel.SINGLE_SELECTION ListSelectionModel.SINGLE_INTERVAL_SELECTI

ON ListSelectionModel.MULTIPLE_INTERVAL_SELEC

TION

public int getVisibleRowCount() Returns the value of the visibleRowCount property.

public void setVisibleRowCount(int visibleRowCount) It sets the number of items that can be visible in the

list box at once.

Presentation by Nyversity

public boolean isSelectedIndex(int index) Returns true if the specified index is selected, else

false.public int[] getSelectedIndices()

Returns an array of all of the selected indices, in increasing order.

public void setSelectedIndices(int[] indices) Changes the selection to be the set of indices specified

by the given array.public Object[] getSelectedValues()

Returns an array of all the selected values, in increasing order based on their indices in the list.

public int getSelectedIndex() Returns the smallest selected cell index; the selection

when only a single item is selected in the list.

Presentation by Nyversity

Demo of JList

JFrame Class

Presentation by Nyversity

The JFrame class creates a frame which is a window.

This window has a border, a title, and buttons for closing, minimizing, maximizing the window.

Constructors public JFrame()

Constructs a new frame that is initially invisible. public JFrame (String title)

Creates a new, initially invisible Frame with the specified title.

Methods of JFrame

Presentation by Nyversity

public void setDefaultCloseOperation(int operation) Sets the operation that will happen by default when the user

initiates a "close" on this frame. You must specify one of the following choices:

DO_NOTHING_ON_CLOSE It does nothing when the close button is pressed.

HIDE_ON_CLOSE It hides the window when you press the close button, but still

it remains alive. This is the default operation. DISPOSE_ON_CLOSE

This hides and disposes the window when you press the close button in title bar.

it removes the window from the screen and removes it from memory also.

EXIT_ON_CLOSE It exits the whole application on pressing the close button

using System.exit(0) method.

Presentation by Nyversity

public void setJMenuBar(JMenuBar menubar) Sets the menubar for this frame.

public void remove(Component comp) Removes the specified component from the container.

public void setLayout(LayoutManager manager) Sets the LayoutManager.

public Container getContentPane() Returns the contentPane object for this frame.

public void setTitle(String title) Sets the title for this frame to the specified string. title - the title to be displayed in the frame's border. A

null value is treated as an empty string, "".- public String getTitle()

Gets the title of the frame. The title is displayed in the frame's border.

Returns: the title of this frame, or an empty string ("") if this frame doesn't have a title.

Presentation by Nyversity

public void setBounds(int x,int y,int width,int height) Moves and resizes this component. The new location of the

top-left corner is specified by x and y, and the new size is specified by width and height.

Parameters: x - the new x-coordinate of this component y - the new y-coordinate of this component width - the new width of this component height - the new height of this component

public void setVisible (boolean b) Shows or hides this Window depending on the value of

parameter b. public void setEnabled (boolean b)

Enables or disables this component, depending on the value of the parameter b.

An enabled component can respond to user input and generate events. Components are enabled initially by default.

Presentation by Nyversity

Demo of JFrame 1Demo of JFrame 2

JMenu, JMenuBar and JMenuItem

Presentation by Nyversity

These classes provide functionalities to create a menu bar which contains menus.

These menus in turn have menu items.

The menu bar is created using JMenubar class.

Menu is created using JMenu class and the JMenuItem creates menu items.

Steps for creating menu

Presentation by Nyversity

1. Create a JMenuBar object.2. Then set the menu bar using setMenuBar()

method.3. Create menu objects of JMenu class.4. Create menu item odjects using JMenuItem

class.5. Add the menu items to the particular menus.6. Add the menus to the menu bar.

JMenuBar Class

Presentation by Nyversity

JMenuBar class creates a menu bar which contains the menus.

Constructor

public JMenuBar() Creates a new menu bar.

Methods of JMenuBar class

Presentation by Nyversity

public JMenu add (JMenu  c) Appends the specified menu to the end of the menu

bar. Parameters: c - the JMenu component to add Returns: the menu component

public int getMenuCount() Returns the number of items in the menu bar. Returns: the number of items in the menu bar.

JMenu Class

Presentation by Nyversity

An implementation of a menu -- a popup window containing JMenuItems that is displayed when the user selects an item on the JMenuBar.

The JMenu class is a container of menus.Constructors

public JMenu() Constructs a new JMenu with no text. public JMenu (String s) Constructs a new JMenu with the supplied string as its

text. Parameters: s - the text for the menu label

Methods of JMenu Class

Presentation by Nyversity

public boolean isSelected() Returns true if the menu is currently selected

(highlighted). public void setSelected (boolean b)

Sets the selection status of the menu public JMenuItem add (JMenuItem

 menuItem) Appends a menu item to the end of this menu. Returns

the menu item added. Parameters: menuItem - the JMenuitem to be added Returns: the JMenuItem added

Presentation by Nyversity

public void addSeparator() Appends a new separator to the end of the menu.

public int getItemCount() Returns the number of items on the menu, including

separators.

public void remove (JMenuItem  item) Removes the specified menu item from this menu. If

there is no popup menu, this method will have no effect.

Parameters: item - the JMenuItem to be removed from the menu

public void removeAll() Removes all menu items from this menu.

JMenuItem Class

Presentation by Nyversity

The JMenuItem class is used to create menu items.these menu items will be added to the menu.

An implementation of an item in a menu. A menu item is essentially a button sitting in a list.

When the user selects the "button", the action associated with the menu item is performed.

Constructors of JMenuItem

Presentation by Nyversity

public JMenuItem() Creates a JMenuItem with no set text or icon.

public JMenuItem(String text) Creates a JMenuItem with the specified text. Parameters: text - the text of the JMenuItem

public JMenuItem (Icon icn)public JMenuItem (String txt, Icon icr)

Presentation by Nyversity

Demo of JMenu Demo of JMenu 2