More AWT - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

More AWT

Description:

Menu represents the drop-down menu attached to a menu bar. Instantiate a ... c2 = new Checkbox('whoa!'); c3 = new Checkbox('wow!'); Adding Checkboxes to Panel ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 29
Provided by: bas44
Category:
Tags: awt | more | whoa

less

Transcript and Presenter's Notes

Title: More AWT


1
More AWT
2
Menus in Java
  • Following three classes are used to create menus
    in java.
  • MenuBar
  • Menu
  • MenuItem

3
Java.awt.MenuBar
4
Java.awt.MenuBar
  • MenuBar is always added on top of the screen
  • java.awt.MenuBar is instantiated as follows
  • MenuBar mb new MenuBar()
  • Associate this menubar with a window
  • Frame f new Frame()
  • f. setMenuBar(mb)

5
Java.awt.Menu
  • Menu represents the drop-down menu attached to a
    menu bar
  • Instantiate a menu Item
  • Menu file new Menu("File")
  • Associate this Menu with the MenuBar
  • mb.add(file)

6
Java.awt.Menu
7
Java.awt.MenuItem
  • Every Item on a Menu is represented by an object
    of MenuItem class
  • Create Menu Item
  • MenuItem add new MenuItem("Add Item")
  • MenuItem exit new MenuItem(Exit)
  • Associating these items with Menu
  • file.add(add)
  • file.add(exit)

8
Java.awt.MenuItem
9
Making Menu Item work
  • You can add an actionListener to this menu Item
    to make it work.
  • Code written in ActionPerformed is executed
    whenever this menu item is clicked

10
java.awt.Panel
  • Panels are dependent containers
  • You can add other components to Panels
  • You can set Layout of Panels
  • Panels can eventually be added to other
    Containers (including Panels)

11
Panels
  • Creating Panels
  • Panel p new Panel()
  • Setting Layout
  • p.setLayout(new FlowLayout())
  • Adding Components to a Panel
  • p.add( new Button(OK))
  • Adding panel to the frame
  • Frame f new Frame()
  • f.setLayout(new BorderLayout())
  • f.add(p, BorderLayout.CENTER)

12
  • This screen has 2 panels.
  • One on the East, and the other on the south

13
Checkbox
  • java.awt.Checkbox represents an on-screen checkbox

14
Checkbox
  • Creating Checkboxes
  • Checkbox c1, c2, c3
  • c1 new Checkbox("hey!")
  • c2 new Checkbox("whoa!")
  • c3 new Checkbox("wow!")

15
Adding Checkboxes to Panel
  • Panel east new Panel()
  • east.setLayout(new FlowLayout())
  • east.add(c1)
  • east.add(c2)
  • east.add(c3)

16
Adding the Panel to Frame
  • Frame f new Frame()
  • f.setLayout(new BorderLayout())
  • f.add(east, BorderLayout.EAST)

17
Resulting Screen
18
Checkbox Groups
  • A number of Checkboxes can be grouped into
    Checkbox Groups
  • Only one checkbox can be in a CHECKED state when
    associated with a checkbox group

19
Checkbox Group
  • Only one element Checked
  • Also known as Radio Button

20
Creating Checkbox Group
  • CheckboxGroup cbg new CheckboxGroup()

21
Associating Checkboxes with CheckboxGroup
  • Checkbox c4, c5, c6
  • c4 new Checkbox("yes", cbg, true)
  • c5 new Checkbox("no", cbg, true)
  • c6 new Checkbox("may be", cbg, true)

22
Adding to Panel
  • Panel south new Panel()
  • south.setLayout(new FlowLayout())
  • south.add(c4 )
  • south.add(c5)
  • south.add(c6)
  • Frame f new Frame()
  • f.add(south, BorderLayout.SOUTH)

23
Resulting Screen
24
List
  • Class java.awt.List Represents the List-Boxes
  • Creating a ListList myList new List()
  • Then simply add this list to the Frame
  • f.add(myList, BorderLayout.CENTER)

25
Adding Elements to the List
  • l.add(Message 1")
  • l.add(Message 2")
  • l.add(Message 3")
  • l.add(Message 4")

26
Resulting Screen
27
Try-out Making This Screen
28
Try-out Making This Screen
  • Requirements
  • Add a List at CENTER
  • Add a Label at WEST
  • Add a Panel with 3 checkboxes at EAST
  • Add a Panel with 3 checkboxes (in one group) at
    SOUTH
  • Add MenuBar with one FILE menu having two Items
    i.e Add Item and EXIT
  • Clicking Add Item should add a string to list
  • Clicking Exit should EXIT the program
Write a Comment
User Comments (0)
About PowerShow.com