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

Java AWT

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

Java AWT

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

Java AWT Checkbox

The Checkbox 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".

AWT Checkbox Class Declaration


1. public class Checkbox extends Component implements ItemSelectable, Accessible

Checkbox Class Constructors

Sr. Constructor
no.

1. Checkbox() It constructs a checkbox with no string as the label.

2. Checkbox(String label) It constructs a checkbox with the given label.

3. Checkbox(String label, boolean state) It constructs a checkbox with the given label and sets the given
state.

4. Checkbox(String label, boolean state, It constructs a checkbox with the given label, set the given state
CheckboxGroup group) in the specified checkbox group.

5. Checkbox(String label, CheckboxGroup It constructs a checkbox with the given label, in the given
group, boolean state) checkbox group and set to the specified state.

Method inherited by Checkbox


The methods of Checkbox class are inherited by following classes:

o java.awt.Component
o java.lang.Object

Checkbox Class Methods

Sr. Method name Description


no.

1. void addItemListener(ItemListener IL) It adds the given item listener to get the item events from the
checkbox.

2. AccessibleContext It fetches the accessible context of checkbox.


getAccessibleContext()

3. void addNotify() It creates the peer of checkbox.

4. CheckboxGroup getCheckboxGroup() It determines the group of checkbox.

5. ItemListener[] getItemListeners() It returns an array of the item listeners registered on checkbox.

6. String getLabel() It fetched the label of checkbox.


7. T[] getListeners(Class listenerType) It returns an array of all the objects registered as FooListeners.

8. Object[] getSelectedObjects() It returns an array (size 1) containing checkbox label and returns
null if checkbox is not selected.

9. boolean getState() It returns true if the checkbox is on, else returns off.

10. protected String paramString() It returns a string representing the state of checkbox.

11. protected void processEvent(AWTEvent It processes the event on checkbox.


e)

12. protected void It process the item events occurring in the checkbox by
processItemEvent(ItemEvent e) dispatching them to registered ItemListener object.

13. void removeItemListener(ItemListener l) It removes the specified item listener so that the item listener
doesn't receive item events from the checkbox anymore.

14. void setCheckboxGroup(CheckboxGroup It sets the checkbox's group to the given checkbox.
g)

15. void setLabel(String label) It sets the checkbox's label to the string argument.

16. void setState(boolean state) It sets the state of checkbox to the specified state.

Java AWT Checkbox Example


In the following example we are creating two checkboxes using the Checkbox(String label) constructo and adding them into the
Frame using add() method.
CheckboxExample1.javallscreen

1. // importing AWT class


2. import java.awt.*;
3. public class CheckboxExample1
4. {
5. // constructor to initialize
6. CheckboxExample1() {
7. // creating the frame with the title
8. Frame f = new Frame("Checkbox Example");
9. // creating the checkboxes
10. Checkbox checkbox1 = new Checkbox("C++");
11. checkbox1.setBounds(100, 100, 50, 50);
12. Checkbox checkbox2 = new Checkbox("Java", true);
13. // setting location of checkbox in frame
14. checkbox2.setBounds(100, 150, 50, 50);
15. // adding checkboxes to frame
16. f.add(checkbox1);
17. f.add(checkbox2);
18.
19. // setting size, layout and visibility of frame
20. f.setSize(400,400);
21. f.setLayout(null);
22. f.setVisible(true);
23. }
24. // main method
25. public static void main (String args[])
26. {
27. new CheckboxExample1();
28. }
29. }

Output:

Java AWT Checkbox Example with ItemListener


In the following example, we have created two checkboxes and adding them into the Frame. Here, we are adding the ItemListener
with the checkbox which displays the state of the checkbox (whether it is checked or unchecked) using the getStateChange()
method.
CheckboxExample2.java

1. // importing necessary packages


