11GUI Elements in JavaNelson Padua-PerezChau-Wen TsengDepartment of Computer ScienceUniversity of Maryland, College Park2GUI ElementsComponent ⇒ items displayed (widgets)Container ⇒ region containing widgets Layout ⇒ arrangement of containerEvent ⇒ interactions for GUI23GUI Elements – ComponentDefinitionActual items (widgets) user sees in GUIExamplesLabels (fixed text)Text areas (for entering text)ButtonsCheckboxesTablesMenusToolbarsEtc…4GUI Elements – ContainerDefinitionAbstractions occupying space in GUIPropertiesUsually contain one or more widgetsCan be nested in other containersExampleWindow containing1 menu (component) 3 buttons (component)2 windows (container)35GUI Elements – LayoutDefinitionArrangement of widgets in containerLayout specificationLogical terms (2ndrow, 1stcolumn, left)Preferred approachActual coordinates (100 pixels, 5 inches)Can be too rigid, limited to certain window sizes Layout managerEntity translating layout specifications into actual coordinates at runtime, depending on conditions6Example Java Layout ManagersGridLayoutLays out components in a grid of user specified sizeBorderLayoutDesignates portions of the container as North, South, East, West, and Center CardLayoutAdds components one on top of another GridBagLayoutCustomizable manager that can use rows and columns of varying lengths47GUI Elements – EventsDefinitionAction or condition occurring outside normal flow of control of programExamplesMouse clicks Keyboard inputMenu selectionsWindow actions8Java GUI ClassesAWT (Abstract Window Toolkit) (java.awt.*)Old GUI framework for Java (Java 1.1)Some reliance on native code counterpartsPlatform independence problemsSwing (javax.swing.*)New GUI framework first introduced in Java 1.2Includes AWT features plus many enhancementsPure Java components (no reliance on native code)Pluggable look and feel architecture59JFrame HierarchySeveral super classes and well as implemented interfacesMany, many member methods including inherited methods that allow for operations such as resizing, setting properties, adding components, etc.Other top level containersJDialog (dialog boxes)JApplet (web applets)JWindow (stripped down JFrame, no title bar or window buttons)ObjectComponentContainerWindowFrameJFrame10Java GUI ComponentsExamplesJFrameJTextfieldJlabelButtonJListJComboBoxMenuComboPanesIndicators Dialog boxesJFileChooserColor chooserJTableJTree611ExamplesIllustrates how to create a JFrame and a JPanelIllustrates how we can set a call back to take care of certain mouse events (those associated with MouseListener)Example: package example1 of code distribution.Example: package example2 – Shows how we can put all together in one classExample: package example3 – Shows how two layout managers organize components12JFrame StructureMost things go into content panegetContentPane()Use glassPane for pop up menus, some animationsMethodsgetRootPane()getLayeredPane()getContentPane()getGlassPane()Can set…Pane explicitlyglassPanerootPaneJFramelayeredPaneLayeredPane contains contentPaneLayeredPane manages (optional) JMenuBar713JFrame14JTextField and JLabel815ButtonsToolbar16JList and JComboBox917Menu18Panels and PanesTabbed PaneSplit PaneScroll Pane1019Various IndicatorsSpinnerSliderProgress Bar20A Dialog Box1121JFileChooser22Color Chooser1223Jtable and JTree24Text
View Full Document