Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

MODULE-IV Complete Notes Java Events Handling Functions and LayoutManager-1

Uploaded by

shivakumark4648
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

MODULE-IV Complete Notes Java Events Handling Functions and LayoutManager-1

Uploaded by

shivakumark4648
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 52

MODULE-IV

PART-A
Java Events Handling Methods or Functions
 Event Handling is the mechanism that controls the event and decides what
should happen if an event occurs.
 This mechanism have the code which is known as event handler that is
executed when an event occurs.
 Java Uses the Delegation Event Model to handle the events.
 Changing the state of an object is known as an event.
 Example,: click on button, dragging mouse etc.
 The java.awt.event to declared in package to provides many event classes
and Listener Interfaces for event handling.

Types of Events or Classification Events:

A). Foreground Events


B). Background Events

A) Foreground Events:
Foreground events are the events that require user interaction to generate, i.e.,
foreground events are generated due to interaction by the user on components in
Graphic User Interface (GUI). Interactions are nothing but clicking on a button,
scrolling the scroll bar, cursor moments, etc.
B) . Background Events :
Events that don’t require interactions of users to generate are known as
background events. Examples of these events are operating system
failures/interrupts, operation completion, etc.

Delegations Event Model

Two types of Delegation Event Models :

A) Sources
B) Listeners

A). Source: Events are generated from the source. There are various sources like
buttons, checkboxes, list, menu-item, choice, scrollbar, text components,
windows, etc., to generate events.

B) Listeners: Listeners are used for handling the events generated from the
source. Each of these listeners represents interfaces that are responsible for
handling events.
Events Classes and Event Listerners

Event Class Event Listener Description


Interface

An event that indicates that a


component-defined action
ActionEvent ActionListener occurred like a button click or
selecting an item from the
menu-item list.

The adjustment event is


AdjustmentEvent AdjustmentListener emitted by an Adjustable
object like Scrollbar.

An event that indicates that a


component moved, the size
ComponentEvent ComponentListener
changed or changed its
visibility.

When a component is added


to a container (or) removed
ContainerEvent ContainerListener from it, then this event is
generated by a container
object.

These are focus-related


FocusEvent FocusListener events, which include focus,
focusin, focusout, and blur.

ItemEvent ItemListener An event that indicates


whether an item was selected
Event Class Event Listener Description
Interface

or not.

An event that occurs due to a


KeyEvent KeyListener sequence of keypresses on the
keyboard.

The events that occur due to


MouseListener &
MouseEvent the user interaction with the
MouseMotionListener
mouse (Pointing Device).

An event that specifies that


MouseWheelEvent MouseWheelListener the mouse wheel was rotated
in a component.

An event that occurs when an


TextEvent TextListener
object’s text changes.

An event which indicates


WindowEvent WindowListener whether a window has
changed its status or not.

Examples of some Registration Events Handling Classes


and Methods:
For registering the component with the Listener, many classes provide the
registration methods.
Examples:

 Button

public void addActionListener(ActionListener a)

// input output statements;

 MenuItem

public void addActionListener(ActionListener a)

// input output statements;

 TextField

public void addActionListener(ActionListener a)

// input output statements;

public void addTextListener(TextListener a)

// input output statements;


}

 TextArea

public void addTextListener(TextListener a)

// input output statements;

 Checkbox

public void addItemListener(ItemListener a)

// input output statements;

 Choice

public void addItemListener(ItemListener a)

// input output statements;

 List

public void addActionListener(ActionListener a)


