CompSci 125 Lecture 17 GUI: Graphics, Check Boxes, Radio Buttons
Announcements
GUI Review
Review: Inheritance Subclass is a Parent class Includes parent s features May Extend May Modify extends! Parent Class Subclass
JFrame Container Title Bar includes the title text, and the minimize, maximize and close buttons Content pane Re-position the frame Re-size the frame http://docs.oracle.com/javase/6/docs/api/javax/swing/jframe.html
Content Pane Is not a class! Is the area of the JFrame where we can add/draw stuff JFrame constructs it as a JComponent but getcontentpane returns a reference to a Container We can add other graphical components to it Has a LayoutManager getcontentpane from the JFrame setcontentpane in the JFrame add a component to the content pane
JPanel Is a Container to which you may add other Components May have a LayoutManager Can be configured as double-buffered You may add a JPanel to the content pane Use setcontentpane to replace the content pane with a JPanel Implements a paintcomponent(graphics g) method You may subclass a JPanel and override paintcomponent Then you can draw on the Graphics context
JLabel Displays text string, an icon or both Write only --- no user input possible http://docs.oracle.com/javase/6/docs/api/javax/swing/jlabel.html
JButton Displays a button Our class implements the ActionListener interface Java calls ActionListener s actionperformed method when user clicks button http://docs.oracle.com/javase/6/docs/api/javax/swing/jbutton.html
ActionListener Interface Our class includes implements ActionListener clause Java ensures we implement the actionperformed method Java passes an ActionEvent parameter to actionperformed ActionEvent s getsource method returns a reference to the object (e.g. an instance of a JButton or JTextField) in which the event occurred
JTextField Displays text (like a JLabel) Unlike a JLabel, user can enter text Our class implements ActionListener Java calls actionperformed when user modifies text ActionEvent s getsource method identifies source of event JTextField s gettext returns reference to user s text String Many, many methods! http://docs.oracle.com/javase/6/docs/api/javax/swing/jtextfield.html
The Graphics Context
Graphics Context 2-D canvas --- we can draw graphical shapes on it Integer coordinate system in pixels Java passes reference to a Graphics to paintcomponent http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/graphics.html
The Graphics State Includes Current clip rectangle Current color Current font
Graphics Operations Include drawarc drawimage drawline drawoval drawrect drawstring fillarc filloval fillrect
Graphics Etc clearrect getcliprect setcolor setfont more
CheckBoxes
JCheckBox Labeled stateful check box Text label describes an attribute Check box displays associated state (selected or not) Clicking box toggles its associated state Useful methods JCheckBox( Label Text String ) isselected() Our class implements either ActionListener or ItemListener interface
ItemListener Interface public void itemstatechanged(itemevent event) Called when check box s state changes: User interaction (clicking) Programmatically (unlike ActionListener) ItemEvent methods include: getitemselectable: Returns reference to source of event getstatechange: Returns reason for the event
Radio Buttons
Radio Buttons JRadioButton --- only one can be selected at any time ButtonGroup --- manages group of related JRadioButtons ActionListener
JRadioButton Labeled stateful button Text label describes an attribute Button displays associated state (selected or not) Clicking button toggles its associated state Only one button in group selected at any time Useful methods JRadioButton( Selected Button s Text String,true) JRadioButton( Unselected Button Text ) isselected() Define a ButtonGroup and add each JRadioButton to it Our class typically implements ActionListener interface
ButtonGroup Use add method to add a JRadioButton to a ButtonGroup Ensures only one JRadioButton will be selected at a time