Archive for February, 2008

700 Graphical User Interface (Web hosting domain names) Components: Part 1 Chapter

Friday, February 22nd, 2008

700 Graphical User Interface Components: Part 1 Chapter 12 18 private Container container; 19 private GridLayout grid1, grid2; 20 21 // set up GUI 22 public GridLayoutDemo() 23 { 24 super( “GridLayout Demo” ); 25 26 // set up layouts 27 grid1 = new GridLayout( 2, 3, 5, 5 ); 28 grid2 = new GridLayout( 3, 2 ); 29 30 // get content pane and set its layout 31 container = getContentPane(); 32 container.setLayout( grid1 ); 33 34 // create and add buttons 35 buttons = new JButton[ names.length ]; 36 37 for ( int count = 0; count < names.length; count++ ) { 38 buttons[ count ] = new JButton( names[ count ] ); 39 buttons[ count ].addActionListener( this ); 40 container.add( buttons[ count ] ); 41 } 42 43 setSize( 300, 150 ); 44 setVisible( true ); 45 } 46 47 // handle button events by toggling between layouts 48 public void actionPerformed( ActionEvent event ) 49 { 50 if ( toggle ) 51 container.setLayout( grid2 ); 52 else 53 container.setLayout( grid1 ); 54 55 toggle = !toggle; // set toggle to opposite value 56 container.validate(); 57 } 58 59 // execute application 60 public static void main( String args[] ) 61 { 62 GridLayoutDemo application = new GridLayoutDemo(); 63 64 application.setDefaultCloseOperation( 65 JFrame.EXIT_ON_CLOSE ); 66 } 67 68 } // end class GridLayoutDemo Fig. 12.26 Program that demonstrates components in GridLayout. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface (Geocities web hosting) Components: Part 1

Friday, February 22nd, 2008

Chapter 12 Graphical User Interface Components: Part 1 699 Look-and-Feel Observation 12.11 If no region is specified when adding a Component to a BorderLayout, it is assumed that the Componentshould be added to region BorderLayout.CENTER. Common Programming Error 12.6 Adding more than one component to a particular region in a BorderLayout results in only the last component added being displayed. There is no error message to indicate this problem. When the user clicks on a particular JButton in the layout, method actionPerformed(lines 50 61) executes. The for loop at lines 52 57 uses an if/elsestructure to hide the particular JButtonthat generated the event. Method setVisible(inherited into JButton from class Component) is called with a false argument to hide the JButton. If the current JButton in the array is not the one that generated the event, method setVisibleis called with a trueargument to ensure that the JButtonis displayed on the screen. Line 60 uses LayoutManager method layoutContainer to recalculate the layout of the content pane. Notice in the screen captures of Fig. 12.25 that certain regions in the BorderLayout change shape as JButtons are hidden and displayed in other regions. Try resizing the application window to see how the various regions resize based on the width and height of the window. 12.14.3 GridLayout The GridLayoutlayout manager divides the container into a grid so that components can be placed in rows and columns. Class GridLayoutinherits directly from class Object and implements interface LayoutManager. Every Componentin a GridLayouthas the same width and height. Components are added to a GridLayout starting at the top- left cell of the grid and proceeding left-to-right until the row is full. Then the process continues left-to-right on the next row of the grid, etc. Figure 12.26 demonstrates the Grid- Layoutlayout manager using six JButtons. 1 // Fig. 12.26: GridLayoutDemo.java 2 // Demonstrating GridLayout. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class GridLayoutDemo extends JFrame 12 implements ActionListener { 13 14 private JButton buttons[]; 15 private String names[] = 16 { “one”, “two”, “three”, “four”, “five”, “six” }; 17 private boolean toggle = true; Fig. 12.26 Program that demonstrates components in GridLayout. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web site layout - 698 Graphical User Interface Components: Part 1 Chapter

Thursday, February 21st, 2008

698 Graphical User Interface Components: Part 1 Chapter 12 67 68 application.setDefaultCloseOperation( 69 JFrame.EXIT_ON_CLOSE ); 70 } 71 72 } // end class BorderLayoutDemo Fig. 12.25 Demonstrating components in BorderLayout(part 3 of 3). Line 24 in the constructor defines a BorderLayout. The arguments specify the number of pixels between components that are arranged horizontally (horizontal gap space) and the number of pixels between components that are arranged vertically (vertical gap space), respectively. The default BorderLayoutconstructor supplies 0 pixels of gap space horizontally and vertically. Line 28 uses method setLayout to set the content pane s layout to layout. Adding Components to a BorderLayout requires a different add method from class Container, which takes two arguments the Component to add and the region in which the Component will be placed. For example, line 39 specifies that the buttons[0] should appear in the NORTH position. The components can be added in any order, but only one component can be added to each region. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting top - Chapter 12 Graphical User Interface Components: Part 1