{

// input output statements;

public void addItemListener(ItemListener a)

// input output statements;

Example: Write a java program how to create mouse event handling method.

import java.awt.*;
import java.awt.event.*;
public class ButtonE {
public static void main(String[] args)
{
Frame f = new Frame("Button Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
b.addActionListener(new ActionListener() // Mouse Event Handling Method
{
public void actionPerformed (ActionEvent e) // Action Event Method
{
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);
f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

OUTPUT:
Handling Mouse and Keyboard Events Interfaces

MouseListners Interface:
 The Java MouseListener is notified whenever you change the state of mouse.

 It is notified against MouseEvent.

 The MouseListener interface is found in java.awt.event package.

 It has five methods.

A. public abstract void mouseClicked(MouseEvent e);


B. public abstract void mouseEntered(MouseEvent e);
C. public abstract void mouseExited(MouseEvent e);
D. public abstract void mousePressed(MouseEvent e);
E. public abstract void mouseReleased(MouseEvent e);

Example : Write a java program how to implement


mouse Listeners events handling Interface
1. import java.awt.*;
2. import java.awt.event.*;
public class MouseListenerE extends Frame implements MouseListener{
3. Label l;
4. MouseListenerE(){
5. addMouseListener(this);
6.
7. l=new Label();
8. l.setBounds(20,50,100,20);
9. add(l);
10. setSize(300,300);
11. setLayout(null);
12. setVisible(true);
13. }
14. public void mouseClicked(MouseEvent e) {
15. l.setText("Mouse Clicked");
16. }
17. public void mouseEntered(MouseEvent e) {
18. l.setText("Mouse Entered");
19. }
20. public void mouseExited(MouseEvent e) {
21. l.setText("Mouse Exited");
22. }
23. public void mousePressed(MouseEvent e) {
24. l.setText("Mouse Pressed");
25. }
26. public void mouseReleased(MouseEvent e) {
27. l.setText("Mouse Released");
28. }
29. public static void main(String[] args) {
30. new MouseListenerE();
31. }
32. }
OUTPUT:

Example 2: Write a java program how to implement MouseMotion Event


Interface.

1. import java.awt.*;
2. import java.awt.event.*;
public class MouseMotionListenerE extends Frame implements
MouseMotionListener{
3. MouseMotionListenerE(){
4. addMouseMotionListener(this);
5. setSize(300,300);
6. setLayout(null);
7. setVisible(true);
8. }
9. public void mouseDragged(MouseEvent e) {
10. Graphics g=getGraphics();
11. g.setColor(Color.BLUE);
12. g.fillOval(e.getX(),e.getY(),20,20);
13. }
14. public void mouseMoved(MouseEvent e) {}
15. public static void main(String[] args) {
16. new MouseMotionListenerE();
17. }
18. }

OUTPOUT:
Java Key Board Events Interface

The Java KeyListener is notified whenever you change


the state of key.
It is notified against KeyEvent.
The KeyListener interface is found in java.awt.event
package, and it has three methods.
Syntax of keyboard event Interface
public interface KeyListener extends EventListener

Example :
public class KeyListener extends Frame implements Key
Listener

Following Methods of KeyListener interface :

Sl. Method name Description


no.

1. public abstract void keyPressed It is invoked when a key h


(KeyEvent e); been pressed.

2. public abstract void It is invoked when a key h


keyReleased (KeyEvent e); beenreleased.

3. public abstract void keyTyped It is invoked when a key


(KeyEvent e); has been typed.

Example: Write a java program how to implement


Key Boards Events handling functions.
1. import java.awt.*;
2. import java.awt.event.*;

// class which inherits Frame class and implements


KeyListener interface
3. public class KeyListenerExample2 extends Frame implem
ents KeyListener {
4. // object of Label and TextArea

5. Label l;
6. TextArea area;
7. // class constructor

8. KeyListenerExample2() {
9. // creating the label
10. l = new Label();
11. // setting the location of label
12. l.setBounds (20, 50, 200, 20);
13. // creating the text area

14. area = new TextArea();


15. // setting location of text area

16. area.setBounds (20, 80, 300, 300);


17. // adding KeyListener to the text area

18. area.addKeyListener(this);
19. // adding label and text area to frame

20. add(l);
21. add(area);

22. // setting size, layout and visibility of frame

23. setSize (400, 400);


24. setLayout (null);
25. setVisible (true);
26. }
27. public void keyPressed(KeyEvent e) {}
// Create
keyReleased() method of KeyListener interface
28. public void keyReleased (KeyEvent e) {
29. String text = area.getText();
30. // splitting the string in words

31. String words[] = text.split ("\\s");


32. // printing the number of words and characters of the s
tring
33. l.setText ("Words: " + words.length + " Characters:"
34. + text.length());

35. }
36. public void keyTyped(KeyEvent e) {}
37. // main method

38. public static void main(String[] args)


39. {

40. new KeyListenerExample2();


41. }
42. }

OUTPUT:
Java Adapter Classes
Java adapter classes provide the default implementation
of listener interfaces.
It assists the unrelated classes to work combined.
It provides ways to use classes in different ways.
It increases the transparency of classes.
It provides a way to include related patterns in the
class.
It provides a plug-gable kit for developing an
application.
It increases the re-usability of the class.
The adapter classes are declared in java.awt.event,
and javax.swing.event packages.
List of Adapter classes and Listener Interfaces

Adapter class Listener interface

WindowAdapter WindowListener

KeyAdapter KeyListener

MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener

FocusAdapter FocusListener

ComponentAdapter ComponentListener

ContainerAdapter ContainerListener

HierarchyBoundsAdapter HierarchyBoundsListener

Example : Write a java program how to create Adapter


Class
1. import java.awt.*;
2. import java.awt.event.*;

3. // class which inherits the MouseAdapter class

4. public class MAdapterE extends MouseAdapter {

5. // object of Frame class

6. Frame f;
7. // class constructor

8. MouseAdapterE() {
9. // creating the frame with the title

10. f = new Frame ("Mouse Adapter");


11. // adding MouseListener to the Frame

12. f.addMouseListener(this);
13. // setting the size, layout and visibility of the frame
14. f.setSize (300, 300);
15. f.setLayout (null);
16. f.setVisible (true);
17. }
18. // overriding the mouseClicked() method of the
MouseAdapter class
19. public void mouseClicked (MouseEvent e)

20. {

21. // creating the Graphics object and fetching them


from the Frame object using getGraphics() method
22. Graphics g = f.getGraphics();
23. // setting the color of graphics object

24. g.setColor (Color.BLUE);


25. // setting the shape of graphics object

26. g.fillOval (e.getX(), e.getY(), 30, 30);


27. }
28. // main method

29. public static void main(String[] args)

30. {

31. new MouseAdapterE();


32. }

33. }
OUTPUT:
Module-IV
PART-B
Java Layout Managers
 The LayoutManagers are used to arrange components in a particular
manner.
 The Java LayoutManagers facilitates us to control the positioning and
size of the components in GUI forms.
 Layout Managers is an interface that is implemented by all the
classes of layout managers.
 There are the following classes that represent the layout managers:

Types of Layout Mangers:


There are five different types of layout managers. They
are
A. Border Layout
B. Grid Layout
C. Flow Layout
D. Card Layout
E. Grid Bag Layout

A) . Border Layout:
 The BorderLayout is used to arrange the components in five
regions: north, south, east, west, and center.
 Each region (area) may contain one component only.
 It is the default layout of a frame or window.
 The BorderLayout provides five constants for each region:
1. public static final int NORTH
2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER
Constructors of BorderLayout class:

 BorderLayout(): creates a border layout but with no gaps


between the components.
 BorderLayout(int hgap, int vgap): creates a border layout
with the given horizontal and vertical gaps between the
components.

Example : Write a java program how to implement Border


layout
1. import java.awt.*;
2. import javax.swing.*;
3. public class Border
4. {
5. JFrame f;
6. Border()
7. {
8. f = new JFrame();
9. // creating buttons
JButton b1 = new JButton("NORTH");;
10. JButton b2 = new JButton("SOUTH");;
11. JButton b3 = new JButton("EAST");;
12. JButton b4 = new JButton("WEST");;
13. JButton b5 = new JButton("CENTER");;
14. f.add(b1, BorderLayout.NORTH);
15. f.add(b2, BorderLayout.SOUTH);
16. f.add(b3, BorderLayout.EAST);
17. f.add(b4, BorderLayout.WEST);
18. f.add(b5, BorderLayout.CENTER);
19. f.setSize(300, 300);
20. f.setVisible(true);
21. }
22. // Main method
23. public static void main(String[] args)
24. {
25. new Border();
26. }
27. }

OUTPUT:
B. Grid Layout :
 The Java GridLayout class is used to arrange the components in a
rectangular grid.
 One component is displayed in each rectangle.

Constructors of GridLayout class :


1. GridLayout(): creates a grid layout with one column per
component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the
given rows and columns but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a
grid layout with the given rows and columns along with given
horizontal and vertical gaps.

Example: Write a java how to implement grid layout


1. import java.awt.*;
2. import javax.swing.*;
3. public class MyGridLayout
4. {
5. JFrame f;
6. MyGridLayout()
7. {
8. f=new JFrame();
9. JButton b1=new JButton("1");
10. JButton b2=new JButton("2");
11. JButton b3=new JButton("3");
12. JButton b4=new JButton("4");
13. JButton b5=new JButton("5");
14. JButton b6=new JButton("6");
15. JButton b7=new JButton("7");
16. JButton b8=new JButton("8");
17. JButton b9=new JButton("9");
18. // adding buttons to the frame
19. f.add(b1);
20. f.add(b2);
21. f.add(b3);
22. f.add(b4);
23. f.add(b5);
24. f.add(b6);
25. f.add(b7);
26. f.add(b8);
27. f.add(b9);
28. // setting grid layout of 3 rows and 3 columns
29. f.setLayout(new GridLayout(3,3));
30. f.setSize(300,300);
31. f.setVisible(true);
32. }
33. public static void main(String[] args) {
34. new MyGridLayout();
35. }
36. }

OUTPUT:
C. Flow Layout:
The Java FlowLayout class is used to arrange the
components in a line, one after another (in a flow).
It is the default layout of the applet or panel.
Fields of FlowLayout class :
1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Constructors of FlowLayout class :
1. FlowLayout(): creates a flow layout with centered
alignment and a default 5 unit horizontal and vertical
gap.
2. FlowLayout(int align): creates a flow layout with
the given alignment and a default 5 unit horizontal
and vertical gap.
3. FlowLayout(int align, int hgap, int vgap): creates a
flow layout with the given alignment and the given
horizontal and vertical gap.
4.

Example : Write a java program how to create Flow


Layout.
1. import java.awt.*;
2. import javax.swing.*;
3. public class FlowLayoutE
4. {
5. JFrame frameObj;

6. FlowLayoutE()

7. {

8. // creating a frame object


9. frameObj = new JFrame();
10. // creating the buttons
11. JButton b1 = new JButton("1");
12. JButton b2 = new JButton("2");
13. JButton b3 = new JButton("3");
14. JButton b4 = new JButton("4");
15. JButton b5 = new JButton("5");
16. JButton b6 = new JButton("6");
17. JButton b7 = new JButton("7");
18. JButton b8 = new JButton("8");
19. JButton b9 = new JButton("9");
20. JButton b10 = new JButton("10");
21.

22.

23. // adding the buttons to frame


frameObj.add(b1);
24. frameObj.add(b2);

25. frameObj.add(b3);

26. frameObj.add(b4);

27. frameObj.add(b5);
28. frameObj.add(b6);
29. frameObj.add(b7);

30. frameObj.add(b8);

31. frameObj.add(b9);

32. frameObj.add(b10);

33. frameObj.setLayout(new FlowLayout());

34. frameObj.setSize(300, 300);

35. frameObj.setVisible(true);

36. }

37. public static void main(String argvs[])

38. {

39. new FlowLayoutExample();


40. }

41. }

OUTPUT:
D. Card Layout :
 The Java CardLayout class manages the components in such
a manner that only one component is visible at a time.
 It treats each component as a card that is why it is known as
CardLayout.

Constructors of Card Layout Class:

 CardLayout(): creates a card layout with zero horizontal and


vertical gap.
 CardLayout(int hgap, int vgap): creates a card layout with the
given horizontal and vertical gap

Example: Write a java program how to implement


Card Layout

1. import java.awt.*;
2. import java.awt.event.*;
3. import javax.swing.*;
public class CardLayoutE extends JFrame implements
ActionListener{
4. CardLayout card;
5. JButton b1,b2,b3;
6. Container c;
7. CardLayoutE(){

8. c=getContentPane();
9. card=new CardLayout(40,30);
10. //create CardLayout object with 40 hor space and 30
ver space
11. c.setLayout(card);
12. b1=new JButton("Apple");
13. b2=new JButton("Boy");
14. b3=new JButton("Cat");
15. b1.addActionListener(this);
16. b2.addActionListener(this);
17. b3.addActionListener(this);
18. c.add("a",b1);
19. c.add("b",b2);
20. c.add("c",b3);
21. }
22. public void actionPerformed(ActionEvent e)
23. card.next(c);
24. }
25. public static void main(String[] args) {
CardLayoutE cl=new CardLayoutE();
26. cl.setSize(400,400);
27. cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
28. } }
OUTPUT:
E. Grid Bag Layout:
 The Java GridBagLayout class is used to align
components vertically, horizontally or along their
baseline.
 The components may not be of the same size. Each
GridBagLayout object maintains a dynamic,
rectangular grid of cells.
 Each component occupies one or more cells known
as its display area.
 Each component associates an instance of
GridBagConstraints.
 With the help of the constraints object, we arrange
the component's display area on the grid.
 The GridBagLayout manages each component's
minimum and preferred sizes in order to determine
the component's size.
 GridBagLayout components are also arranged in the
rectangular grid but can have many different sizes
and can occupy multiple rows or columns.

Constructor:
GridBagLayout(): The parameterless constructor is used
to create a grid bag layout manager.

Example: Write a java program how to create Grid


Bag Layout.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
1. public class GridBagLayoutE extends JFrame{
2. public static void main(String[] args) {
3. GridBagLayoutE a = new GridBagLayoutE();
4. }
5. public GridBagLayoutE() {
6. GridBagLayoutgrid = new GridBagLayout();
7. GridBagConstraints gbc = new GridBagConstraints();
8. setLayout(grid);
9. setTitle("GridBag Layout Example");
10. GridBagLayout layout = new GridBagLayout();
11. this.setLayout(layout);
12. gbc.fill = GridBagConstraints.HORIZONTAL;
13. gbc.gridx = 0;
14. gbc.gridy = 0;
15. this.add(new Button("Button One"), gbc);
16. gbc.gridx = 1;
17. gbc.gridy = 0;
18. this.add(new Button("Button two"), gbc);
19. gbc.fill = GridBagConstraints.HORIZONTAL;
20. gbc.ipady = 20;
21. gbc.gridx = 0;
22. gbc.gridy = 1;
23. this.add(new Button("Button Three"), gbc);
24. gbc.gridx = 1;
25. gbc.gridy = 1;
26. this.add(new Button("Button Four"), gbc);
27. gbc.gridx = 0;
28. gbc.gridy = 2;
29. gbc.fill = GridBagConstraints.HORIZONTAL;
30. gbc.gridwidth = 2;
31. this.add(new Button("Button Five"), gbc);
32. setSize(300, 300);
33. setPreferredSize(getSize());
34. setVisible(true);
35. setDefaultCloseOperation(EXIT_ON_CLOSE);
36. }
37. }

OUTPUT:
Java Lists Panels
 The Java List Panel is a simplest container class.
 It provides space in which an application can attach
any other component.
 It inherits the Container class.
Types of Java Lists Panels:
There are four different types of lists Panels. They are
a). Scroll pane
b). Dialog pane
c). Menu bar pane
d). Graphic pane