2. import java.awt.*;
3. import java.awt.event.*;
4. public class CheckboxExample2
5. {
6. // constructor to initialize
7. CheckboxExample2() {
8. // creating the frame
9. Frame f = new Frame ("CheckBox Example");
10. // creating the label
11. final Label label = new Label();
12. // setting the alignment, size of label
13. label.setAlignment(Label.CENTER);
14. label.setSize(400,100);
15. // creating the checkboxes
16. Checkbox checkbox1 = new Checkbox("C++");
17. checkbox1.setBounds(100, 100, 50, 50);
18. Checkbox checkbox2 = new Checkbox("Java");
19. checkbox2.setBounds(100, 150, 50, 50);
20. // adding the checkbox to frame
21. f.add(checkbox1);
22. f.add(checkbox2);
23. f.add(label);
24.
25. // adding event to the checkboxes
26. checkbox1.addItemListener(new ItemListener() {
27. public void itemStateChanged(ItemEvent e) {
28. label.setText("C++ Checkbox: "
29. + (e.getStateChange()==1?"checked":"unchecked"));
30. }
31. });
32. checkbox2.addItemListener(new ItemListener() {
33. public void itemStateChanged(ItemEvent e) {
34. label.setText("Java Checkbox: "
35. + (e.getStateChange()==1?"checked":"unchecked"));
36. }
37. });
38. // setting size, layout and visibility of frame
39. f.setSize(400,400);
40. f.setLayout(null);
41. f.setVisible(true);
42. }
43. // main method
44. public static void main(String args[])
45. {
46. new CheckboxExample2();
47. }
48. }
Output:

Java AWT CheckboxGroup


The object of CheckboxGroup class is used to group together a set of Checkbox. At a time only one check box button is allowed to
be in "on" state and remaining check box button in "off" state. It inherits the object class.

Note: CheckboxGroup enables you to create radio buttons in AWT. There is no special control for creating radio buttons in AWT.

AWT CheckboxGroup Class Declaration


1. public class CheckboxGroup extends Object implements Serializable
Java AWT CheckboxGroup Example
1. import java.awt.*;
2. public class CheckboxGroupExample
3. {
4. CheckboxGroupExample(){
5. Frame f= new Frame("CheckboxGroup Example");
6. CheckboxGroup cbg = new CheckboxGroup();
7. Checkbox checkBox1 = new Checkbox("C++", cbg, false);
8. checkBox1.setBounds(100,100, 50,50);
9. Checkbox checkBox2 = new Checkbox("Java", cbg, true);
10. checkBox2.setBounds(100,150, 50,50);
11. f.add(checkBox1);
12. f.add(checkBox2);
13. f.setSize(400,400);
14. f.setLayout(null);
15. f.setVisible(true);
16. }
17. public static void main(String args[])
18. {
19. new CheckboxGroupExample();
20. }
21. }

Output:
Java AWT CheckboxGroup Example with ItemListener
1. import java.awt.*;
2. import java.awt.event.*;
3. public class CheckboxGroupExample
4. {
5. CheckboxGroupExample(){
6. Frame f= new Frame("CheckboxGroup Example");
7. final Label label = new Label();
8. label.setAlignment(Label.CENTER);
9. label.setSize(400,100);
10. CheckboxGroup cbg = new CheckboxGroup();
11. Checkbox checkBox1 = new Checkbox("C++", cbg, false);
12. checkBox1.setBounds(100,100, 50,50);
13. Checkbox checkBox2 = new Checkbox("Java", cbg, false);
14. checkBox2.setBounds(100,150, 50,50);
15. f.add(checkBox1); f.add(checkBox2); f.add(label);
16. f.setSize(400,400);
17. f.setLayout(null);
18. f.setVisible(true);
19. checkBox1.addItemListener(new ItemListener() {
20. public void itemStateChanged(ItemEvent e) {
21. label.setText("C++ checkbox: Checked");
22. }
23. });
24. checkBox2.addItemListener(new ItemListener() {
25. public void itemStateChanged(ItemEvent e) {
26. label.setText("Java checkbox: Checked");
27. }
28. });
29. }
30. public static void main(String args[])
31. {
32. new CheckboxGroupExample();
33. }
34. }
Output:
Java AWT Choice
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 Component class.

AWT Choice Class Declaration


