|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Figure 38.12 shows HelloPlus.java laid out using a GridBagLayout. The code that produced this layout is as follows: public void init() { GridBagLayout theGridBag = new GridBagLayout(); setLayout(theGridBag); GridBagConstraints theConstraints = new GridBagConstraints(); // have all components expand to their largest size theConstraints.fill = GridBagConstraints.BOTH; // set the first label to span a row theConstraints.gridwidth = GridBagConstraints.REMAINDER; theConstraints.weightx = 1.0; Label theHelloLabel = new Label(Hello, World!, java.awt.Label.CENTER); theGridBag.setConstraints(theHelloLabel, theConstraints); add(theHelloLabel); // the text field and its label are a row theConstraints.gridwidth = GridBagConstraints.RELATIVE; theConstraints.weightx = 1.0; fTextField = new TextField(TextField); theGridBag.setConstraints(fTextField, theConstraints); add(fTextField); fLabelForTextField = new Label(Your text is TextField); theConstraints.gridwidth = GridBagConstraints.REMAINDER; theConstraints.weightx = 0.0; theGridBag.setConstraints(fLabelForTextField, theConstraints); add(fLabelForTextField); // make the button double-height setBackground(java.awt.Color.red); theConstraints.gridwidth = 1; theConstraints.gridheight = 2; theConstraints.weightx = 0.0; theConstraints.weighty = 1.0; fButton = new Button(White); theGridBag.setConstraints(fButton, theConstraints); fButton.setBackground(java.awt.Color.white); add(fButton); // let the checkbox end its own row theConstraints.gridwidth = GridBagConstraints.REMAINDER; theConstraints.gridheight = 1; fCheckbox = new Checkbox(Checkbox); theConstraints.weightx = theConstraints.weighty = 0.0; theGridBag.setConstraints(fCheckbox, theConstraints); add(fCheckbox); fDialog = OKDialog.makeDialog(You clicked the checkbox!); // and the choice and corresponding label span another row Choice theChoice = new Choice(); theChoice.addItem(Choice Item 1); theChoice.addItem(Choice Item 2); theChoice.addItem(Choice Item 3); theConstraints.gridwidth = GridBagConstraints.RELATIVE; theConstraints.weightx = 1.0; theGridBag.setConstraints(theChoice, theConstraints); add(theChoice); fLabelForChoice = new Label(You havent chosen anything); theConstraints.gridwidth = GridBagConstraints.REMAINDER; theConstraints.weightx = 0.0; theGridBag.setConstraints(fLabelForChoice, theConstraints); add(fLabelForChoice);
As you can see, most of the changes have to do with manipulating the data members of the GridBagConstraint as we move from row to row and cell to cell. Adding Menus to FramesWe first saw a frame in Listing 38.1. You can attach a MenuBar class to a frame to provide drop-down menu capabilities. To make a menu bar and add it to a frame, write MenuBar myMenuBar = new MenuBar(); myFrame.setMenuBar( myMenuBar ); After you have a menu bar, you can add menus to it. The following code fragment creates a menu called File and adds it to the menu bar: Menu fileMenu = new Menu( File ); myMenuBar.add( fileMenu ); Some windowing systems enable you to create menus that stay up after you release the mouse button. These are referred to as tear-off menus. You can specify that a menu is a tear-off menu when you create it: // true indicates it can be torn off Menu tearOffMenu = new Menu( Tear Me Off, true ); In addition to adding submenus, you will want to add menu items to your menus. Menu items are the parts of a menu the user actually selects. Menus are used to contain menu items as well as submenus. The File menu on many systems, for example, contains menu items such as New, Open, Save, and Save As. You can add menu items to a menu in two ways. You can add an item name by writing // Add an Open option to the file menu fileMenu.add( Open );
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |