More Swing CS180 Recitation 12/(04,05)/08
Announcements No lecture/labs next week Recitations and evening consulting hours will be held as usual. Debbie's study group on tuesday and office hours on thursday as usual. Attend recitations and study group for final exam help and tips.
Applets Remember to import both java.awt.* and java.awt.event.* You can add icons as ImageIcon objects that represent a picture. There are no restrictions on the size of the ImageIcon.
HTML Language for writing webpages. Command format <Command> </Command> e.g. <head> <title> </title> </head> You can think of these commands as 'containers', just like Java uses JFrames, JApplets and JPanels as containers.
Displaying a Picture HTML documents can contain both text and pictures. Common to place picture files in images/picture_file.
Applets in HTML Applets may be imbedded in HTML documents, listing 14.6a JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 6 6
Applets in HTML Applets may be imbedded in HTML documents listing 14.6b JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 7 7
Browser Output Listing 14.6c JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN 0136130887 2008 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved 8 8
Applets and Security In Slide 7, when embedding an applet, code= AdderApplet.class and code= AdderApplet are equivalent. Applets may not access files or run programs on a remote machine. Although this restricts what applets can do, its important for security reasons. None of us want code someone else wrote to have access to our files! The McGraw-Hill Companies, Inc. Permiss 9
Menus The javax.swing package contains three menurelated classes: JMenuBar, JMenu, and JMenuItem. JMenuBar is a bar where the menus are placed. There is one menu bar per frame. JMenu (such as File or Edit) is a group of menu choices. JMenuBar may include many JMenu objects. JMenuItem (such as Copy, Cut, or Paste) is an individual menu choice in a JMenu object. Only the JMenuItem objects generate events. http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html The McGraw-Hill Companies, Inc. Permiss 10
Menu Components Edit View Help JMenuBar File Edit View Help JMenu JMenuItem separator The McGraw-Hill Companies, Inc. Permiss 11
Sequence for Creating Menus 1. Create a JMenuBar object and attach it to a frame. 2. Create a JMenu object. 3. Create JMenuItem objects and add them to the JMenu object. 4. Attach the JMenu object to the JMenuBar object. The McGraw-Hill Companies, Inc. Permiss 12
Inside a Jframe: JMenu filemenu = new JMenu("File"); JMenuItem Menu Creation Example item; //Add New Option item = new JMenuItem("New"); item.addactionlistener( this ); filemenu.add( item );... //add a horizontal separator line filemenu.addseparator();... JMenuBar menubar = new JMenuBar(); setjmenubar(menubar); //Method of JFrame menubar.add(filemenu); The McGraw-Hill Companies, Inc. Permiss 13
http://java.sun.com/docs/books/tutorial/uiswing/layout/box.htm BoxLayout Aligns components in a line Horizontally or vertically Can install a BoxLayout into a Jcomponent comp.setlayout( new BoxLayout( comp,boxlayout.y_axis ); Or create a Box component Box.create[Vertical/Horizontal]Box(); Box.create[Vertical/Horizontal]Strut( int ); Box.create[Vertical/Horizontal]Glue(); See Sun tutorial: The McGraw-Hill Companies, Inc. Permiss 14
BoxLayout Example P1 P2 C1 C4 C2 C5 C3 C6 The McGraw-Hill Companies, Inc. Permiss 15
CardLayout Helps you manage two or more components (usually JPanel instances) that share the same display space. Conceptually, each component a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html The McGraw-Hill Companies, Inc. Permiss 16
CardLayout Example //Where instance variables are declared: J Pa n e l c a r d s ; final static String BUTTONPANEL = "JPanel with JButtons"; final static String TEXTPANEL = "JPanel with JTextField"; //Where the components controlled by the CardLayout are initialized: //Create t h e "cards". JPanel card1 = new JPanel();... JPanel card2 = new JPanel();... //Create the panel that contains the "cards". cards = new JPanel(new CardLayout()); cards.add(card1, BUTTONPANEL); cards.add(card2, TEXTPANEL); The McGraw-Hill Companies, Inc. Permiss 17
Inner Class A class defined within a class Generally used as a private utility class that does not need to be seen by others Operates as a sub-part of the outer class Can have constructors, instance variables, and methods just like a regular class The McGraw-Hill Companies, Inc. Permiss 18
Advantages of Inner Classes They can make the outer class more self-contained. The methods in the inner class have access to all the instance variables and methods of the outer class, including the private methods and variables. Inner classes are used frequently as listeners to handle events fired by the outer class or by a component of the outer class. The name of the inner class is local to the class in which it is defined, making it possible to have another class with the same name defined outside the class in which it is defined. The McGraw-Hill Companies, Inc. Permiss 19
Inner Class example class Outer { p r i v a t e c l a s s I n n e r { Object data;... private void method() {... }... } } void dosomething() { Inner i = new Inner(); i. m e t h o d ( ) ;... }... The McGraw-Hill Companies, Inc. Permiss 20
Buttons, Menus, and Abstract Buttons 21
WindowListener Receives Window events The class that is interested in processing a window event implements this interface. The listener object created from that class is then registered with a Window using the window's addwindowlistener method. 22
WindowListener Interface windowactivated( WindowEvent e ); windowclosed( WindowEvent e ); windowclosing( WindowEvent e ); windowdeactivated( WindowEvent e ); windowdeiconified( WindowEvent e ); windowiconified( WindowEvent e ); windowopened( WindowEvent e ); 23
WindowAdapter Implements WindowListener All methods are empty Extend this class to create a WindowEvent listener and override the methods for the events of interest. Convenient 24
JScrollPane Examples http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html 25