1. public class Choice extends Component implements ItemSelectable, Accessible

Choice Class constructor

Sr. no. Constructor Description

1. Choice() It constructs a new choice menu.

Methods inherited by class


The methods of Choice class are inherited by following classes:

o java.awt.Component
o java.lang.Object

Choice Class Methods


Sr. Method name Description
no.

1. void add(String item) It adds an item to the choice menu.

2. void addItemListener(ItemListener l) It adds the item listener that receives item events from the choice
menu.

3. void addNotify() It creates the peer of choice.

4. AccessibleContext It gets the accessbile context related to the choice.


getAccessibleContext()

5. String getItem(int index) It gets the item (string) at the given index position in the choice
menu.

6. int getItemCount() It returns the number of items of the choice menu.

7. ItemListener[] getItemListeners() It returns an array of all item listeners registered on choice.

8. T[] getListeners(Class listenerType) Returns an array of all the objects currently registered as
FooListeners upon this Choice.

9. int getSelectedIndex() Returns the index of the currently selected item.

10. String getSelectedItem() Gets a representation of the current choice as a string.

11. Object[] getSelectedObjects() Returns an array (length 1) containing the currently selected item.

12. void insert(String item, int index) Inserts the item into this choice at the specified position.
13. protected String paramString() Returns a string representing the state of this Choice menu.

14. protected void It processes the event on the choice.


processEvent(AWTEvent e)

15. protected void processItemEvent Processes item events occurring on this Choice menu by dispatching
(ItemEvent e) them to any registered ItemListener objects.

16. void remove(int position) It removes an item from the choice menu at the given index
position.

17. void remove(String item) It removes the first occurrence of the item from choice menu.

18. void removeAll() It removes all the items from the choice menu.

19. void removeItemListener (ItemListener It removes the mentioned item listener. Thus is doesn't receive item
l) events from the choice menu anymore.

20. void select(int pos) It changes / sets the selected item in the choice menu to the item at
given index position.

21. void select(String str) It changes / sets the selected item in the choice menu to the item
whose string value is equal to string specified in the argument.

Java AWT Choice Example


In the following example, we are creating a choice menu using Choice() constructor. Then we add 5 items to the menu using add()
method and Then add the choice menu into the Frame.
ChoiceExample1.java

1. // importing awt class


2. import java.awt.*;
3. public class ChoiceExample1 {
4.
5. // class constructor
6. ChoiceExample1() {
7.
8. // creating a frame
9. Frame f = new Frame();
10.
11. // creating a choice component
12. Choice c = new Choice();
13.
14. // setting the bounds of choice menu
15. c.setBounds(100, 100, 75, 75);
16.
17. // adding items to the choice menu
18. c.add("Item 1");
19. c.add("Item 2");
20. c.add("Item 3");
21. c.add("Item 4");
22. c.add("Item 5");
23.
24. // adding choice menu to frame
25. f.add(c);
26.
27. // setting size, layout and visibility of frame
28. f.setSize(400, 400);
29. f.setLayout(null);
30. f.setVisible(true);
31. }
32.
33. // main method
34. public static void main(String args[])
35. {
36. new ChoiceExample1();
37. }
38. }

Output:
Java AWT Choice Example with ActionListener
In the following example, we are creating a choice menu with 5 items. Along with that we are creating a button and a label. Here,
we are adding an event to the button component using addActionListener(ActionListener a) method i.e. the selected item from
the choice menu is displayed on the label when the button is clicked.

ChoiceExample2.java

1. // importing necessary packages


