Unformatted text preview:

September 29, 2009 CS 350 Lecture 10 1CS 350: Computer/Human InteractionLecture 10 Overview●Java basics●Java Swing applications●Input elements and listeners●Message dialogs, menus●File output●Java Swing applets●References: JTUT, JSWI, JAPISeptember 29, 2009 CS 350 Lecture 10 2CS 350: Computer/Human InteractionJava Basics●Everything is part of a class, including static methods●Application is a class with a static main method●Class can extend another class; i.e., inherits superclass data and methods●Class can implement an interface; i.e., “inherits” and must implement the methods of the interfaceSeptember 29, 2009 CS 350 Lecture 10 3CS 350: Computer/Human InteractionJava Basics●All objects are dynamically createdJLabel title = new JLabel ("A label");●Console output using System.outSystem.out.print ("Output without newline");System.out.println ("Output with newline");●String concatenation operator is +, all objects convert using toString methodtitle.setText ("A new label: " + number);September 29, 2009 CS 350 Lecture 10 4CS 350: Computer/Human InteractionIn-class Exercise 1●Download PizzaJava1-3.zip from course website.●File unzips into a Java Eclipse workspace with 5 projects●Launch Eclipse, File → Switch Workspace, browse to the workspace folder●Look at HelloWorldApp.javaSeptember 29, 2009 CS 350 Lecture 10 5CS 350: Computer/Human InteractionJava Swing●Sun Swing Tutorial (JSWI) is very good or follow links on API (JAPI) pages●Swing is a GUI toolkit of components implemented in Java on top of AWT (Abstract Windowing Toolkit). Components are nested.●Every GUI must have a base top-level component, most commonly: JFrame or JAppletSeptember 29, 2009 CS 350 Lecture 10 6CS 350: Computer/Human InteractionJava Swing●Top-level containers consist of a JRootPane, the area under the title bar, with a JLayeredPane child that manages the menu bar, if there is one, with a Container child, called the content pane, where GUI components are managed.●Characteristics of the environment can be obtained from the singleton Toolkit object.●Look at Pizza1App.javaSeptember 29, 2009 CS 350 Lecture 10 7CS 350: Computer/Human InteractionJava Applications●A Java application class extends a JFrame or has a JFrame data member; constructor creates GUI.●Swing is not thread-safe, so use SwingUtilities.invokeLater() with new anonymous inner class that implements Runnable interface to create application.●The event-dispatcher will execute the code in the run method in a separate thread.September 29, 2009 CS 350 Lecture 10 8CS 350: Computer/Human InteractionGUI Components●JLabel – label with text●JTextField – one line of text●JTextArea – multiple lines of text●JRadioButton – radio button, grouped using ButtonGroup●JCheckBox – check boxes●JButton – button with text labelSeptember 29, 2009 CS 350 Lecture 10 9CS 350: Computer/Human InteractionGUI Components●JPanel – container with layout, used to position components●Text can include HTML tags●Most components allow their border to be set; BorderFactory class contains methods for creating various border optionsSeptember 29, 2009 CS 350 Lecture 10 10CS 350: Computer/Human InteractionJava Events●GUI interaction is event-driven similar to VB●Event-dispatching thread is automatically created●Events include: clicks, keypress, mouse move, etc.●Event-handling interfaces defined by AWTSeptember 29, 2009 CS 350 Lecture 10 11CS 350: Computer/Human InteractionListeners●Interaction is provided by adding a listener to a component●Listeners are interfaces with methods that must be implemented. E.g., ActionListener interface has actionPerformed; ItemListener interface has itemStateChanged●Often use an anonymous inner class to implement these interfaces.September 29, 2009 CS 350 Lecture 10 12CS 350: Computer/Human InteractionLayout Managers●Layout manager determines where components appear in a panel; each panel can have a different layout manager●FlowLayout: default panel manager; left to right placement●BorderLayout: default root pane manager; north, south, west, east, and center placementsSeptember 29, 2009 CS 350 Lecture 10 13CS 350: Computer/Human InteractionLayout Managers●GridLayout: equal-sized cells of specified number of rows and columns (0 is undetermined); left to right, top to bottom default placement ●BoxLayout: centered placement, either horizontally or vertically●SpringLayout: most general, placement determined by spring constraints between componentsSeptember 29, 2009 CS 350 Lecture 10 14CS 350: Computer/Human InteractionMore GUI Components●Look at Pizza2App.java●JComboBox – construct with array of string items–Uses ActionListener interface, dynamically cast the source of the event object into a JComboBox and get the selected itemSeptember 29, 2009 CS 350 Lecture 10 15CS 350: Computer/Human InteractionMore GUI Components●JList – construct with array of string items–Uses ListSelectionListener interface with valueChanged function–MULTIPLE_INTERVAL_SELECTION mode is default, also SINGLE_SELECTION and SINGLE_INTERVAL_SELECTION modes–getSelectedIndices method returns int array of indices of selected itemsSeptember 29, 2009 CS 350 Lecture 10 16CS 350: Computer/Human InteractionMessage Dialogs●Simple OK message dialogs can be created usingJOptionPane.showMessageDialog()●Arguments are the top-level frame, dialog message, window title, and message type (determines the icon); can also add icon image argumentSeptember 29, 2009 CS 350 Lecture 10 17CS 350: Computer/Human InteractionMenu Bars and Menus●JMenuBar is added to top-level layered pane: setJMenuBar(createMenuBar());●Default placement is platform-dependent●Add JMenu objects to menu bar●Add JMenuItem, JMenu objects to menus●Uses ActionListener interfaceSeptember 29, 2009 CS 350 Lecture 10 18CS 350: Computer/Human InteractionFile Output●General paradigm is to wrap a buffered stream around an unbuffered stream around a file.●Using a file must be in try-catch blockPrintWriter outputStream = null;try { outputStream = new PrintWriter( new FileWriter ("output/orders.txt")); outputStream.println (...);} catch (IOException ex) { // error code} finally { // clean up code }September 29, 2009 CS 350 Lecture 10 19CS 350: Computer/Human InteractionMiscellaneous●Added keyboard mnemonics to buttonssubmit.setMnemonic (KeyEvent.VK_S);Underlines the letter in the labelSeptember


View Full Document

UE CS 350 - Lecture 10 Overview

Download Lecture 10 Overview
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10 Overview and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10 Overview 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?