Advanced GUIs II CS 102-02 Lecture 9-1 1
Agenda Scrollbars Frames and applications Popup menus Layout managers CardLayout GridBagLayout
Scrollbars Use Scrollbars two ways: Add scrollbars to components Use standalone as "sliders" Scrollbar objects are Components which implement Adjustable interface For objects with adjustable numeric value contained within a bounded range
What Scrollbars Need to Know Minimum value Maximum Horizontal or vertical orientation Delta for blocks and units Proportion viewed at one time
Scrollbar Tips Visible amount scroll bar is portion of total visible at one time Scrollbar's value can't exceed max minus scroll box size: minimum <= adjustableValue <= maximum - visible
Frames Applets run within a host application Other Containers Host provides the window Applets are Panels, which are Containers\ Other Containers ScrollPane Window Frame IS-A Window Frame = Window title + border
Frames are Handy to Have Use Frames to build standalone GUI applications Applets can use Frames too (pops up a new window)
Frame Tips Frames don't display by default Call the Frame object's setVisible() method with a true argument Frames don't have a default size Better call setSize() on the Frame object Default layout is BorderLayout Windows have events, which Frames inherit
Window Events Windows implement WindowListener windowActivated(WindowEvent) Invoked when a window is activated. windowClosed(WindowEvent) Invoked when a window has been closed. windowClosing(WindowEvent) Invoked when a window is in the process of being closed. windowDeactivated(WindowEvent) Invoked when a window is de-activated. windowDeiconified(WindowEvent) Invoked when a window is de-iconified. windowIconified(WindowEvent) Invoked when a window is iconified. windowOpened(WindowEvent) Invoked when a window has been opened.
Double-Duty: Applets and Applications Applications are similar to Applets, but have a main() method (like C & C++) Applications don't have a host window, so create one
Building Your Own Browser Write an applet which has a main() main() creates a host window (a Frame) Invokes the Applet inside the newly created Frame
Example of Appletication // Allow this applet to run as an application also public static void main( String args[] ) { // create application Frame and resize Frame app = new Frame( "Application" ); app.setSize( 300, 100 ); // register window handler to terminate application app.addWindowListener( new CloseWindowAndExit() ); // create applet instance MyFrame m = new MyFrame(); // initialize and start the applet m.init(); m.start(); // add applet to center of Frame app.add( m, BorderLayout.CENTER ); // display the frame (everything attached is painted) app.setVisible( true ); }
What's on the Menu? Applets don't have menus, because they don't have menu bars Frames can have menu bars Menus are: Good because they "clean up" the interface and unclutter it Bad because they often hide much functionality
Menus in Java Menu bar is the thingie that menus drop down from Menu item is a string inside a menu that causes an action to be performed when selected Action or another menu
Frames and Menus in Action Title bar MenuItem Menu Menubar
ScratchPad Application Lets users type text and change font and color Prevent typing by choosing read-only
Dialogs Dialogs are Windows with titles Common for passing information to user Two kinds Modal: Can't escape Non-modal (or modeless): Can be ignored
Popup Menus Popup menus can be attached to any Component (works for applets too!) Different platforms have different popup menu gestures When the AWT detects that a mouse event is the popup trigger event, it tags the event (remember MouseEvent.isPopupTrigger()?)
Layout Managers More layout managers CardLayout: "Stacks" several layouts on top of one another GridBagLayout: Like GridLayout, but more flexible (read more complex)