2. import java.awt.*;
3. import java.awt.event.*;
4.
5. public class ChoiceExample2 {
6.
7. // class constructor
8. ChoiceExample2() {
9.
10. // creating a frame
11. Frame f = new Frame();
12.
13. // creating a final object of Label class
14. final Label label = new Label();
15.
16. // setting alignment and size of label component
17. label.setAlignment(Label.CENTER);
18. label.setSize(400, 100);
19.
20. // creating a button
21. Button b = new Button("Show");
22.
23. // setting the bounds of button
24. b.setBounds(200, 100, 50, 20);
25.
26. // creating final object of Choice class
27. final Choice c = new Choice();
28.
29. // setting bounds of choice menu
30. c.setBounds(100, 100, 75, 75);
31.
32. // adding 5 items to choice menu
33. c.add("C");
34. c.add("C++");
35. c.add("Java");
36. c.add("PHP");
37. c.add("Android");
38.
39. // adding above components into the frame
40. f.add(c);
41. f.add(label);
42. f.add(b);
43.
44. // setting size, layout and visibility of frame
45. f.setSize(400, 400);
46. f.setLayout(null);
47. f.setVisible(true);
48.
49. // adding event to the button
50. // which displays the selected item from the list when button is clicked
51. b.addActionListener(new ActionListener() {
52. public void actionPerformed(ActionEvent e) {
53. String data = "Programming language Selected: "+ c.getItem(c.getSelectedIndex());
54. label.setText(data);
55. }
56. });
57. }
58.
59. // main method
60. public static void main(String args[])
61. {
62. new ChoiceExample2();
63. }
64. }

Output:

Java AWT List


The object of List class represents a list of text items. With the help of the List class, user can choose either one item or multiple
items. It inherits the Component class.

AWT List class Declaration


1. public class List extends Component implements ItemSelectable, Accessible

AWT List Class Constructors

Sr. Constructor Description


no.

1. List() It constructs a new scrolling list.

2. List(int row_num) It constructs a new scrolling list initialized with the given number of
rows visible.

3. List(int row_num, Boolean It constructs a new scrolling list initialized which displays the given
multipleMode) number of rows.

Methods Inherited by the List Class


The List cl ass methods are inherited by following classes:

o java.awt.Component
o java.lang.Object
List Class Methods

Sr. Method name Description


no.

1. void add(String item) It adds the specified item into the end of scrolling list.

2. void add(String item, int index) It adds the specified item into list at the given index position.

3. void addActionListener(ActionListener l) It adds the specified action listener to receive action events
from list.

4. void addItemListener(ItemListener l) It adds specified item listener to receive item events from list.

5. void addNotify() It creates peer of list.

6. void deselect(int index) It deselects the item at given index position.

7. AccessibleContext getAccessibleContext() It fetches the accessible context related to the list.

8. ActionListener[] getActionListeners() It returns an array of action listeners registered on the list.

9. String getItem(int index) It fetches the item related to given index position.

10. int getItemCount() It gets the count/number of items in the list.

11. ItemListener[] getItemListeners() It returns an array of item listeners registered on the list.

12. String[] getItems() It fetched the items from the list.


13. Dimension getMinimumSize() It gets the minimum size of a scrolling list.

14. Dimension getMinimumSize(int rows) It gets the minimum size of a list with given number of rows.

15. Dimension getPreferredSize() It gets the preferred size of list.

16. Dimension getPreferredSize(int rows) It gets the preferred size of list with given number of rows.

17. int getRows() It fetches the count of visible rows in the list.

18. int getSelectedIndex() It fetches the index of selected item of list.

19. int[] getSelectedIndexes() It gets the selected indices of the list.

20. String getSelectedItem() It gets the selected item on the list.

21. String[] getSelectedItems() It gets the selected items on the list.

22. Object[] getSelectedObjects() It gets the selected items on scrolling list in array of objects.

23. int getVisibleIndex() It gets the index of an item which was made visible by method
makeVisible()

24. void makeVisible(int index) It makes the item at given index visible.

25. boolean isIndexSelected(int index) It returns true if given item in the list is selected.

26. boolean isMultipleMode() It returns the true if list allows multiple selections.

27. protected String paramString() It returns parameter string representing state of the scrolling
list.
28. protected void It process the action events occurring on list by dispatching
processActionEvent(ActionEvent e) them to a registered ActionListener object.

29. protected void processEvent(AWTEvent e) It process the events on scrolling list.

30. protected void processItemEvent(ItemEvent It process the item events occurring on list by dispatching
e) them to a registered ItemListener object.

