Hi guys, I tought I'd experiment a bit with layout managers, trying to get to grips with them. I will start with a flowLayout, then hopefully move on to more complex ones.
OK, so here is the code for the first example, very simple one:
/*FlowLayoutManager.java
layout manager tests*/
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
public class FlowLayoutManager extends JFrame{
private JTextField textField1;
private JTextField textField2;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private FlowLayout layout;
public FlowLayoutManager(){
super( "Flow Layout" );
//creating and setting layout for controllers
layout = new FlowLayout();
setLayout( layout );
//creating controllers
textField1 = new JTextField( "", 10 );
textField2 = new JTextField( "", 10 );
checkBox1 = new JCheckBox( "checkbox 1" );
checkBox2 = new JCheckBox( "checkbox 2" );
//attaching controllers to JFrame window
add( textField1 );
add( textField2 );
add( checkBox1 );
add( checkBox2 );
}//end of constructor
}//end of class
and test class
/*testing FlowLayoutManager.java
FlowLayoutManagerTest.java
*/
import javax.swing.JFrame;
public class FlowLayoutManagerTest{
public static void main( String args[] ){
FlowLayoutManager FlowLayoutManagerTest = new FlowLayoutManager();
FlowLayoutManagerTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//FlowLayoutManagerTest.setSize( 400, 200 );
FlowLayoutManagerTest.setVisible( true );
FlowLayoutManagerTest.pack();
}//end of main
}//end of class
This produces this layout:
Now, it's OK but the height of the window is not reakky what I want. How do I increase that to a more user friendly size?
Also, you will notice in the FlowLayoutManagerTest.java that I have commented out //FlowLayoutManagerTest.setSize( 400, 200 );
Although I have been kindly warned already, if I uncomment that line and comment out this instead FlowLayoutManagerTest.pack();
, the layout breaks in that the last checkbox is not aligned left by default as it should in a flowLayout, see screenshot
Why is that and what is the best way to fix it? Set a minimum size?