Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26 Exam 2 Wed 5/2 5:30-7:30 Last time: Basic GUI JFrame - container of components Components JTextField - text entry JTextArea - write text JRadioButton - allocate two or more JRadioButton s (text under) ButtonGroup - groups JRadioButton s so only one is selected JButton - selection button (text in)
Lecture 33: More Graphical User Interface (GUI) Today Fill out surveys first Review GUI structure JFrame structure –Example features of components in a JFrame –What goes in the instance variables –What goes in the constructor –What goes in the listener (performAction) JRadioButton - example details New component: ImageIcon
Lecture 33: More Graphical User Interface (GUI) Review GUI Code Structure GUI structure differs substantially from our programming model so far model so far –main has a bunch of statements Java does in order, –when we get to the end, we are finished GUI model –main creates a GUI object & that’s it!? –The class GUI constructor registers a “listener” method with an event (e.g., a button selection) –When that event occurs, Java calls the “listener” method –“listener” gathers the user input and acts on it
Lecture 33: More Graphical User Interface (GUI) JFrame What goes in the instance variables? –Default sizes, colors, strings, and policies –Component sizes, strings, etc. What goes in the constructor? –addActionListener() - one for every component that has an event you want to handle –policy for close, setting window size, make window visible, colors, etc. –compose components –add each component to the JFrame –lots and lots of options e.g., get/setBackground, setLayout, setResizabe, mouse,... ng/JFrame.htmlhttp://java.sun.com/j2se/1.4.2/docs/api/javax/swi ng/JFrame.html
Lecture 33: More Graphical User Interface (GUI) JFrame What goes in a listener (performAction) –Grab the input and do something in response –Examples TextArea: getText(), getSelectedText(), getCursor JRadioButton, JButton: isSelected()
Lecture 33: More Graphical User Interface (GUI) Example Component Fuctionality JRadioButton selected or deselected displays its state to the user Used with a ButtonGroup object to create a logical group of buttons in which only one button at a time can be selected Example constructor –JRadioButton(String text, boolean selected) Creates button with label text and selection state
Lecture 33: More Graphical User Interface (GUI) Example Component Fuctionality ButtonGroup void add(AbstractButton b) Adds the button to the group. int getButtonCount() Returns the number of buttons in the group. Enumeration getElements() // a collection Returns all the buttons that are participating in this group. ButtonModel getSelection() Returns the model of the selected button. boolean isSelected(ButtonModel m) Returns whether a ButtonModel is selected. void remove(AbstractButton b) Removes the button from the group. void setSelected(ButtonModel m, boolean b) Sets the selected value for the ButtonModel.
Lecture 33: More Graphical User Interface (GUI) ImageIcon Component Example constructor ImageIcon(Image image) Creates an ImageIcon from an image object. Example methods void setImage(Image image) Sets the image displayed by this icon. int getIconHeight() Gets the height of the icon. int getIconWidth() Gets the width of the icon.
Lecture 33: More Graphical User Interface (GUI) Show ImageIcon in BlueJ Let’s try it out