31. void removeActionListener(ActionListener l) It removes specified action listener. Thus it doesn't receive
further action events from the list.

32. void removeItemListener(ItemListener l) It removes specified item listener. Thus it doesn't receive
further action events from the list.

33. void remove(int position) It removes the item at given index position from the list.

34. void remove(String item) It removes the first occurrence of an item from list.

35. void removeAll() It removes all the items from the list.

36. void replaceItem(String newVal, int index) It replaces the item at the given index in list with the new
string specified.

37. void select(int index) It selects the item at given index in the list.

38. void setMultipleMode(boolean b) It sets the flag which determines whether the list will allow
multiple selection or not.

39. void removeNotify() It removes the peer of list.


Java AWT List Example
In the following example, we are creating a List component with 5 rows and adding it into the Frame.

ListExample1.java

1. // importing awt class


2. import java.awt.*;
3.
4. public class ListExample1
5. {
6. // class constructor
7. ListExample1() {
8. // creating the frame
9. Frame f = new Frame();
10. // creating the list of 5 rows
11. List l1 = new List(5);
12.
13. // setting the position of list component
14. l1.setBounds(100, 100, 75, 75);
15.
16. // adding list items into the list
17. l1.add("Item 1");
18. l1.add("Item 2");
19. l1.add("Item 3");
20. l1.add("Item 4");
21. l1.add("Item 5");
22.
23. // adding the list to frame
24. f.add(l1);
25.
26. // setting size, layout and visibility of frame
27. f.setSize(400, 400);
28. f.setLayout(null);
29. f.setVisible(true);
30. }
31.
32. // main method
33. public static void main(String args[])
34. {
35. new ListExample1();
36. }
37. }
38. Output:
39.

Java AWT List Example with ActionListener


In the following example, we are creating two List components, a Button and a Label and adding them into the frame. Here, we are
generating an event on the button using the addActionListener(ActionListener l) method. On clicking the button, it displays the
selected programming language and the framework.

ListExample2.java

1. // importing awt and event class


2. import java.awt.*;
3. import java.awt.event.*;
4.
5. public class ListExample2
6. {
7. // class constructor
8. ListExample2() {
9. // creating the frame
10. Frame f = new Frame();
11. // creating the label which is final
12. final Label label = new Label();
13.
14. // setting alignment and size of label
15. label.setAlignment(Label.CENTER);
16. label.setSize(500, 100);
17.
18. // creating a button
19. Button b = new Button("Show");
20.
21. // setting location of button
22. b.setBounds(200, 150, 80, 30);
23.
24. // creating the 2 list objects of 4 rows
25. // adding items to the list using add()
26. // setting location of list components
27. final List l1 = new List(4, false);
28. l1.setBounds(100, 100, 70, 70);
29. l1.add("C");
30. l1.add("C++");
31. l1.add("Java");
32. l1.add("PHP");
33.
34.
35. final List l2=new List(4, true);
36. l2.setBounds(100, 200, 70, 70);
37. l2.add("Turbo C++");
38. l2.add("Spring");
39. l2.add("Hibernate");
40. l2.add("CodeIgniter");
41.
42. // adding List, Label and Button to the frame
43. f.add(l1);
44. f.add(l2);
45. f.add(label);
46. f.add(b);
47.
48. // setting size, layout and visibility of frame
49. f.setSize(450,450);
50. f.setLayout(null);
51. f.setVisible(true);
52.
53. // generating event on the button
54. b.addActionListener(new ActionListener() {
55. public void actionPerformed(ActionEvent e) {
56. String data = "Programming language Selected: "+l1.getItem(l1.getSelectedIndex());
57. data += ", Framework Selected:";
58. for(String frame:l2.getSelectedItems()) {
59. data += frame + " ";
60. }
61. label.setText(data);
62. }
63. });
64. }
65.
66. // main method
67. public static void main(String args[])
68. {
69. new ListExample2();
70. }
71. }

Output:
72.

Java AWT Canvas


The Canvas class controls and represents a blank rectangular area where the application can draw or trap input events from the
user. It inherits the Component class.

