DOC PREVIEW
CORNELL CS 211 - More on GUIs

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS211Fall 20002■ Declaring the characteristics of an ADT● Examples: Collection, Set, List■ Declaring a single characteristic that is shared by several top-level, stand-alone classes (e.g., String, Integer)● Examples: Comparable, Cloneable■ Declaring a single helper method that is thus attached to an object; the object can be stored or passed as an argument● Examples: Comparator, various Event Listeners3■ In general, a class that implements Comparable has many other methods■ In general, a class that implements Comparator contains just the single method compare( )■ This isn’t quite true because a single class can be made to serve multiple purposes (i.e., you can define a single class that implements Comparable, Comparator, Set, andActionListener)Comparing Objects x and y■ Intuitionif (x < y)…■ When you want the natural order// Can throw ClassCastExceptionComparable cx = (Comparable)x;if (cx.compareTo(y) < 0)…■ When using a Comparator (com)// Can throw a ClassCastExceptionif (com.compare(x,y) < 0)…■ When you might be using a Comparatorif (com == null) {Comparable cx = (Comparable)x;if (cx.compareTo(y) < 0)…}elseif (com.compare(x,y) < 0)…4■ java.awt.Point defines a 2D point with integer coordinates■ If p is a Point then● p.x is the x-coordinate● p.y is the y-coordinate■ Goal: use lexicographic ordering (i.e., the first coordinate determines the order, but if they’re tied, use the second coordinate)import java.awt.Point;class MyPointComparator implements Comparator {public int compare (Object a, Object b) {Point pa = (Point) a;Point pb = (Point) b;if (pa.x <p b.x) return –1;if (pa.x > pb.x) return 1;if (pa.y < pb.y) return –1;if (pa.y > pb.y) return 1;return 0;}}5■ If we store Points in a SortedSet then it’s easy to print them in lexicographic order■ Assume we start with an array p of Points■ Attempt #1:SortedSet s = new SortedSet(java.util.Arrays.asList(p));● This fails (throws a ClassCastException) because Points are not Comparable■ Attempt #2:SortedSet s = new SortedSet(new MyPointComparator());s = s.addAll(java.util.Arrays.asList(p)); // Bulk add● This succeeds because we provided a Comparator● If I try to put a non-Point into s then my compare() method throws a ClassCastException6■ Layout● How items are arranged● There are lots of predefined GUI items JButton, JLabel, JCheckbox, JList, JScrollbar,…● You have to write the code that determines layout● In Java, you use LayoutManagers to help with layout■ Event Handling● An event is (generally) a user input or action● The JVM (Java Virtual Machine) takes care of generating eventsButton pushed, text typed, mouse clicked,…● You have to write the code that determines how your program responds to an event7public static void main (String[ ] args) {JFrame frame = new JFrame();JPanel panel = new JPanel();JPanel topPanel = new JPanel();JPanel botPanel = new JPanel();topPanel.add(new JButton("One"));topPanel.add(new JButton("Two"));botPanel.add(new JButton("Three"));botPanel.add(new JButton("Four"));botPanel.add(new JButton("Five"));panel.setLayout(new BorderLayout());panel.add(topPanel,BorderLayout.NORTH);panel.add(botPanel,BorderLayout.SOUTH);frame.getContentPane().add(panel);frame.setSize(250,300);frame.setVisible(true);}8import javax.swing.*;import java.awt.GridLayout;import java.awt.BorderLayout;class GUITest {public static void main (String[ ] args) {JFrame frame = new JFrame();JPanel panel = new JPanel();panel.setLayout(new GridLayout(0,1));panel.add(new JButton("One"));panel.add(new JButton("Two"));panel.add(new JButton("Three"));panel.add(new JButton("Four"));panel.add(new JButton("Five"));frame.getContentPane().add(panel,BorderLayout.WEST);frame.setSize(250,300);frame.setVisible(true);} }9■ The JVM (Java Virtual Machine) determines the event’s source and type● The source is the component from which the event originated● Each source has certain types of events it can generate■ The JVM looks for one or more event listeners that have registeredwith the source● An event listener is an object that implements one of the Listener interfaces in java.awt.event or in javax.swing.event● You register an event listener by using one of the component’saddListener methods■ The JVM creates an event object using one of the classes in java.awt.event or in javax.swing.event■ For each registered event listener, the JVM invokes the listener's event-handling method and passes the event object as the parameter10import javax.swing.*; import java.awt.event.*;import java.awt.Color;class GUITest {static String[ ] name = {"red","blue","green","magenta","cyan","yellow"};static Color[ ] color = {Color.red,Color.blue,Color.green,Color.magenta,Color.cyan,Color.yellow};public static void main (String[ ] args) {JFrame frame = new JFrame();JPanel panel = new JPanel();for (int i = 0; i < name.length; i++) {JButton button = new JButton(name[i]);panel.add(button);button.addActionListener(new MyListener(panel,color[i]));}frame.getContentPane().add(panel);frame.setSize(250,300);frame.setVisible(true);}11■ Class for color manipulation■ Constants● black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow■ Constructors (2 of several)● Color (int r, int g, int b); // 0 to 255● Color (float r, float g, float b); // 0.0 to 1.0■ Methods● lighter( ), darker( ), getRed( ), getGreen( ), getBlue( ), getHSBColor( ),…12static class MyListener implements ActionListener {Color myColor;JPanel myPanel;MyListener (JPanel panel, Color color) {myColor = color;myPanel = panel;}public void actionPerformed (ActionEvent event) {myPanel.setBackground(myColor);}}}13■ The user clicks a button■ The JVM examines the button to see if any ActionListeners have registered with the button● If the button is supposed to do something, an ActionListener must have registered with the button prior to the button-click● Use button.addActionListener(…) to register● You can register a Listener at any time, but this is typically done when the button is created (as in the example)■ The JVM creates an ActionEvent (call it e)■ For each registered ActionListener x, the JVM calls x.actionPerformed(e)● This is where/when your code (telling what should happen when the button is clicked) is executed● In the example, the ActionListener remembers the panel and a color when it’s created; when the action is performed (i.e., the button is


View Full Document

CORNELL CS 211 - More on GUIs

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download More on GUIs
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 More on GUIs 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 More on GUIs 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?