Download presentation
Presentation is loading. Please wait.
1
Advanced User Interfaces
Additional Swing components CS-1020 Dr. Mark L. Hornick
2
JMenuItem (and variants):
CS-1020 6/1/2018 The javax.swing package contains several classes used for creating menus JMenuBar Jmenu JMenuItem (and variants): JRadioButtonMenuItem JCheckboxMenuItem Edit View Help Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
3
JMenuBar is where the menus are placed
CS-1020 6/1/2018 JMenuBar is where the menus are placed There is only one JMenuBar per JFrame. Edit View Help JMenuBar Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
4
JMenu (such as “File” or “Edit”) is a group of menu choices
CS-1020 6/1/2018 JMenu (such as “File” or “Edit”) is a group of menu choices A JMenuBar object may include many JMenu objects. JMenu object become visible/invisible automatically. A “File” JMenu object in a JMenuBar Edit View Help Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
5
CS-1020 6/1/2018 A JMenuItem (such as New, Open, or Quit) is an individual menu choice in a JMenu object JMenuItem’s can be visually grouped by adding JSeparator’s to a JMenu Edit View Help “Open” JMenuItem Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. JSeparator CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
6
Only the JMenuItem objects (and variants) generate events.
CS-1020 6/1/2018 Only the JMenuItem objects (and variants) generate events. A JMenuItem’s Action Command defaults to the text of menu item Override with call to setActionCommand() Useful when providing UI in multiple languages Edit View Help Almost all nontrivial GUI programs support menus. By using these three menu-related classes, we can easily add menus to our Java programs. CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
7
JMenuItems generate Action Events
Some Events and Their Associated Event Listeners Act that Results in the Event Event Listener Type Action: User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener CS-1020 Dr. Mark L. Hornick
8
Sequence for Creating Menus
CS-1020 6/1/2018 Sequence for Creating Menus Create a JMenuBar object and add it to a frame via setJMenuBar() Create a JMenu object. Create JMenuItem objects and add them to the JMenu object. Add the JMenu object to the JMenuBar object. This is not the only valid sequence. Other sequences are possible. We list this sequence as one possible sequence you can follow in creating menus. CS-1020 Dr. Mark L. Hornick Dr. Mark L. Hornick
9
Demo Menus CS-1020 Dr. Mark L. Hornick
10
Radio buttons and Check boxes
Along with JButton, Radio buttons and Check boxes are all specific kinds of buttons – they all derive from the AbstractButton class This implies similar behavior for these classes These all generate Action Events CS-1020 Dr. Mark L. Hornick
11
Check boxes Implemented by JCheckBox
A GUI usually permits more than one Check box to be selected at one time CS-1020 Dr. Mark L. Hornick
12
Radio Buttons Implemented by JRadioButton
A GUI usually prevents more than one Radio button to be selected at once Typically used along with a ButtonGroup object ButtonGroup takes care of deselecting the previous Radio button when a new one is selected CS-1020 Dr. Mark L. Hornick
13
Some Events and Their Associated Event Listeners
Mouse Events Some Events and Their Associated Event Listeners Act that Results in the Event Event Listener Type Action: User clicks a button, presses Enter while typing in a text field, or chooses a menu item ActionListener User closes a frame (main window) WindowListener User presses a mouse button while the cursor is over a component MouseListener User moves the mouse over a component MouseMotionListener Component becomes visible ComponentListener Component gets the keyboard focus FocusListener Table or list selection changes ListSelectionListener Any property in a component changes such as the text on a label PropertyChangeListener CS-1020 Dr. Mark L. Hornick
14
Mouse Events Mouse events arise from such user interactions as
moving the mouse dragging the mouse (moving the mouse while the mouse button is being pressed) clicking the mouse buttons. Who listens for Mouse Events? JFrame ContentPane JPanel … or whoever is interested in getting events CS-1020 Dr. Mark L. Hornick
15
Handling Mouse Events A MouseListener handles most mouse events:
mouseEntered – cursor crossed into the boundary mouseExited – cursor crossed out of the boundary mousePressed – button down (specifics in MouseEvent) mouseReleased – button up mouseClicked – button down/up in quick succession A MouseMotionListener handles mouse movement mouseDragged – mouse moved while button down mouseMoved – mouse moved while button up Point location info in MouseEvent Location is relative to the listening components origin CS-1020 Dr. Mark L. Hornick
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.