AWT Canvas class Declaration


1. public class Canvas extends Component implements Accessible

Canvas Class Constructors

Sr. Constructor Description


no.

1. Canvas() It constructs a new Canvas.

2. Canvas(GraphicConfiguration It constructs a new Canvas with the given Graphic Configuration


config) object.

Class methods

Sr. Method name Description


no.

1. void addNotify() It creates the canvas's peer.

2. void createBufferStrategy (int numBuffers) It creates a new multi buffering strategies on the particular
component.
3. void createBufferStrategy (int numBuffers, It creates a new multi buffering strategies on the particular
BufferCapabilities caps) component with the given buffer capabilities.

4. AccessibleContext getAccessibleContext() It gets the accessible context related to the Canvas.

5. BufferStrategy getBufferStrategy() It returns the buffer strategy used by the particular


component.

6. void paint(Graphics g) It paints the canvas with given Graphics object.

7. void pdate(Graphics g) It updates the canvas with given Graphics object.

Method Inherited by Canvas Class


The Canvas has inherited above methods from the following classes:

o lang.Component
o lang.Object

Java AWT Canvas Example


In the following example, we are creating a Canvas in the Frame and painting a red colored oval inside it.

CanvasExample.java

1. // importing awt class


2. import java.awt.*;
3.
4. // class to construct a frame and containing main method
5. public class CanvasExample
6. {
7. // class constructor
8. public CanvasExample()
9. {
10.
11. // creating a frame
12. Frame f = new Frame("Canvas Example");
13. // adding canvas to frame
14. f.add(new MyCanvas());
15.
16. // setting layout, size and visibility of frame
17. f.setLayout(null);
18. f.setSize(400, 400);
19. f.setVisible(true);
20. }
21.
22. // main method
23. public static void main(String args[])
24. {
25. new CanvasExample();
26. }
27. }
28.
29. // class which inherits the Canvas class
30. // to create Canvas
31. class MyCanvas extends Canvas
32. {
33. // class constructor
34. public MyCanvas() {
35. setBackground (Color.GRAY);
36. setSize(300, 200);
37. }
38.
39. // paint() method to draw inside the canvas
40. public void paint(Graphics g)
41. {
42.
43. // adding specifications
44. g.setColor(Color.red);
45. g.fillOval(75, 75, 150, 75);
46. }
47. }

Output:
Java AWT MenuItem and Menu
The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem
or any of its subclass.

The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.

AWT MenuItem class declaration


1. public class MenuItem extends MenuComponent implements Accessible

AWT Menu class declaration


1. public class Menu extends MenuItem implements MenuContainer, Accessible

Java AWT MenuItem and Menu Example


import java.awt.*;

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

30.
Java AWT PopupMenu
PopupMenu can be dynamically popped up at specific position within a component. It inherits the Menu class.

AWT PopupMenu class declaration


1. public class PopupMenu extends Menu implements MenuContainer, Accessible

Java AWT PopupMenu Example


1. import java.awt.*;
2. import java.awt.event.*;
3. class PopupMenuExample
4. {
5. PopupMenuExample(){
6. final Frame f= new Frame("PopupMenu Example");
7. final PopupMenu popupmenu = new PopupMenu("Edit");
8. MenuItem cut = new MenuItem("Cut");
9. cut.setActionCommand("Cut");
10. MenuItem copy = new MenuItem("Copy");
11. copy.setActionCommand("Copy");
12. MenuItem paste = new MenuItem("Paste");
13. paste.setActionCommand("Paste");
14. popupmenu.add(cut);
15. popupmenu.add(copy);
16. popupmenu.add(paste);
17. f.addMouseListener(new MouseAdapter() {
18. public void mouseClicked(MouseEvent e) {
19. popupmenu.show(f , e.getX(), e.getY());
20. }
21. });
22. f.add(popupmenu);
23. f.setSize(400,400);
24. f.setLayout(null);
25. f.setVisible(true);
26. }
27. public static void main(String args[])
28. {
29. new PopupMenuExample();
30. }
31. }
JAVA AWT PANEL
The Panel is a simplest container class. It provides space in which an application can attach any other component. It inherits the
Container class.