a). scroll pane:


 A JscrollPane is used to make scrollable view of a
component.
 When screen size is limited, we use a scroll pane to
display a large component or a component whose
size can change dynamically.
Constructors :

Constructor/Method Purpose

JScrollPane() It creates a scroll pane. The Component


parameter,
J JScrollPane(Component)
when present, sets the scroll pane's client.
J JScrollPane(int, int) The two int parameters, when present,
set the vertical and horizontal scroll bar
JJScrollPane(Component, int,
int) policies (respectively).

Example : Write a java program how to implement


Scrollpaae

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JScrollPaneE {
private static final long serialVersionUID = 1L;
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("Scroll Pane Example");
// Display the window.
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_
CLOSE);
// set flow layout for the frame
frame.getContentPane().setLayout(new FlowLayout());
JTextArea textArea = new JTextArea(20, 20);
JScrollPane scrollableTextArea = new JScrollPane
(textArea);
scrollableTextArea.setHorizontalScrollBarPolicy(JScroll
Pane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollableTextArea.setVerticalScrollBarPolicy(JScrollPa
ne.VERTICAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scrollableTextArea);
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
createAndShowGUI();
});
}
}

OUTPUT:
b). Java Dialog Box Class:
The JDialog control represents a top level window with
a border and a title used to take some form of input
from the user.
It inherits the Dialog class.
Commonly used Constructors:

