Beruflich Dokumente
Kultur Dokumente
JavaSwing
CIS172JavaI Prof.Rasley
Model: Models the current problem (e.g. counting votes). Has nothing to do with interacting with the user. View: Shows relevant information in the model to the user. Controller: Gathers input from the user to modify the model.
Java: Learning to Program with Robots
47
WhatisSwing?
AnAPIforGraphicalUserInterfaces(GUI) Createdtoprovideamoresophisticatedsetof GUIcomponentsthantheAbstractWindows Toolkit(AWT) Toolkit (AWT) PartoftheJavaFoundationClasses(JFC) Lightweight componentsbecausetheylook consistentonplatforms
12/3/2008
JavaSwingClassHierarchy
TheclassJComponent, descendeddirectlyfrom Container,istherootclass formostofSwing'suser interfacecomponents.
From:http://www.javabeginner.com/javaswingtutorial.htm
JFrame inheritance
Creatingwindows Graphicalobjecttobedisplayed
Holdsotherobjects
Createsbasicwindows
Addstitlebar&border
JFrame constructors&methods
Constructors: JFrame()notextinthetitlebar JFrame (Stringtitlebartext) Methods: getContentPane() returnsthecontentpaneobjectforthe window setDefaultCloseOperation(int operation)setsthedefault operationwhentheuserclosestheXintheuppercorner setSize (int width,int height)setsthewindowsizeinpixels setVisible (boolean mode)displayswindowifmode=true; hidesiffalse
12/3/2008
GUIapplicationtasks:
CallconstructoroftheJFrame superclass Getanobjectreferencetothecontentpane container GUIobjectsareaddedtothispane SetthelayoutmanagertoarrangeGUI componentsinthewindow Instantiateeachcomponent Addeachcomponenttothecontentpane Setthewindowsize Displaythewindow
SwingComponents:
JLabel displaysanimageorreadonlytext JTextField singlelinetextboxforinput JTextArea multiplelinetextbox JPasswordField singlelineforacceptingpasswordsbut notdisplayingwhatistyped d l h d JButton commandbutton JRadioButton toggleoneinagroup JCheckBox userselects0,1,ormoreoptionsinagroup JList listofitemstoselect1ormore JComboBox dropdownlisttoselectoneitem
JComponent Methods:(allreturnvoid)
setVisible (boolean mode)truemeansvisible setToolTipText (StringtoolTip)mousehovershowsthis text setForeground (ColorforeColor)foregroundcolorof thecomponent p setBackground (Colorbackcolor)backgroundcolorof thecomponent setOpaque (boolean mode)backgroundisopaqueif trueandtransparentiffalse setEnabled (boolean mode)enablediftrueandcan respondtouserinput
12/3/2008
EventHandling
GUIsareeventdriven userchoosetoactandtheGUI respondsviaaneventhandler Toprogramforthisweneedto:
Writealistener(eventhandler) Instantiateanobjectofthatclass Registerthelisteneron1ormorecomponents
DialogBoxesinSwing
JDialog isdirectlydescendedfromtheDialog class JDialog containsarootPane hierarchy includingacontentPane including a contentPane Alldialogsaremodal,whichmeansthe currentthreadisblockeduntiluserinteraction withithasbeencompleted.
From:http://www.javabeginner.com/javaswingtutorial.htm
TypesofDialogBoxes
JOptionPane.showMessageDialog reports somethingthathashappenedtotheuser JOptionPane.showConfirmDialog asksfora confirmingresponsee.g.yes,no,cancel JOptionPane showInputDialog prompt the user JOptionPane.showInputDialog prompttheuser forinput Lotsofgoodinformationat: http://java.sun.com/docs/books/tutorial/uiswing /components/dialog.html