Wednesday, February 20th, 2008

Chapter 12 Graphical User Interface Components: Part 1 697 14 private JButton buttons[]; 15 private String names[] = { “Hide North”, “Hide South”, 16 “Hide East”, “Hide West”, “Hide Center” }; 17 private BorderLayout layout; 18 19 // set up GUI and event handling 20 public BorderLayoutDemo() 21 { 22 super( “BorderLayout Demo” ); 23 24 layout = new BorderLayout( 5, 5 ); 25 26 // get content pane and set its layout 27 Container container = getContentPane(); 28 container.setLayout( layout ); 29 30 // instantiate button objects 31 buttons = new JButton[ names.length ]; 32 33 for ( int count = 0; count < names.length; count++ ) { 34 buttons[ count ] = new JButton( names[ count ] ); 35 buttons[ count ].addActionListener( this ); 36 } 37 38 // place buttons in BorderLayout; order not important 39 container.add( buttons[ 0 ], BorderLayout.NORTH ); 40 container.add( buttons[ 1 ], BorderLayout.SOUTH ); 41 container.add( buttons[ 2 ], BorderLayout.EAST ); 42 container.add( buttons[ 3 ], BorderLayout.WEST ); 43 container.add( buttons[ 4 ], BorderLayout.CENTER ); 44 45 setSize( 300, 200 ); 46 setVisible( true ); 47 } 48 49 // handle button events 50 public void actionPerformed( ActionEvent event ) 51 { 52 for ( int count = 0; count < buttons.length; count++ ) 53 54 if ( event.getSource() == buttons[ count ] ) 55 buttons[ count ].setVisible( false ); 56 else 57 buttons[ count ].setVisible( true ); 58 59 // re-layout the content pane 60 layout.layoutContainer( getContentPane() ); 61 } 62 63 // execute application 64 public static void main( String args[] ) 65 { 66 BorderLayoutDemo application = new BorderLayoutDemo(); Fig. 12.25 Demonstrating components in BorderLayout(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

696 Graphical User Interface Components: Part 1 Chapter (Php web hosting)

Tuesday, February 19th, 2008

696 Graphical User Interface Components: Part 1 Chapter 12 Each button s actionPerformed event handler executes two statements. For example, line 38 in method actionPerformed for button left uses FlowLayout method setAlignmentto change the alignment for the FlowLayoutto a left-aligned (FlowLayout.LEFT) FlowLayout. Line 41 uses LayoutManager interface method layoutContainerto specify that the content pane should be rearranged based on the adjusted layout. According to which button was clicked, the actionPerformed method for each button sets the FlowLayout s alignment to FlowLayout.LEFT, Flow- Layout.CENTERor FlowLayout.RIGHT. 12.14.2 BorderLayout The BorderLayoutlayout manager (the default layout manager for the content pane) arranges components into five regions: NORTH, SOUTH, EAST, WESTand CENTER(North corresponds to the top of the container). Class BorderLayout inherits from Object and implements interface LayoutManager2 (a subinterface of LayoutManager that adds several methods for enhanced layout processing). Up to five components can be added directly to a BorderLayout one for each region. The component placed in each region can be a container to which other components are attached. The components placed in the NORTHand SOUTHregions extend horizontally to the sides of the container and are as tall as the components placed in those regions. The EASTand WESTregions expand vertically between the NORTHand SOUTHregions and are as wide as the components placed in those regions. The component placed in the CENTER region expands to take all remaining space in the layout (this is the reason the JTextArea in Fig. 12.22 occupies the entire window). If all five regions are occupied, the entire container s space is covered by GUI components. If the NORTHor SOUTHregion is not occupied, the GUI components in the EAST, CENTER and WEST regions expand vertically to fill the remaining space. If the EASTor WEST region is not occupied, the GUI component in the CENTER region expands horizontally to fill the remaining space. If the CENTER region is not occupied, the area is left empty the other GUI components do not expand to fill the remaining space. The application of Fig. 12.25 demonstrates the BorderLayout layout manager by using five JButtons. 1 // Fig. 12.25: BorderLayoutDemo.java 2 // Demonstrating BorderLayout. 3 4 // Java core packages 5 import java.awt.*; 6 import java.awt.event.*; 7 8 // Java extension packages 9 import javax.swing.*; 10 11 public class BorderLayoutDemo extends JFrame 12 implements ActionListener { 13 Fig. 12.25 Demonstrating components in BorderLayout(part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 12 Graphical User Interface Components: Part 1 (Web hosting support)

