360 likes | 441 Views
Software Engineering. Recitation 8 Suhit Gupta. Today. Regular Expressions javax.swing. Regular Expressions. For chat matching in the ChatScript Useful for AI’s in particular. Regular Expressions: Links. http://www.perldoc.com/perl5.6/pod/perlre.html Ouch
E N D
Software Engineering Recitation 8 Suhit Gupta
Today • Regular Expressions • javax.swing
Regular Expressions • For chat matching in the ChatScript • Useful for AI’s in particular
Regular Expressions: Links • http://www.perldoc.com/perl5.6/pod/perlre.html • Ouch • http://www.oreilly.com/catalog/regex/ • Slightly overkill • We’ll use the gnu.regexp classes • You basically use it in place of .equals() • http://www.softe.cs.columbia.edu/jars/gnu.regexp-1.1.4/docs/index.html
Regular Expressions: Intro • In the simplest form, it’s like wildcards • * matches 0 or more of preceding expression • + matches 1 or more • ? matches 0 or 1 • Ex1: aa* = a+ • Ex2: a*b?c+ matches: • aaaabc, ccccc, bccc, abc • Does not match • abbcc, aaab
Regular Expressions: A bit more • Can use [] operators to designate a choice • ab[xyz]c matches: • abxc, abyc, abzc only • ab[0-9]c matches • ab0c, ab1c, …, ab9c • ab[a-zA-Z1-4]c matches • abac, abZc, ab3c • not ab5c • Can also combine wildcards, such as ab[a-z]*c • abzzaaccbbddeeffggc
Regular Expressions: The Dot • ‘.’ matches any character (like DOS wildcard) • ab.c matches abbc, abqc, ab6c, ab(c, ab^c • Common: .* • ab.*c matches any string beginning with ab and ending in c • \. matches a literal period
Regular Expressions: Using • bash, egrep have decent regular expression support • gnu.regexp has a sample applet associated with it, you can try it out • We’ll give sample code, recitation will also cover • It all comes from Perl • Perl = two languages in one • Amazing string processing • Practical Extraction and Report Language • Pathologically Eclectic Rubbish Lister
A Bit of History • With each major release of the Java programming language, an updated version of the GUI code has also been released • Java 1.0 - Basic Interface Components • Java 1.1 - New Event Handling Methods • Java 1.2 - Introduction of Swing
First things first! • import javax.swing.*; • import java.awt.*; • import java.awt.event.*; • These three packages will need to be imported in nearly all GUI based application. They contain most of the underlying classes that provide for GUI development.
Introduction • You can identify Swing components because their names start with J. The AWT button class, for example, is named Button, while the Swing button class is named JButton. • The AWT components are in the java.awt package, while the Swing components are in the javax.swing package. • The biggest difference between the AWT components and Swing components is that the Swing components are implemented with absolutely no native code. Since Swing components aren't restricted to the least common denominator – the features that are present on every platform – they can have more functionality than AWT components.
Swing capabilities • Swing buttons and labels can display images instead of, or in addition to, text. • You can easily add or change the borders drawn around most Swing components. For example, it's easy to put a box around the outside of a container or label. • You can easily change the behavior or appearance of a Swing component by either invoking methods on it or creating a subclass of it.
Swing capabilities [II] • Swing components don't have to be rectangular. Buttons, for example, can be round. • Assistive technologies such as screen readers can easily get information from Swing components. For example, a tool can easily get the text that's displayed on a button or label. • Swing lets you specify which look and feel your program's GUI uses. By contrast, AWT components always have the look and feel of the native platform.
Swing drawbacks • Programs should not use AWT components alongside Swing components. This is because when Swing components overlap AWT components, the AWT component is always painted on top. • The containment hierarchy for any window or applet that contains Swing components must have a Swing top-level container at the root of the hierarchy. For example, a main window should be implemented as a JFrame instance rather than as a Frame instance. • You don't add components directly to a top-level container such as a JFrame. Instead, you add components to a container (called the content pane) that is itself contained by the JFrame. Again, we’ll see what this looks like in the examples.
Swing Components • Here’s a visual index of swing components • A simple example: an Address Book GUI written as an applet with swing components. I’ll introduce the following components: • JApplet • JtabbedPane (and adding icons to the tabs) • JTextField • JtextArea • JComboBox • JPanel • JscrollPane (but I’m still learning this one, so it’s not quite work correctly)
Swing Containers JFrame • Frames are the simplest and most commonly used type of container. A standard GUI based application will typically contain at least one frame. JDialog • A dialog box is just like a dialog box in Windows. Usually used for warning messages, helping the user, or asking for information.
Swing Containers JPanel • A panel is a second level container that is used to hold components within a top level container such as a frame or dialog. Various Types of JPanes • JDesktopPane - allows for a virutal desktop, MDI • JTextPane - allows graphical representation of attributes ( Bolding, Italics, and the like...) • JTabbedPane - allows switching between panes by choosing the appropriate tab • and more….
Components Components are the various items that are placed within containers. These include (among many others) : • JLabel • JButton • JRadioButton • JList • JComboBox • Text Components (JTextField, JTextArea) • JTable
Components • JLabel - a basic textual label • JButtons - a button with text and/or graphic labels • JRadioButtons - on/off buttons where only on or off may be selected within a group • JList - contains a list of strings, one or more of which can be selected, scrollable • JComboBox - contains strings in a drop down box, one of which can be selected, also has a textfield for input
Components • Text Components - a single line or multi-line box for handling text input (JTextField, JTextArea) • JTable - a multicolumn, multirow table for allowing data presentation and input
Layouts Layouts are the manner of defining how you want the different components to appear within a container. BorderLayout BoxLayout CardLayout FlowLayout GridLayout GridBagLayout and the infamous… Null Layout
Layouts • BorderLayout • The standard layout for a content pane • Put components at positions “North”, “South”, “East”, “West”, and “Center” • BoxLayout • Puts components in one row or one column • Preserves component size
Layouts • CardLayout • Allows one to have different components appear in the same area at different times • “Switch” cards to change to next set of components • FlowLayout • The standard layout for a JPanel • Places components left to right
Layouts • GridLayout • Sets up specified number of rows and colums • Makes all components the same size • GridBagLayout • Places components in different sized grid cells • Most flexible layout, resizes best • Define GridBagConstraints • Null Layout • Absolute positioning (x,y, width, height)
Look and Feel • With Java Look and Feel, a programmer has the ability to define a unique look for his interface, in such areas as button and border styles, or to use one of the current OS interfaces. • Four basic LookAndFeels are defined with Java 1.2 : • JavaLookAndFeel (Metal) • MotifLookAndFeel (XWindows Look) • WindowsLookAndFeel (only on Win32) • MacLookAndFeel (only on MacOS)
Look and Feel • Look and Feel can be set in a program with one line of code : UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows. WindowsLookAndFeel"); sets the WindowsLookAndFeel
Look and Feel • To create your own LookAndFeel, extend class javax.swing.plaf.basic.BasicLookAndFeel • Give it an id, name, description, whether it is native to the current OS, and whether supported by OS • set Class, Component, and Color Defaults where you define how Components will look when drawn
Event Handling Introduction • Event handling is the concept of responding to user actions. The technique designed for event handling in the current version of Java is based on the idea of a Listener. • Listeners are interfaces - this means any object can respond to user events if they implement the interface. • import java.awt.event.*; • import javax.swing.event.*;
Event Handling Basics • Most components that a user can interact with create Event objects when the user has performed some action. These include : • WindowEvent - actions on the Frame or Dialog • ActionEvent - button presses • MouseEvent - mouse clicks, mouse selects • MouseMotionEvent - mouse movement • KeyboardEvent - key presses among many others….
Event Handling Basics • An object implements the interface for the type of Events it wants to respond to • Listening for Window Events? Implement WindowListener • Listening for Button Presses? Implement ActionListener • Each of the interfaces defines methods appropriate for handling the different types of events of that type
WindowListener Example • For example, the WindowListener interface consists of : public void windowActivated(WindowEvent event) public void windowDeactivated(WindowEvent event) public void windowClosed(WindowEvent event) public void windowClosing(WindowEvent event) public void windowIconfied(WindowEvent event) public void windowDeiconified(WindowEvent event) public void windowOpened(WindowEvent event)
Event Handling Designing Your Own Events • You can also design your own events and listeners - • Define the event (extend java.util.EventObject) • Define the listener interface (extends java.util.EventListener) • Have the object that creates the event have an addXListener() method that registers the listener with the object (ie the object can store all registered listeners in a Vector) and that calls the appropriate listener interface functions when needed on all registered listeners
Menus • Menus can easily be added to windows. • You need to : • Create a JMenuBar that contains JMenus, made up of JMenuItems, and Jseparators, used for spacing. • Set the JMenuBar on the window of choice. • Add listeners to handle menu selections.
Applets • Applets are small applications that can be run within a web browser. • An Applet is basically an extension of java.awt.Panel with some control mechanisms (init(), start(), stop()) • You can add components, change the way the Panel paints, call any Panel (as well as Container and Component) methods • You can also use the Panel as root of a full fledged GUI with windows
Applets • Applets are called from HTML files. • A basic applet tag looks like : <APPLET CODE=classfilename HEIGHT = height WIDTH = width CODEBASE = codebase > < PARAM name = VALUE> • To view Swing applets, you have to download the applet plugin for Java 1.2. Regular AWT code (Java 1.1) should work in most current browsers.