1 / 9

Announcements & Review

Explore Graphical User Interface (GUI) programming using JFrame, components, listeners, and event handling in Java. Learn core concepts in creating interactive interfaces.

reggiel
Download Presentation

Announcements & Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) Announcements & Review Lecture 33: More Graphical User Interface (GUI)

  2. 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)

  3. 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)

  4. 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, ... • http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html Lecture 33: More Graphical User Interface (GUI)

  5. 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)

  6. 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)

  7. 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)

  8. 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)

  9. Show ImageIcon in BlueJ Let’s try it out Lecture 33: More Graphical User Interface (GUI)

More Related