开发者

Java - GridLayout Problems

I'm doing a school project and need help with the Layout because it's doing some funky stuff.

I only need help with the south panel. The south panel is organized something like this:

           JRadioButton JLabel1 JLabel2
           JRadioButton JLabel1 JLabel2
    JLabel3--------JTextField----------JButton

I've tried the following:

  1. Set the south JPanel to a gridlayout with 3 rows
  2. Another JPanel also with a gridlayout with 3 rows added to the first row of the gridlayout.
  3. Repeat #2 with the second and third rows. The second row has 3 rows by 3 columns.
  4. Added the components in the appropriate rows/columns.

Doesn't format the way I need it to. I've tried some other techniques I can't remember. Any suggestions? Thank you. Here's a picture of what the south panel should look like: http://www.freeimagehosting.net/image.php?d14a73db5e.jpg

It starts at the "Start Dat开发者_如何学Pythone..."


Create a new JPanel and use Group Layout for that.

Like this http://img163.imageshack.us/img163/1403/capturadepantalla201001bo.png

Using group layout allows you to specify the components that will go in the horizontal group and the components that will go in the vertical group.

c1, c2, c3
c4, c5, c6
panel[ c7, c8, c9 ]

Here's how I layout the image above:

    // Layout Horizontal components 
    layout.setHorizontalGroup(
        layout.createSequentialGroup()
        .addGroup( 
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent( c1 )
            .addComponent( c4 )
        ).addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent( c2 )
                .addComponent( c5)
        ).addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent( c3 )
                .addComponent( c6 )
        )

    );

    // Layout vertical components 
    layout.setVerticalGroup(
        layout.createSequentialGroup()
        .addGroup( 
             layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
             .addComponent( c1 )
             .addComponent( c2 )
             .addComponent( c3 )
         ).addGroup(
             layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                 .addComponent( c4 )
                 .addComponent( c5 )
                 .addComponent( c6 ) 
        )
    );

    southPanel.add( panel ); // the start-end dates
    southPanel.add( bookIt, BorderLayout.SOUTH ); // the enter your name... 

Give it a try.


What I would do for the south JPanel is make it a 1x3 grid, then add the three components of the first row into a horizontal Box and add it to the grid, then the three components of the second row into another horizontal Box and add it to the grid, then the three components of the third row into a 3x1 grid and add it to the grid. Then you can adjust the exact look you want for the first two lines by adding various glue and strut components.

I achieve most of my layouts by putting jpanels or Boxes inside of others with different layouts. This gives you pretty good control over what you want, and allows you to build it up a piece at a time.


Seems like you'd want a GridLayout with 3 columns, not 3 rows for the bottom panel there.

In the image at the link you included, the text field is wider than the JLabel or the button. You may want to try a GridBagLayout instead.


Recently I stumbled upon MigLayout, I'll try using it in my next university project and I'd advice you to give it a shot also it looks really cool and simple.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