150 likes | 169 Views
Advanced User Interfaces. Additional Swing components. Edit. View. Help. The javax.swing package contains several classes used for creating menus. JMenuBar Jmenu JMenuItem (and variants): JRadioButtonMenuItem JCheckboxMenuItem. Edit. View. Help.
E N D
Advanced User Interfaces Additional Swing components CS-1020 Dr. Mark L. Hornick
Edit View Help The javax.swing package contains several classes used for creating menus • JMenuBar • Jmenu • JMenuItem (and variants): • JRadioButtonMenuItem • JCheckboxMenuItem CS-1020 Dr. Mark L. Hornick
Edit View Help JMenuBar is where the menus are placed There is only one JMenuBar per JFrame. JMenuBar CS-1020 Dr. Mark L. Hornick
Edit View Help 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 CS-1020 Dr. Mark L. Hornick
Edit View Help 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 “Open” JMenuItem JSeparator CS-1020 Dr. Mark L. Hornick
Edit View Help 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 CS-1020 Dr. Mark L. Hornick
JMenuItems generate Action Events CS-1020 Dr. Mark L. Hornick
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. CS-1020 Dr. Mark L. Hornick
Demo • Menus CS-1020 Dr. Mark L. Hornick
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
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
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
Mouse Events CS-1020 Dr. Mark L. Hornick
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
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