It doesn't have title bar.

AWT PANEL CLASS DECLARATION


1. public class Panel extends Container implements Accessible

JAVA AWT PANEL EXAMPLE


1. import java.awt.*;
2. public class PanelExample {
3. PanelExample()
4. {
5. Frame f= new Frame("Panel Example");
6. Panel panel=new Panel();
7. panel.setBounds(40,80,200,200);
8. panel.setBackground(Color.gray);
9. Button b1=new Button("Button 1");
10. b1.setBounds(50,100,80,30);
11. b1.setBackground(Color.yellow);
12. Button b2=new Button("Button 2");
13. b2.setBounds(100,100,80,30);
14. b2.setBackground(Color.green);
15. panel.add(b1); panel.add(b2);
16. f.add(panel);
17. f.setSize(400,400);
18. f.setLayout(null);
19. f.setVisible(true);
20. }
21. public static void main(String args[])
22. {
23. new PanelExample();
24. }
25. }

Output:
JAVA AWT DIALOG
The Dialog 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 Window class.

Unlike Frame, it doesn't have maximize and minimize buttons.

FRAME VS DIALOG

Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog doesn't have.

AWT DIALOG CLASS DECLARATION


1. public class Dialog extends Window

JAVA AWT DIALOG EXAMPLE


1. import java.awt.*;
2. import java.awt.event.*;
3. public class DialogExample {
4. private static Dialog d;
5. DialogExample() {
6. Frame f= new Frame();
7. d = new Dialog(f , "Dialog Example", true);
8. d.setLayout( new FlowLayout() );
9. Button b = new Button ("OK");
10. b.addActionListener ( new ActionListener()
11. {
12. public void actionPerformed( ActionEvent e )
13. {
14. DialogExample.d.setVisible(false);
15. }
16. });
17. d.add( new Label ("Click button to continue."));
18. d.add(b);
19. d.setSize(300,300);
20. d.setVisible(true);
21. }
22. public static void main(String args[])
23. {
24. new DialogExample();
25. }
26. }

Output:

27.
JAVA AWT TOOLKIT
Toolkit class is the abstract superclass of every implementation in the Abstract Window Toolkit. Subclasses of Toolkit are used to
bind various components. It inherits Object class.

AWT TOOLKIT CLASS DECLARATION


1. public abstract class Toolkit extends Object

JAVA AWT TOOLKIT EXAMPLE


1. import java.awt.*;
2. public class ToolkitExample {
3. public static void main(String[] args) {
4. Toolkit t = Toolkit.getDefaultToolkit();
5. System.out.println("Screen resolution = " + t.getScreenResolution());
6. Dimension d = t.getScreenSize();
7. System.out.println("Screen width = " + d.width);
8. System.out.println("Screen height = " + d.height);
9. }
10. }

Output:

Screen resolution = 96
Screen width = 1366
Screen height = 768
JAVA AWT TOOLKIT EXAMPLE: BEEP()
1. import java.awt.event.*;
2. public class ToolkitExample {
3. public static void main(String[] args) {
4. Frame f=new Frame("ToolkitExample");
5. Button b=new Button("beep");
6. b.setBounds(50,100,60,30);
7. f.add(b);
8. f.setSize(300,300);
9. f.setLayout(null);
10. f.setVisible(true);
11. b.addActionListener(new ActionListener(){
12. public void actionPerformed(ActionEvent e){
13. Toolkit.getDefaultToolkit().beep();
14. }
15. });
16. }
17. }

Output:
JAVA AWT TOOLKIT EXAMPLE: CHANGE TITLEBAR ICON
1. import java.awt.*;
2. class ToolkitExample {
3. ToolkitExample(){
4. Frame f=new Frame();
5. Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
6. f.setIconImage(icon);
7. f.setLayout(null);
8. f.setSize(400,400);
9. f.setVisible(true);
10. }
11. public static void main(String args[]){
12. new ToolkitExample();
13. }
14. }

Output:

Output:

You might also like