I\'m trying to use GridBagLayout
, but I don\'t get what I expect and I can\'t find the error in this code:
public class GridBagEx1 extends JPanel {
I managed to design the needed layout without any hack and supporting dynamic resizing by using JGoodies FormLayout:
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JPanel;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
public class FormLayoutPanel extends JPanel
{
public FormLayoutPanel()
{
setAlignmentY( Component.BOTTOM_ALIGNMENT );
setAlignmentX( Component.RIGHT_ALIGNMENT );
setLayout( new FormLayout( new ColumnSpec[] {
ColumnSpec.decode( "41px:grow" ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
ColumnSpec.decode( "25px:grow" ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
ColumnSpec.decode( "41px:grow" ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
ColumnSpec.decode( "41px:grow" ),
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
ColumnSpec.decode( "41px:grow" ), },
new RowSpec[] {
RowSpec.decode( "25px:grow" ),
FormFactory.LINE_GAP_ROWSPEC,
RowSpec.decode( "25px:grow" ),
FormFactory.LINE_GAP_ROWSPEC,
RowSpec.decode( "25px:grow" ), } ) );
JButton button1 = new JButton( "1" );
add( button1, "1, 1, 3, 1, fill, fill" );
JButton button2 = new JButton( "2" );
add( button2, "5, 1, fill, fill" );
JButton button3 = new JButton( "3" );
add( button3, "7, 1, 3, 3, fill, fill" );
JButton button4 = new JButton( "4" );
add( button4, "1, 3, fill, fill" );
JButton button5 = new JButton( "5" );
add( button5, "3, 3, 3, 1, fill, fill" );
JButton button6 = new JButton( "6" );
add( button6, "1, 5, fill, fill" );
JButton button7 = new JButton( "7" );
add( button7, "3, 5, 3, 1, fill, fill" );
JButton button8 = new JButton( "8" );
add( button8, "7, 5, fill, fill" );
JButton button9 = new JButton( "9" );
add( button9, "9, 5, fill, fill" );
}
}