Java - GridLayout 问题

发布于 2024-08-18 05:36:12 字数 638 浏览 3 评论 0原文

我正在做一个学校项目,需要布局方面的帮助,因为它正在做一些时髦的事情。

我只需要南面板的帮助。南面板的组织方式如下:

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

我尝试了以下操作:

  1. 将南 JPanel 设置为具有 3 行的网格布局
  2. 另一个 JPanel 也具有一个网格布局,其中将 3 行添加到网格布局的第一行。
  3. 对第二行和第三行重复#2。第二行有 3 行 x 3 列。
  4. 在适当的行/列中添加组件。

没有按照我需要的方式格式化。我尝试过一些我不记得的其他技术。有什么建议吗?谢谢。下面是南面板的外观图片:http://www.freeimagehosting。 net/image.php?d14a73db5e.jpg

它从“开始日期...”开始

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 Date..."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人间☆小暴躁 2024-08-25 05:36:12

创建一个新的 JPanel 并使用 Group布局

像这样http://img163.imageshack.us/img163/1403/capturadepantalla201001bo.png< /a>

使用组布局允许您指定将进入水平组的组件和将进入垂直组的组件。

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

以下是我如何布局上面的图像:

    // 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... 

尝试一下。

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.

神妖 2024-08-25 05:36:12

我对南 JPanel 所做的是将其设为 1x3 网格,然后将第一行的三个组件添加到水平框中并将其添加到网格中,然后将第二行的三个组件添加到另一个水平框中并将其添加到到网格中,然后将第三行的三个分量组成一个3x1的网格并将其添加到网格中。然后,您可以通过添加各种胶水和支柱组件来调整前两条线的确切外观。

我通过将 jpanels 或 Boxes 放入具有不同布局的其他布局中来实现大部分布局。这使您可以很好地控制您想要的内容,并允许您一次构建它。

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.

静若繁花 2024-08-25 05:36:12

看起来您想要一个具有 3 的 GridLayout,而不是底部面板的 3

在您包含的链接的图像中,文本字段比 JLabel 或按钮宽。您可能想尝试使用 GridBagLayout

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.

暮色兮凉城 2024-08-25 05:36:12

最近我偶然发现了 MigLayout,我将在下一个大学项目中尝试使用它,我建议你尝试一下,它看起来非常酷而且简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文