Constructor Description

JDialog() It is used to create a modelers


dialog without a title and without
a specified Frame owner.

JDialog(Frame owner) It is used to create a modelers


dialog with specified Frame as
its owner and an empty title.

JDialog(Frame owner, It is used to create a dialog


String title, boolean with the specified title, owner Frame
modal)
and modality.
Example: Write a java program how to implement
dialog pane.
1. import javax.swing.*;
2. import java.awt.*;

3. import java.awt.event.*;

4. public class DialogE {

5. public static JDialog d;

6. DialogE() {
7. JFrame f= new JFrame();
8. d = new JDialog(f , "Dialog Example", true);
9. d.setLayout( new FlowLayout() );
10. JButton b = new JButton ("OK");
11. b.addActionListener ( new ActionListener()
12. {
13. public void actionPerformed( ActionEvent e )
14. {
15. DialogE.d.setVisible(false);
16. }
17. });
18. d.add( new JLabel ("Click button to continue."));
19. d.add(b);
20. d.setSize(300,300);
21. d.setVisible(true);
22. }
23. public static void main(String args[])
24. {
25. new DialogE();
26. }
27. }
OUTPUT:

C. Menu Bar or JMenu or Jmenu items


The JMenuBar class is used to display menu bar 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 JMenu Item class.
The object of JMenu Item class adds a simple labeled
menu item.
The items used in a menu must belong to the
JMenuItem or any of its subclass.
JMenuBar class declaration
Syntax:
public class JMenuBar extends JComponent implements
MenuElement, Accessible
JMenu class declaration
Syntax:
public class JMenu extends JMenuItem implements
MenuElement, Accessible
JMenuItem class declaration
Syntax:
public class JMenuItem extends AbstractButton
implements Accessible, MenuElement
Example: Write a java program how to implement
Menu Bar or JMenu or JMenuItems
1. import javax.swing.*;