Tuesday, February 19th, 2008

Chapter 12 Graphical User Interface Components: Part 1 695 79 // process rightButton event 80 public void actionPerformed( ActionEvent event ) 81 { 82 layout.setAlignment( FlowLayout.RIGHT ); 83 84 // re-align attached components 85 layout.layoutContainer( container ); 86 } 87 } 88 ); 89 90 container.add( rightButton ); 91 92 setSize( 300, 75 ); 93 setVisible( true ); 94 } 95 96 // execute application 97 public static void main( String args[] ) 98 { 99 FlowLayoutDemo application = new FlowLayoutDemo(); 100 101 application.setDefaultCloseOperation( 102 JFrame.EXIT_ON_CLOSE ); 103 } 104 105 } // end class FlowLayoutDemo Fig. 12.24 Program that demonstrates components in FlowLayout(part 3 of 3). As seen previously, a container s layout is set with method setLayout of class Container. Line 25 sets the content pane s layout manager to the FlowLayoutdefined at line 21. Normally, the layout is set before any GUI components are added to a container. Look-and-Feel Observation 12.10 Each container can have only one layout manager at a time. (Separate containers in the same program can have different layout managers.) Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

694 Graphical User Interface Components: Part 1 (Dedicated web hosting) Chapter

Monday, February 18th, 2008

694 Graphical User Interface Components: Part 1 Chapter 12 27 // set up leftButton and register listener 28 leftButton = new JButton( “Left” ); 29 30 leftButton.addActionListener( 31 32 // anonymous inner class 33 new ActionListener() { 34 35 // process leftButton event 36 public void actionPerformed( ActionEvent event ) 37 { 38 layout.setAlignment( FlowLayout.LEFT ); 39 40 // re-align attached components 41 layout.layoutContainer( container ); 42 } 43 44 } // end anonymous inner class 45 46 ); // end call to addActionListener 47 48 container.add( leftButton ); 49 50 // set up centerButton and register listener 51 centerButton = new JButton( “Center” ); 52 53 centerButton.addActionListener( 54 55 // anonymous inner class 56 new ActionListener() { 57 58 // process centerButton event 59 public void actionPerformed( ActionEvent event ) 60 { 61 layout.setAlignment( FlowLayout.CENTER ); 62 63 // re-align attached components 64 layout.layoutContainer( container ); 65 } 66 } 67 ); 68 69 container.add( centerButton ); 70 71 // set up rightButton and register listener 72 rightButton = new JButton( “Right” ); 73 74 rightButton.addActionListener( 75 76 // anonymous inner class 77 new ActionListener() { 78 Fig. 12.24 Program that demonstrates components in FlowLayout(part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

692 Graphical User Interface Components: Part 1 Chapter (Hp web site)

Sunday, February 17th, 2008

692 Graphical User Interface Components: Part 1 Chapter 12 taining the name of the key that was pressed. For a complete list of virtual key constants, see the on-line documentation for class KeyEvent (package java.awt.event). Method keyTyped(lines 51 55) uses KeyEventmethod getKeyCharto get the Unicode value of the character typed. All three event handling methods finish by calling method setLines2and3 (lines 58 72) and passing it the KeyEvent object. This method uses KeyEvent method isActionKey to determine if the key in the event was an action key. Also, InputEventmethod getModifiersis called to determine if any modifier keys (such as Shift, Alt and Ctrl) were pressed when the key event occurred. The result of this method is passed to KeyEventmethod getKeyModifiersText, which produces a string containing the names of the pressed modifier keys. [Note: If you need to test for a specific key on the keyboard, class KeyEventprovides a key constant for every key on the keyboard. These constants can be used from the key event handlers to determine if a particular key was pressed. Also, to determine whether the Alt, Ctrl, Meta and Shift keys are pressed individually, InputEvent methods isAlt- Down, isControlDown, isMetaDown and isShiftDown each return a boolean indicating if the particular key was pressed during the key event.] 12.14 Layout Managers Layout managers are provided to arrange GUI components on a container for presentation purposes. The layout managers provide basic layout capabilities that are easier to use than determining the exact position and size of every GUI component. This enables the programmer to concentrate on the basic look and feel and lets the layout managers process most of the layout details. Look-and-Feel Observation 12.9 Most Java programming environments provide GUI design tools that help a programmer graphically design a GUI, then automatically write Java code to create the GUI. Some GUI designers also allow the programmer to use the layout managers described here and in Chapter 13. Figure 12.23 summarizes the layout managers presented in this chapter. Other layout managers are discussed in Chapter 13. Layout manager Description FlowLayout Default for java.awt.Applet, java.awt.Paneland javax.swing.JPanel. Places components sequentially (left to right) in the order they were added. It is also possible to specify the order of the components using the Containermethod addthat takes a Componentand an integer index position as arguments. BorderLayout Default for the content panes of JFrames (and other windows) and JApplets. Arranges the components into five areas: North, South, East, West and Center. GridLayout Arranges the components into rows and columns. Fig. 12.23 Layout managers. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Apache web server for windows - Chapter 12 Graphical User Interface Components: Part 1

