DOC PREVIEW
UNC-Chapel Hill COMP 401 - comp401sp13lecture21Java_UIpt2

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java$UI,$pt.$2$COMP$401$Spring$2013$Lecture$21$4/2/2013$Top$Level$Windows$• JDialog$– Dialog$box$topHlevel$windows.$– Several$types$preHdefined$for$ease$of$use.$• File$choosers,$informaOonal,$input$prompt,$etc.$• JFrame$– Normal$topHlevel$windows$for$UI$• JApplet$– We’ll$be$ignoring$this$for$now.$COMP$401$::$Spring$2012$ 2$JOpOonPane$Examples$• JOpOonPane$provide$quick$and$easy$dialog$boxes.$– showConfirmDialog$• Allows$user$to$confirm$a$choice$with$a$Yes/No/Cancel$response.$– showInputDialog$• Prompts$for$text$input.$– showMessageDialog$• Shows$message,$waits$for$user$to$acknowledge.$• All$of$these$are$“modal”.$– Flow$of$execuOon$is$halted$unOl$dialog$is$dealt$with.$– Method$returns$response$directly.$• lec21.v2$COMP$401$::$Spring$2012$ 3$JFrame$• NonHdialog$topHlevel$window.$– May$or$may$not$have$a$window$bar$• LocaOon$of$which$is$operaOng$system$and$lookHandHfeel$specific$– OperaOon$defined$by$what$is$placed$in$it.$– General$pa[ern$• Main$method$of$program$sets$up$UI$in$a$JFrame$• JFrame$made$visible.$• Rest$of$program$now$responds$asynchronously$to$user$interface$events.$• Hello,$world$revisited.$COMP$401$::$Spring$2012$ 4$Containment$Hierarchy$• JPanel$used$as$content$pane$for$topHlevel$window$is$root$of$“containment$hierarchy”.$– All$user$interface$elements$must$be$placed$in$a$container$(i.e.,$JPanel).$– Containers$can$be$nested$within$containers$• Groups$UI$elements$together$and$allows$subHlayout.$COMP$401$::$Spring$2012$ 5$Layout$Managers$• BorderLayout$– 5$areas:$NORTH,$EAST,$WEST,$SOUTH,$CENTER$– N/E/W/S$also$known$as:$• PAGE_START,$LINE_START,$LINE_END,$PAGE_END$• BoxLayout$– Stacked$either$horizontally$or$verOcally$• GridLayout$– Equal$sized,$regular$grid.$– Specify$number$of$rows$and$columns$at$construcOon.$• 0$indicates$“auto”$for$that$dimension$• Others:$Card,$Flow,$GridBag,$Goup,$Spring$– See$h[p://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html$COMP$401$::$Spring$2012$ 6$Swing$Components$• Text$– JLabel,$JTextField,$JTextArea$• Bu[ons$– JBu[on,$JCheckBox,$JRadioBu[on,$JT o ggl eBu[on$• Sliders$– JSlider$• Lots$of$others$$COMP$401$::$Spring$2012$ 7$UI$Events$• UI$elements$respond$to$interacOon$by$generaOng$“events”.$– Listeners$are$registered$to$receive$noOficaOon$when$a$parOcular$event$occurs.$– Different$listener$interfaces$defined$for$different$kinds$of$events.$• Listening$method$receives$event$informaOon$as$a$parameter.$• UI$elements$are$like$Observable$• Listeners$are$like$Observer$$COMP$401$::$Spring$2012$ 8$Bu[on$Example$• lec21.v3$• addAcOonListener(AcOonListener$l)$– Method$common$to$all$types$of$bu[ons.$– Defined$in$AbstractBu[on$• Parent$of$all$bu[on$types.$– AcOonListener$interface:$• void$acOonPerformed(AcOonEvent$e)$• AcOonEvent$encapsulates$all$event$informaOon$– AcOonEvent$• Parent$class$is$AWTEvent$– Common$informaOon$for$all$event$types.$– getSource()$» Returns$reference$to$object$that$generated$the$event.$• Other$informaOon$provided$specific$to$subclass.$COMP$401$::$Spring$2012$ 9$Java$UI’s$so$far$• Create$a$top$level$window.$• Create$a$JPanel$to$be$content$pane.$• Fill$JPanel$with$other$components$– Bu[ons,$sliders,$etc.$– Containers$with$components$arranged$in$them.$• And$so$forth.$• Connect$UI$events$with$“listeners”$– Listeners$take$acOon$as$response.$– AcOon$may$change/update$UI$• Make$top$level$window$visible.$COMP$401$::$Spring$2012$ 10$Swing$Component$Class$Hierarchy$COMP$401$::$Spring$2012$ 11$java.awt.Component$java.awt.Container$javax.swing.JComponent$JPanel$JScrollPane$JSplitPane$JTabbedPane$JToolBar$JComboBox$JLabel$JList$JProgressBar$JSeparator$JSlider$JToolTip$JTree$AbstractBu[on$$JBu[on$$JToggleBu[on$$$JCheckBox$$$JRadioBu[on$$JMenuItem$$$JMenu$Listener$Types$• Supported$at$awt.Component$– ComponentListener$– FocusListener$– KeyListener$– MouseListener$– MouseMoOonListener$– MouseWheelListener$• Supported$at$awt.Container$– ContainerListener$• Supported$at$AbstractBu[on$– AcOonListener$– ChangeListener$– ItemListener$• Supported$by$individual$components$– AcOonListener$:$JComboBox$– ChangeListener$:$JSlider,$JProgressBar$$$COMP$401$::$Spring$2012$ 12$Keyboard$Concepts$• Keyboard$events$go$to$component$that$currently$has$“focus”.$– setFocusable(boolean$status)$– isFocusable()$– requestFocusInWindow()$• Java$provides$a$framework$for$managing$focus$in$more$sophisOcated$ways$if$necessary.$– h[p://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#api$• KeyListener$interface$– keyTyped(KeyEvent$e)$$– keyPressed(KeyEvent$e)$– keyReleased(KeyEvent$e)$• Typing$of$character$vs.$pressing$of$key$– Basic$sequence$of$events$generated:$key$press,$key$typed,$key$released$• Important$KeyEvent$methods$– char$getKeyChar()$• This$is$only$valid$for$key$typed$events.$– int$getKeyCode()$– isAltDown(),$isControlDown(),$isShilDown(),$isMetaDown()$COMP$401$::$Spring$2012$ 13$Mouse$Concepts$• MouseListener$– mouseClicked(MouseEvent$e)$– mouseEntered(MouseEvent$e)$– mouseExited(MouseEvent$e)$– mousePressed(MouseEvent$e)$– mouseReleased(MouseEvent$e)$• MouseMoOonListener$– mouseDragged(MouseEvent$e)$– mouseMoved(MouseEvent$e)$• MouseEvent$– PosiOon$info$• RelaOve$to$component:$getX(),$getY(),$getPoint()$• Absolute$posiOon:$$getXOnScreen(),$getYOnScreen()$– Click$info:$getClickCount(),$getBu[on()$– Modifiers:$isAltDown(),$isShilDown(),$etc...$COMP$401$::$Spring$2012$ 14$Composing$Widgets$• A$set$of$related$UI$elements$that$act$as$a$unit$within$your$UI.$• Basic$idea:$– Subclass$JPanel$– In$constructor,$create$and$arrange$UI$components.$– Provide$methods$for$a[aching$listeners,$requesOng$current$state$of$UI,$etc.$• Could$be$straight$delegaOon$or$could$be$mediated$by$more$sophisOcated$logic.$COMP$401$::$Spring$2012$ 15$Color$Chooser$Widget$• lec21.v4$– Basic$widget$construcOon$and$set$up$• lec21.v5$–


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lecture21Java_UIpt2

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download comp401sp13lecture21Java_UIpt2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view comp401sp13lecture21Java_UIpt2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lecture21Java_UIpt2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?