|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Heres a code snippet that shows these changes in bold: public void init() { setLayout(new BorderLayout()); add (new Label(Hello, World!), BorderLayout.NORTH); Panel theWestPanel = new Panel(); fTextField = new TextField(TextField); theWestPanel.add(fTextField); fLabelForTextField = new Label(Your text is TextField); theWestPanel.add(fLabelForTextField); add(theWestPanel, BorderLayout.WEST); setBackground(java.awt.Color.red); fButton = new Button(White); fButton.setBackground(java.awt.Color.white); add(fButton, BorderLayout.CENTER); fCheckbox = new Checkbox(Checkbox); add(fCheckbox, BorderLayout.EAST); fDialog = OKDialog.makeDialog(You clicked the checkbox!); Panel theSouthPanel = new Panel(); Choice theChoice = new Choice(); theChoice.addItem(Choice Item 1); theChoice.addItem(Choice Item 2); theChoice.addItem(Choice Item 3); theSouthPanel.add(theChoice); fLabelForChoice = new Label(You havent chosen anything); theSouthPanel.add(fLabelForChoice); add(theSouthPanel, BorderLayout.SOUTH);
Using CardLayoutFigure 38.11 shows how HelloPlus.java looks when you switch to CardLayout. Like BorderLayout, you need to do more than just call setLayout(). CardLayout shows one component at a time; you call next() and previous() to move from one card to another. In the design shown in Figure 38.11, theres a BorderLayout for the application, then a CardLayout in a Panel in the South position to hold the contents. Two buttons are in a Panel in the North position; by clicking those buttons you can issue calls to next() and previous(). The code changes from HelloPlus are public void init() { setLayout(new BorderLayout()); Panel theControls = new Panel(); Button thePreviousButton = new Button(Previous); theControls.add(thePreviousButton); Button theNextButton = new Button(Next); theControls.add(theNextButton); add(theControls, BorderLayout.NORTH); fContents = new Panel(); fContents.setLayout(new CardLayout()); fContents.add (new Label(Hello, World!), Hello); Panel theTextPanel = new Panel(); fTextField = new TextField(TextField); theTextPanel.add(fTextField); fLabelForTextField = new Label(Your text is TextField); theTextPanel.add(fLabelForTextField); fContents.add(theTextPanel, Text); setBackground(java.awt.Color.red); fButton = new Button(White); fButton.setBackground(java.awt.Color.white); fContents.add(fButton, Button); fCheckbox = new Checkbox(Checkbox); fContents.add(fCheckbox, Checkbox); fDialog = OKDialog.makeDialog(You clicked the checkbox!); Panel theChoicePanel = new Panel(); Choice theChoice = new Choice(); theChoice.addItem(Choice Item 1); theChoice.addItem(Choice Item 2); theChoice.addItem(Choice Item 3); theChoicePanel.add(theChoice); fLabelForChoice = new Label(You havent chosen anything); theChoicePanel.add(fLabelForChoice); fContents.add(theChoicePanel, Choice); add( fContents, BorderLayout.SOUTH); theNextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { CardLayout theLayout = (CardLayout) fContents.getLayout(); theLayout.next(fContents); }}); thePreviousButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { CardLayout theLayout = (CardLayout) fContents.getLayout(); theLayout.previous(fContents); }});
Using GridBagLayoutAlthough you can control such factors as alignment and horizontal and vertical gap in the other layout managers, the GridBagLayout gives you the ultimate in flexibility. At its simplest, GridBagLayout works much like a grid, except that it puts each component in a cell of its preferred size. The total area that a component occupies is called its display area. You specify suggestionscalled GridBagConstraintsthat further control how the layout will appear. The GridBagConstraints class has a number of variables to control the placement of a component:
|
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. |