troubleshooting Question

Aligning components in Java

Avatar of mkelly2384
mkelly2384Flag for United States of America asked on
Java
3 Comments3 Solutions651 ViewsLast Modified:
In the code below I would like the check boxes to be left aligned, the text boxes centered, and the buttons right-aligned.  I also would like the buttons to all be the same size.

In my code, everything is right aligned and I don't know how to make some components aligned differently than others within the same window.  I'm using TextPad.  Thanks for the help!
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
 
public class GUIContainer extends JFrame
{
   private FlowLayout layout;
   private JLabel xLabel;
   private JLabel yLabel;
   private JTextField xTextField;
   private JTextField yTextField;
   private JCheckBox snapJCheckBox;
   private JCheckBox showJCheckBox;
   private JButton okButton;
   private JButton cancelButton;
   private JButton helpButton;
 
      // GUIContainer constructor adds components to JFrame
   public GUIContainer()
   {
      super( "Align" );
      layout = new FlowLayout(); //create FlowLayout
      setLayout( layout ); // set FlowLayout
      layout.setAlignment( FlowLayout.RIGHT); //Right align the layout
 
 
      snapJCheckBox = new JCheckBox( "Snap to Grid" ); // create first checkbox
      add( snapJCheckBox); // add first checkbox to JFrame
 
      xLabel = new JLabel( "X:" ); //create JLabel
      add( xLabel ); //add xLabel to JFrame
 
      xTextField = new JTextField( "8", 3 ); //create JTextField
      add( xTextField ); // add textField to JFrame
 
      okButton = new JButton( "    OK   " );
      add( okButton );
 
      showJCheckBox = new JCheckBox( "Show Grid    " ); // create second checkbox
	  add( showJCheckBox); // add second checkbox to JFrame
 
      yLabel = new JLabel( "Y:" );
      add( yLabel); //add yLabel to JFrame
 
      // set up JTextFields
      yTextField = new JTextField( "8", 3 );
      add( yTextField );
 
      cancelButton = new JButton( "Cancel" );
      add( cancelButton );
 
      helpButton = new JButton( "  Help   " );
      add( helpButton );
   } //end GUIContainer constructor
} // end class GUIContainer
ASKER CERTIFIED SOLUTION
DaveG1960

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 3 Answers and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros