0% found this document useful (0 votes)
8 views23 pages

Week2A 03april2024 JMenu and ActionEvents

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Week2A 03april2024 JMenu and ActionEvents

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

03 April 2024

Radford Burger
Agenda
 GUI: Re-cap
 LayoutManagers
 Menus
 Inner class for event-handling
Gui: Recap
Every user interface considers the
following three main aspects −

•UI Elements − These are the core visual


elements the user eventually sees and
interacts with.

•Layouts − They define how UI elements


should be organized on the screen and
provide a final look and feel to the GUI
(Graphical User Interface).

•Behavior − These are the events which


occur when the user interacts with UI
elements.
Gui: Recap

Every SWING controls inherits properties from the following Component class hiearchy.
Gui: Recap
list of commonly used controls while designing GUI using SWING:

Class Description
JLabel
Class Placing text in a container
Description
JButton
JLabel
JCheckBox
JButton
JRadioButton
JCheckBox
JColorChooser
JRadioButton
JTextField
JColorChooser Editing of single line of text

JTextArea Editing of multiple lines of text

JComboBox Dropdown menu of options

Jlist Scrolling list of text items

JScrollBar Allows user to scroll through a range of


values
Gui: Layouts
Layout refers to the arrangement of components within
the container. In another way, it could be said that layout
is placing the components at a particular position within
the container.

The layout manager automatically positions all the


components within the container
Gui: Layouts
list of commonly used layout managers:

Class description
BorderLayout Arranges components to fit in 5 regions
GridLayout Places components in a rectangular grid
FlowLayout Arranges components in a directional flow
CardLayout Treats each component as a card. Only 1
card is visible at a time
GridBagLayout Aligns components vertically or
horizontally, or along their baseline
Gui: Menus
As we know that every top-level window has a menu
bar associated with it.
This menu bar consists of various menu choices
available to the end user.
Further, each choice contains a list of items or options,
which is called menu items.
Gui: Menus
list of menu controls:

Class description
JMenuBar Top-level window
JMenu Pull-down menu component displayed
from the menubar
JMenuItem Available options in the pull-down menu
Gui: Menus
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame
2. Create JMenu object
3. Create JMenuItem objects and attach to JMenu
object
4. Attach JMenu object to JMenuBar object
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame

JMenuBar mBar = new JMenuBar( );


setJMenuBar(mBar);
Sequence of steps to create
menus
1. Create JMenuBar object and attach to frame
2. Create JMenu object

JMenu fileMenu = new JMenu(“File”);


Sequence of steps to create
menus
3. Attach JMenu object to JMenuBar object

mBar.add(fileMenu);
Sequence of steps to create
menus
4. Create JMenuItem objects and attach to JMenu
object
JMenuItem newItem = new JMenuItem(“New record”);
JMenuItem updateItem = new JMenuItem(“Update
record”);
fileMenu.add(newItem);
fileMenu.add(updateItem);
Event-handling of menus

JMenuItem objects generate Action events

Register JMenuItem objects with ActionListener


Alternative syntax for
Event-handling
 Anonymous class
Event-handling within class
public class LottoGui2 extends JFrame implements ActionListener {
private JButton btnExit;
public LottoGui2() {
btnExit = new JButton("EXIT");

}
public void setGUI() {
btnExit.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("EXIT")) {
System.exit(0);
}
Alternative syntax for Event-handling

remove

Event
han
anonym dling by
ous cla
ss

replace
This method
will move
Alternative syntax for Event-handling
Alternative syntax for Event-handling

There will thus be an


actionPerformed method
for each GUI component
that generates an
ActionEvent
Practical exercises for this
week
 Add JMenu to Prac 5
 Start Prac 6 (Combo and Menu selection)
 Skeleton code provided on BB
Conclusion

Please check BB regularly for any


announcements
If you have any questions/queries please
contact me via email:
[email protected]
Please use your cput email for all
correspondence with your lecturers

You might also like