GUIs JFrames and Windows creating a Window Menus

  • Slides: 6
Download presentation
GUIs • JFrames and Windows – creating a Window • Menus – adding a

GUIs • JFrames and Windows – creating a Window • Menus – adding a Window to the menu • Event Handling – handling events that happen in the Window • Layouts – arranging components in the Window CPSC 150

Building a Window • Everything is in a JFrame • JFrame has Frame (whole

Building a Window • Everything is in a JFrame • JFrame has Frame (whole window) title bar – Title bar – Menu bar (optional) – Content Pane menu bar content pane CPSC 150

Adding menus: 3 parts • JMenu. Bar – Displayed below the title. – Contains

Adding menus: 3 parts • JMenu. Bar – Displayed below the title. – Contains the menus. • JMenu – e. g. File. Contains the menu items. • JMenu. Item – e. g. Open. Individual items. CPSC 150

private void make. Menu. Bar(JFrame frame) { } JMenu. Bar menubar = new JMenu.

private void make. Menu. Bar(JFrame frame) { } JMenu. Bar menubar = new JMenu. Bar(); frame. set. JMenu. Bar(menubar); To add JMenu to JMenu. Bar { To add JMenu. Bar to JFrame // create the File menu JMenu file. Menu = new JMenu("File"); menubar. add(file. Menu); JMenu. Item open. Item = new JMenu. Item("Open"); file. Menu. add(open. Item); To add JMenu. Item to JMenu { JMenu. Item quit. Item = new JMenu. Item("Quit"); file. Menu. add(quit. Item); } CPSC 150

Struts and Glue • Invisible components used as spacing. • Available from the Box

Struts and Glue • Invisible components used as spacing. • Available from the Box class. • Strut: fixed size. – Component create. Horizontal. Strut(int width) – Component create. Vertical. Strut(int height) • Glue: fills available space. – Component create. Horizontal. Glue() – Component create. Vertical. Glue() CPSC 150

Pushing a menu to the right menu = new JMenu("File"); menubar. add(menu); menu =

Pushing a menu to the right menu = new JMenu("File"); menubar. add(menu); menu = new JMenu("Filter"); menubar. add(menu); menubar. add(Box. create. Horizontal. Glue()); menu = new JMenu("Help"); menubar. add(menu); Glue (invisible) CPSC 150