|
-
Box Layout Question: JTextFields bloated
I am doing the BoxLayoutTest.java example program from
Core Java 2 Volume I - Fundaments book on page 471-473.
I have Java 1.3 SDK.
Unlike the book pictures on pages 469-471, my JTextFields
are bloating up. Did I miss some code? I checked but I may
have missed something.
I didn't change much except to make the WindowAdapter into
setDefaultCloseOperation ( EXIT_ON_CLOSE ) ;
Thanks for any assistance.
import java.awt.* ;
import java.awt.event.* ;
import javax.swing.* ;
public class BoxLayoutTest extends JFrame
implements ActionListener
{
public BoxLayoutTest ()
{
setTitle ( "BoxLayoutTest" ) ;
setSize ( 300, 200 ) ;
setDefaultCloseOperation ( EXIT_ON_CLOSE ) ;
horizontalBox = createBox ( true, false ) ;
verticalBox = createBox ( false, false ) ;
horizontalStrutsAndGlueBox = createBox ( true, true ) ;
verticalStrutsAndGlueBox = createBox ( false, true ) ;
JPanel panel = new JPanel () ;
panel.setLayout ( new GridLayout ( 3, 1 ) ) ;
ButtonGroup directionGroup = new ButtonGroup () ;
horizontalButton = addRadioButton ( panel,
directionGroup, "Horizontal", true ) ;
verticalButton = addRadioButton ( panel,
directionGroup, "Vertical", false ) ;
strutsAndGlueCheckBox = addCheckBox ( panel,
"Struts and Glue" ) ;
Container contentPane = getContentPane () ;
contentPane.add ( panel, BorderLayout.SOUTH ) ;
contentPane.add ( horizontalBox, BorderLayout.CENTER ) ;
currentBox = horizontalBox ;
}
public Box createBox ( boolean horizontal, boolean strutsAndGlue )
{
Box b ;
if ( horizontal )
b = Box.createHorizontalBox () ;
else
b = Box.createVerticalBox () ;
b.add ( new JLabel ( "Name" ) ) ;
b.add ( new JTextField () ) ;
if ( strutsAndGlue )
if ( horizontal )
b.add ( Box.createHorizontalStrut ( 5 ) ) ;
else
b.add ( Box.createVerticalStrut ( 5 ) ) ;
b.add ( new JLabel ( "Password" ) ) ;
b.add ( new JTextField () ) ;
if ( strutsAndGlue )
b.add ( Box.createGlue () ) ;
b.add ( new JButton ( "Ok" ) ) ;
return b ;
}
public JRadioButton addRadioButton ( JPanel p,
ButtonGroup g, String name, boolean selected )
{
JRadioButton button
= new JRadioButton ( name, selected ) ;
button.addActionListener ( this ) ;
g.add ( button ) ;
p.add ( button ) ;
return ( button ) ;
}
public JCheckBox addCheckBox ( JPanel p, String name )
{
JCheckBox checkBox = new JCheckBox ( name ) ;
checkBox.addActionListener ( this ) ;
p.add ( checkBox ) ;
return checkBox ;
}
public void actionPerformed ( ActionEvent evt )
{
Container contentPane = getContentPane () ;
contentPane.remove ( currentBox ) ;
if ( horizontalButton.isSelected () )
{
if ( strutsAndGlueCheckBox.isSelected () )
currentBox = horizontalStrutsAndGlueBox ;
else
currentBox = horizontalBox ;
}
else
{
if ( strutsAndGlueCheckBox.isSelected () )
currentBox = verticalStrutsAndGlueBox ;
else
currentBox = verticalBox ;
}
contentPane.add ( currentBox, BorderLayout.CENTER ) ;
contentPane.validate () ; // force layout
repaint () ;
}
public static void main ( String[] args )
{
Frame f = new BoxLayoutTest () ;
f.show () ;
}
private Box horizontalBox ;
private Box verticalBox ;
private Box horizontalStrutsAndGlueBox ;
private Box verticalStrutsAndGlueBox ;
private Box currentBox ;
private JCheckBox strutsAndGlueCheckBox ;
private JRadioButton horizontalButton ;
private JRadioButton verticalButton ;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks