70 likes | 164 Views
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 week: Array Lists Linked Lists Searching with Boyer/Moore Next two weeks: GUI - graphical user interfaces. Announcements & Review. Graphical User Interface (GUI).
E N D
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 week: Array Lists Linked Lists Searching with Boyer/Moore Next two weeks: GUI - graphical user interfaces Announcements & Review Lecture 32: Graphical User Interface (GUI)
Graphical User Interface (GUI) • Java was in part designed for web applications • Previously the OS provided all the GUI • Library philosophy: grow widely reused and desirable functionality in libraries • libraries are separate from the language • examples: collections, file input, Color • some programmer went on from 305j to write Java library routines ;-) • GUI library makes it easier to write interactive applications Lecture 32: Graphical User Interface (GUI)
GUI Basics • Graphical input/output using JFrames • a JFrame contains on screen components • e.g., buttons, labels, text, scroll bars, images, ... • Layout • arranges components in a Frame • Events • detecting and acting on user input Lecture 32: Graphical User Interface (GUI)
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 32: Graphical User Interface (GUI)
GUI Interface Remember: an interface is not a class, but a partial definition of what needs to be in a class 1. Cannot specify method implementations 2. All methods in an interface are public 3. All variables are public, final, and static 4. A class can implement multiple interfaces public interface ActionListener { // constants // method declarations public void actionPerformed (ActionEvent e) } public class QuestionGUI implementsActionListener {...} Lecture 32: Graphical User Interface (GUI)
A Basic GUI public class QuestionGUI implementsActionListener { // window instance variables private JFrame window = new JFrame(“Review Questions”); public QuestionGUI() { // constructor that creates the GUI window, text, etc. window.add(...); } public void actionPerformed (ActionEvent e) { // grabs user input out of GUI and acts on it } public static void main (String[] args) { QuestionGUI qgui = new QuestionGUI(); } } Lecture 32: Graphical User Interface (GUI)
A Basic GUI • Let’s go work with the code Lecture 32: Graphical User Interface (GUI)