Download presentation
Presentation is loading. Please wait.
Published byEustace Atkinson Modified over 8 years ago
1
Chapter 4: Laying out the GUI Useful containers –Panels –Frames –Dialogs Layout mechanisms –FlowLayout –BorderLayout –GridLayout –CardLayout Menus
2
Controlling the Layout To get look we want use objects control how widgets arranged Generally, divide window into regions and arrange objects in regions Can also bring up new windows Also have mechanisms for creating menus
3
Containers - mechanisms for controlling appearance Canvas - mechanism for controlling look of graphic objects (pictures, etc.)
4
Container class abstract class methods: void add(Component c) - add component c void add(Component c, int position) - add c at index void add(String name, Component c) - add c w/ name void remove(Component c) - remove from container void removeAll() - remove all components Component getComponent(int position) - comp at pos int countComponents() - num of comps in container
5
Container class (cont) methods: Container getParent() - parent cont of container boolean isAncestorOf(Component c) - true if parent, grandparent, etc. of c Dimension minimumSize() - min required size Dimension preferredSize() - preferred size void layout() - force layout to occur (need not call) LayoutManager getLayout() - return the LayoutManager being used (null if none) void setLayout (LayoutManager layout) - set the LayoutManager to use
6
Panel class generally used, small, simple (Applet is a Panel) constructor: Panel() - empty container methods: inherited from general Container class layout method: initially set to FlowLayout Layouts mechanisms for distributing objects each container has Layout manager
7
FlowLayout lay things out like word processor, left to right, one column at a time, using minimum space constants: LEFT, CENTER, RIGHT - for alignments constructors: FlowLayout() - centered, standard horz,vert gaps FlowLayout(int align) - set align FlowLayout(int align, int hGap, int vGap) methods (also for BorderLayout, GridLayout): void layoutContainer(Container c) - layout c w/ this Dimension minimumLayoutSize(Container c) Dimension preferredLayoutSize(Container c)
8
BorderLayout use add method with string as in add(“North”,myButton) which regions depends on string constructors: BorderLayout() - 0 horiz,vert gaps BorderLayout(int hGap, int vGap) methods - same as for FlowLayout notes: BorderLayout resizes objects to fit Layout order: first N,S given needed height, remainder goes to E,C,W, then E,W given needed width, remaining space to C To avoid resizing, add object to panel and add panel to window
9
GridLayout objects arranged left-right, top-bottom in even sized cells objects resized to fit cells columns are added if needed constructors: GridLayout() - 1 row, infinite columns, default gaps GridLayout(int rows, int cols) - default gaps GridLayout(int r, int c, int hGap, int vGap) - set rows, cols, gaps methods - same as for FlowLayout
10
public class TestLayout extends Applet { Panel Panel1 = new Panel(); Panel Panel2 = new Panel(); Panel Panel3 = new Panel(); Button BA1 = new Button("A"); Button BA2 = new Button("A"); Button BA3 = new Button("A"); Button BB1 = new Button("B"); Button BB2 = new Button("B"); Button BB3 = new Button("B"); Button BC1 = new Button("C"); Button BC2 = new Button("C"); Button BC3 = new Button("C"); Button BD1 = new Button("D"); Button BD2 = new Button("D"); Button BD3 = new Button("D"); Button BE1 = new Button("E"); Button BE2 = new Button("E"); Button BE3 = new Button("E"); public void init () { Panel2.setLayout(new BorderLayout()); Panel3.setLayout(new GridLayout(2,3)); Panel1.add(BA1); Panel1.add(BB1); Panel1.add(BC1); Panel1.add(BD1); Panel1.add(BE1); Panel2.add("North",BA2); Panel2.add("South",BB2); Panel2.add("East",BC2); Panel2.add("West",BD2); Panel2.add("Center",BE2); Panel3.add(BA3); Panel3.add(BB3); Panel3.add(BC3); Panel3.add(BD3); Panel3.add(BE3); add(Panel1); add(Panel2); add(Panel3); }
11
CardLayout objects maintained as a stack, only one on top shown best to name components (to find the one you want) constructors: CardLayout() - empty stack CardLayout(int hGap, int vGap) - margins around object group methods: void first(Container par) - first comp in deck (note par) void last(Container par) - last comp in deck void next(Container par) - next comp in deck void previous(Container par) - previous comp in deck void show(Container par, String name) - show named comp see text, pp. 114-115 for example
12
No Layout can also use no layout: myContainer.setLayout(null); in this case you manually shape,locate objects and add things like borders yourself Canvas often used when you want to glue separate objects together (a picture plus text) in a specific way, without worrying about manager decisions constructor: Canvas()
13
Window class abstract class pop-up environments, separate from existing container often used for interaction (Dialogs) two types: Frame, Dialog Window methods: void dispose() - eliminate resources used when done void pack() - resize to fit added components void show() - make the window appear
14
Dialog class generally for simple input/output notes: must have associated frame (ok to use dummy frame) can be modal (takes all input) or modeless must reshape and show to execute uses BorderLayout unless changed constructors: Dialog(Frame parent, boolean isModal) - no title Dialog(Frame parent, String title, boolean isModal) methods: boolean isModal() - true if modal dialog void setModal(boolean isModal) - set modal flag boolean isResizeable() - true if window resizeable void setResizable(boolean canResize)
15
import java.applet.*; import java.awt.*; public class TestDialog extends Applet { Frame dummy = new Frame(); Dialog myDialog = new Dialog(dummy,"NJ Attitude",true); Panel qpanel = new Panel(); Label question = new Label("You talking to me?"); Checkbox yes = new Checkbox("yes"); Checkbox no = new Checkbox("no"); CheckboxGroup answer = new CheckboxGroup(); public void init () { yes.setState(true); yes.setCheckboxGroup(answer); no.setCheckboxGroup(answer); qpanel.setLayout(new GridLayout(1,3)); qpanel.add(question); qpanel.add(yes); qpanel.add(no); myDialog.add(qpanel); myDialog.reshape(100,100,400,100); myDialog.show(); }
16
Frame class more general than Dialog notes: needs no associated frame must reshape and show to execute uses BorderLayout unless changed constructors: Frame() - empty with no title Frame(String title) - empty with title methods: boolean isResizeable() - true if window resizeable void setResizable(boolean canResize)
17
Menus built using set of components: –menu has a MenuBar associated with a Frame –menuBar holds a collection of Menu objects (submenus) –a Menu holds a set of MenuItems (plus separators) –a Menu can be used in place of a MenuItem –MenuItems can be enabled and disabled, also checked
18
MenuComponent class abstract class useful methods: Font getFont() - return Font associated with comp void setFont(Font f) - set Font String getName() - get name of component void setName(String name) - set name of component TearOff Menus some systems allow menus to “tear off”, I.e. be pulled away from the rest of menu
19
MenuBar class constructor: MenuBar() - empty menu bar methods: Menu add(Menu m) - add m to menu bar (return) void remove(int index) - remove index item from bar void remove(MenuComponent m) - remove m int countMenus() - return number of menus Menu getMenu(int index) - return index menu Menu getHelpMenu() - return menu of Help menu void setHelpMenu(Menu m) - set m as Help menu
20
Menu class constructors: Menu(String name) - make named Menu Menu(String name, boolean canTearOff) - named menu with tear off status noted methods: void add(String name) - add named item to Menu MenuItem add(MenuItem m) - add MenuItem to Menu MenuItem addSeparator() - add separator (line) void remove(int index) - remove index item void remove(MenuComponent m) - remove m MenuItem getItem(int index) - index item of Menu int countItems() - number of items in Menu boolean isTearOff() - status of TearOff flag
21
MenuItem class constructor: MenuItem(String name) - make item with name methods: void disable() - mark item as disabled in menu void enable() - mark item as enabled in menu boolean isEnabled() - check enabled/disabled status String getLabel() - name of item void setLabel(String name) - set name of item
22
CheckboxMenuItem class type of MenuItem initially unchecked constructor: CheckboxMenuItem(String name) - named item methods: boolean getState() - return checked status of item void setState(boolean state) - sets checked status of item
23
public class TestMenu extends Applet { Frame myFrame = new Frame("Some choices"); MenuBar mymenuBar = new MenuBar(); Menu fileMenu = new Menu("File"); Menu editMenu = new Menu("Edit"); Menu helpMenu = new Menu("Help!"); Menu openMenu = new Menu("Open"); public void init() { fileMenu.add("New"); openMenu.add("File"); openMenu.add("Project"); fileMenu.add(openMenu); fileMenu.addSeparator(); fileMenu.add("Save"); fileMenu.add("Save As"); fileMenu.add("Quit"); editMenu.add("Cut"); editMenu.add("Copy"); editMenu.add("Paste"); helpMenu.add(new CheckboxMenuItem("Topics")); helpMenu.add("Search"); mymenuBar.add(fileMenu); mymenuBar.add(editMenu); mymenuBar.add(helpMenu); myFrame.setMenuBar(mymenuBar); myFrame.reshape(100,200,400,150); myFrame.show(); }
24
Running Applications Class for application must have a main() method (see example) Generally do not use Applet Generally build own frame/dialogs
25
import java.awt.*; public class TestDialogApp extends Frame { public TestDialogApp() {} public static void main(String[] args) { Frame f = new TestDialogApp(); QDialog qd = new QDialog(f,"You talking to me?"); qd.reshape(10,10,400,100); qd.show(); }} class QDialog extends Dialog { public QDialog(Frame parent, String message) { super(parent,false); // Call parents ctor Panel qpanel = new Panel(); Label question = new Label(message); Checkbox yes = new Checkbox("yes"); Checkbox no = new Checkbox("no"); CheckboxGroup answer = new CheckboxGroup(); setBackground(Color.lightGray);setForeground(Color.black); setResizable(false); yes.setState(true); yes.setCheckboxGroup(answer); no.setCheckboxGroup(answer); qpanel.setLayout(new GridLayout(1,3)); qpanel.add(question); qpanel.add(yes); qpanel.add(no); add("Center",qpanel); reshape(100,100,400,100); show(); }}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.