2. Import java.awt.*;

3. Import java.awt.event.:

4. class MenuE

5. {

6. JMenu menu, submenu;


7. JMenuItem i1, i2, i3, i4, i5;
8. MenuE(){
9. JFrame f= new JFrame("Menu and MenuItem
10. Example");
11. JMenuBar mb=new JMenuBar();
12. menu=new JMenu("Menu");
13. submenu=new JMenu("Sub Menu");
14. i1=new JMenuItem("Item 1");
15. i2=new JMenuItem("Item 2");
16. i3=new JMenuItem("Item 3");
17. i4=new JMenuItem("Item 4");
18. i5=new JMenuItem("Item 5");
19. menu.add(i1);
20. menu.add(i2);
21. menu.add(i3);
22. submenu.add(i4);
23. submenu.add(i5);
24. menu.add(submenu);
25. mb.add(menu);
26. f.setJMenuBar(mb);
27. f.setSize(400,400);
28. f.setLayout(null);
29. f.setVisible(true);
30. }
31. public static void main(String args[])

32. {

33. new MenuE();

34. }

35. }

OUTPUT:
D. Graphics Pane:
Java.awt.Graphics class provides many methods for
graphics programming in java.
Example: Write a java how to create a graphics.
1. import java.awt.*;
2. import javax.swing.
3. import java.awt.event.*;
4. public class DG extends Canvas{
5. public void paint(Graphics g) {

6. g.drawString("Hello", 40,40);

7. setBackground(Color.WHITE);

8. g.fillRect(130, 30,100, 80);

9. g.drawOval(30,130,50, 60);

10. setForeground(Color.RED);

11. g.fillOval(130,130,50, 60);

12. g.drawArc(30, 200, 40,50,90,60);

13. g.fillArc(30, 130, 40,50,180,40);

14. }
15. public static void main(String[] args)
16. {
17. DG m=new DG();
18. JFrame f=new JFrame();
19. f.add(m);
20. f.setSize(400,400);
21. //f.setLayout(null);
22. f.setVisible(true);
23. }
24. }

OUTPUT:

You might also like