Weeks 8 and 9GUI and Event-Driven ProgrammingCS 180Prof. Sunil PrabhakarDepartment of Computer Science Purdue University© Sunil Prabhakar Purdue UniversityGraphical User InterfacesInput/output devices for computersPrinter, punch cardsKeyboard, ScreenGraphical interface with mouse inputYou have used GUIs for most of your interactions with the computer. GUIs consist of windows, buttons, menus, entry fields, …2© Sunil Prabhakar Purdue UniversitySample GUI3© Sunil Prabhakar Purdue UniversityGUI classesJava makes it very easy to create GUIsThe two packages java.awt and javax.swing provide a large number of classes that can be used to construct GUIsBy using these classes, we need not worry about the differences between operating systems or system detailsWe will use classes from the swing package as they are more reliable across platformsThe awt package provides support for swing classes4© Sunil Prabhakar Purdue UniversityCreating a simple GUICreate a window object Add GUI elements to the windowWrite code to respond to the GUI elements5© Sunil Prabhakar Purdue UniversityCreating a WindowThe JFrame class is a common starting point.The JFrame class corresponds to a basic window for the given operating systemIt behaves like most other windowsWe can either create an object of the JFrame class, orcreate a subclass of JFrame if we expect to create multiple windows with the same behavior6© Sunil Prabhakar Purdue University7A simple JFrame objectimport javax.swing.*;class ShowWindow { public static void main( String[] args ) { JFrame myWindow; myWindow = new JFrame();myWindow.setSize(300,400);myWindow.setTitle(“My Window”);myWindow.setResizable(true);myWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); myWindow.setVisible(true); }}© Sunil Prabhakar Purdue University8A custom JFrameimport javax.swing.*;class MyWindow extends JFrame { public MyWindow(String title) {this.setSize(300,400);this.setTitle(title);this.setVisible(true); }}import javax.swing.*;class ShowWindow2 { public static void main( String[] args ) { MyWindow myWindow = new MyWindow(“My Window”); MyWindow window = new MyWindow(“Another Window”); }}© Sunil Prabhakar Purdue University9Some GUI classesFrameA special container corresponding to a window not contained in another window.JFrameJApplet (for web applets)ContainersGUI components that hold other GUI components.JFrame, JApplet, JPanelAn invisible container that can be nested.© Sunil Prabhakar Purdue University10Other GUI classesCommon elementsJButton, JCheckBox, JComboBox,JTextField, JTextAreaGraphicsAllows drawing of circles, strings, etc.FontFor selecting fonts for textColorFor selecting colors of GUI componentsMenu classesJMenuBar, JMenuAnd many more …© Sunil Prabhakar Purdue UniversityCoverageThere are way too many classes for us to consider each oneWe will see a samplingUse the online tutorial from Sun for more examples, other detailshttp://java.sun.com/docs/books/tutorial/ui/features/components.html11© Sunil Prabhakar Purdue University12Essentials of a GUI We begin with a frame (e.g, JFrame, JApplet)We will use JFrame as our starting point.We can change the properties of the frame by calling several methods for it.We cannot add components to the JFrame directly. We have to add them to its Content Pane.We can add (and remove) components from this pane. These can be buttons, text fields, labels, lists, scroll bars, …. , and other panes.© Sunil Prabhakar Purdue University13The Content Pane of a FrameWe access the content pane by calling the frame’s getContentPane() method. It belongs to the Container classThis yellow area is the content pane of this frame.© Sunil Prabhakar Purdue UniversityAdding ComponentsWe can add objects to a container object by using the add() method on the containerWe can add multiple objects to a single containerTheir placement is controlled by eithera layout manager, orabsolute positioning (rare)14© Sunil Prabhakar Purdue UniversityLayout ManagersA layout manager organizes the multiple components added to a single container.For now, we will use a FlowLayoutManager.The flow layout organizes objects similar to how (centered) text is written on a pageWe set the layout manager for a container by using the setLayout() method15© Sunil Prabhakar Purdue University16Adding Buttons A JButton object is a GUI component that represents a pushbutton.Create new objectsGet Container Panel,Set Layout MANAGERAdd Objects to Panel. . .JButton loginButton = new JButton("Login");JButton cancelButton = new JButton("Cancel");Container contentPane;contentPane = myFrame.getContentPane();contentPane.setLayout(new FlowLayout());contentPane.add(loginButton);contentPane.add(cancelButton);. . .© Sunil Prabhakar Purdue Universityimport javax.swing.*;import java.awt.*;class LoginWindow extends JFrame{JButton loginButton, cancelButton;JTextField nameInput; public LoginWindow(String title) {this.setTitle(title);this.setSize(200,100);loginButton = new JButton(“Login”);cancelButton = new JButton(“Cancel”);JLabel label = new JLabel(“Name”);nameInput = new JTextField(“<Enter Name>”);Container contentPane = this.getContentPane();contentPane.setLayout(new FlowLayout());contentPane.add(label);contentPane.add(nameInput);contentPane.add(loginButton);contentPane.add(cancelButton);this.pack();this.setVisible(true);}}17Exampleclass OpenAccount { public static void main( String[] args ) { LoginWindow myWindow = new LoginWindow(“Login to Account”); }}© Sunil Prabhakar Purdue University18Control flow with GUIGUI components introduce a new type of control flow.In earlier example, even though the main method ends, the window (and program) keep running.A separate thread is automatically created which handles the GUI components.What code is running?The separate thread watches for user interactions with the GUI componentshow does it know what to do, e.g., when a button is pressed?Event handling© Sunil Prabhakar Purdue University19Event HandlingAn action involving a GUI object, such as clicking a button, is called an event.The mechanism to process events is called event handling.Event handling in Java is
View Full Document