Saturday, February 16th, 2008

Chapter 12 Graphical User Interface Components: Part 1 691 73 74 // execute application 75 public static void main( String args[] ) 76 { 77 KeyDemo application = new KeyDemo(); 78 79 application.setDefaultCloseOperation( 80 JFrame.EXIT_ON_CLOSE ); 81 } 82 83 } // end class KeyDemo Fig. 12.22 Fig. 12.22 Demonstrating key event-handling (part 3 of 3). The constructor (lines 17 32) registers the application to handle its own key events with method addKeyListener at line 28. Method addKeyListener is defined in class Component, so every subclass of Componentcan notify KeyListeners of key events for that Component. Line 25 in the constructor adds JTextAreatextArea(where the program s output is displayed) to the content pane. Notice, in the screen captures, that textAreaoccupies the entire window. This is due to the content pane s default BorderLayout (discussed in Section 12.14.2 and demonstrated in Fig. 12.25). When a single Component is added to a BorderLayout, the Componentoccupies the entire Container. Methods keyPressed (lines 35 40) and keyReleased (lines 43 48) use KeyEventmethod getKeyCodeto get the virtual key code of the key that was pressed. Class KeyEvent maintains a set of constants the virtual key code constants that represent every key on the keyboard. These constants can be compared with the return value of getKeyCodeto test for individual keys on the keyboard. The value returned by get- KeyCodeis passed to KeyEventmethod getKeyText, which returns a Stringcon Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

690 Graphical (Hosting your own web site) User Interface Components: Part 1 Chapter

Friday, February 15th, 2008

690 Graphical User Interface Components: Part 1 Chapter 12 20 21 // set up JTextArea 22 textArea = new JTextArea( 10, 15 ); 23 textArea.setText( “Press any key on the keyboard…” ); 24 textArea.setEnabled( false ); 25 getContentPane().add( textArea ); 26 27 // allow frame to process Key events 28 addKeyListener( this ); 29 30 setSize( 350, 100 ); 31 setVisible( true ); 32 } 33 34 // handle press of any key 35 public void keyPressed( KeyEvent event ) 36 { 37 line1 = “Key pressed: ” + 38 event.getKeyText( event.getKeyCode() ); 39 setLines2and3( event ); 40 } 41 42 // handle release of any key 43 public void keyReleased( KeyEvent event ) 44 { 45 line1 = “Key released: ” + 46 event.getKeyText( event.getKeyCode() ); 47 setLines2and3( event ); 48 } 49 50 // handle press of an action key 51 public void keyTyped( KeyEvent event ) 52 { 53 line1 = “Key typed: ” + event.getKeyChar(); 54 setLines2and3( event ); 55 } 56 57 // set second and third lines of output 58 private void setLines2and3( KeyEvent event ) 59 { 60 line2 = “This key is ” + 61 ( event.isActionKey() ? “” : “not ” ) + 62 “an action key”; 63 64 String temp = 65 event.getKeyModifiersText( event.getModifiers() ); 66 67 line3 = “Modifier keys pressed: ” + 68 ( temp.equals( “” ) ? “none” : temp ); 69 70 textArea.setText( 71 line1 + “n” + line2 + “n” + line3 + “n” ); 72 } Fig. 12.22 Fig. 12.22 Demonstrating key event-handling (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01