java BoxLayout面板的对齐方式

发布于 2025-01-04 08:39:48 字数 724 浏览 2 评论 0原文

我浏览了一下,没有找到专门适合我情况的解决方案。我有一个在对话框中显示的面板:

//create dialog panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(headerPanel);
panel.add(type1Panel);
panel.add(type2Panel);
panel.add(type3Panel);
panel.add(type4Panel);
panel.add(type5Panel);
panel.add(type6Panel);

int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);

最后两个面板的大小,type5 和 type5 type6 大小相同,因此看起来不错。然而,标题和前 4 个面板的大小不同,我希望它们全部保持对齐。到目前为止我还没有找到一个好的解决方案来解决这个问题。

问题是,如何左对齐前 5 个面板,但不左对齐最后 2 个面板?如果不是,我怎样才能将它们全部左对齐? setalignmentx() 不适用于面板。我尝试过使用 GridLayout,但是 gui 主窗口的宽度相当大,不能很好地适应屏幕,因此沿 Y 轴使用 BoxLayout。感谢您的任何帮助或建议。

I have browsed around and haven't found a solution that specifically tailors to my situation. I have a panel that I display in a dialog box:

//create dialog panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(headerPanel);
panel.add(type1Panel);
panel.add(type2Panel);
panel.add(type3Panel);
panel.add(type4Panel);
panel.add(type5Panel);
panel.add(type6Panel);

int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);

The size of the last two panels, type5 & type6, are of equal size so they look fine. However, the header and first 4 panels are of different sizes and I would like them all to be left aligned. As of yet I haven't found a good solution as how to fix this.

Question is, how can I left align the first 5 panels, but not last 2? If not how can I left align them all? The setalignmentx() isn't available for panels. I've tried using GridLayout, but then the width of the gui's main window is rather large and doesn't fit nicely onto the screen, hence the BoxLayout along Y axis.Thanks for any help or suggestions.

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

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

发布评论

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

评论(3

山川志 2025-01-11 08:39:48

下面是一个示例,它将左对齐添加到用作容器的面板中的所有 JPanel。

   JPanel a = new JPanel();
   JPanel b = new JPanel();
   JPanel c = new JPanel();

   a.setBackground( Color.RED );
   b.setBackground( Color.GREEN  );
   c.setBackground( Color.BLUE );

   a.setMaximumSize( new Dimension(  10, 10) );
   b.setMaximumSize( new Dimension(  50, 10) );

   a.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   b.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   c.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0

   JPanel panel = new JPanel();
   panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
   panel.add(a);
   panel.add(b);
   panel.add(c); 

   int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);

Here is an example that will left align all the JPanels added to the panel used as a container.

   JPanel a = new JPanel();
   JPanel b = new JPanel();
   JPanel c = new JPanel();

   a.setBackground( Color.RED );
   b.setBackground( Color.GREEN  );
   c.setBackground( Color.BLUE );

   a.setMaximumSize( new Dimension(  10, 10) );
   b.setMaximumSize( new Dimension(  50, 10) );

   a.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   b.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   c.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0

   JPanel panel = new JPanel();
   panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
   panel.add(a);
   panel.add(b);
   panel.add(c); 

   int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);
明天过后 2025-01-11 08:39:48

创建一个水平 javax.swing.Box 对象来包含每个 typenPanel 对象。使用水平支柱和胶水,您可以做任何您想做的事情:

Box  b1 = Box.createHorizontalBox();
b1.add( type1Panel );
b1.add( Box.createHorizontalGlue() );
panel.add( b1 );

为了简单起见,编写一个辅助方法来为您执行此操作:

private Component leftJustify( JPanel panel )  {
    Box  b = Box.createHorizontalBox();
    b.add( panel );
    b.add( Box.createHorizontalGlue() );
    // (Note that you could throw a lot more components
    // and struts and glue in here.)
    return b;
}

然后:

panel.add( leftJustify( headerPanel ) );
panel.add( leftJustify( type1Panel ) );
panel.add( leftJustify( type2Panel ) );

等等......您可以通过添加组件、胶水和支柱来对每一行变得更奇特。我非常幸运地深度嵌套垂直和水平框,并且当我想在一个框中多次执行相同的布局时编写辅助方法。您可以做的事情没有任何限制,可以根据需要混合组件、支柱和胶水。

我确信有更好的方法来完成这一切,但我还没有找到。动态调整大小可以让文本较短的用户使用小窗口,而文本较多的用户则可以调整窗口大小,以使其完全适合。

Create a horizontal javax.swing.Box object to contain each typenPanel object. Using horizontal struts and glue you can do whatever you want:

Box  b1 = Box.createHorizontalBox();
b1.add( type1Panel );
b1.add( Box.createHorizontalGlue() );
panel.add( b1 );

For simplicity, write a helper method to do this for you:

private Component leftJustify( JPanel panel )  {
    Box  b = Box.createHorizontalBox();
    b.add( panel );
    b.add( Box.createHorizontalGlue() );
    // (Note that you could throw a lot more components
    // and struts and glue in here.)
    return b;
}

Then:

panel.add( leftJustify( headerPanel ) );
panel.add( leftJustify( type1Panel ) );
panel.add( leftJustify( type2Panel ) );

etc.... You can get fancier with each line, adding components, glue, and struts. I've had great luck deeply nesting vertical and horizontal boxes, and writing helper methods when I want to do the same layout in a box more than once. There's no limits to what you can do, mixing components, struts, and glue as necessary.

I'm sure there's a better way to do all this, but I haven't found it yet. And the dynamic resizing lets a user with short bits of text use a small window and a user with lots of text resize it so it all fits.

雪落纷纷 2025-01-11 08:39:48

您应该在面板上使用 setAlignmentX,因为它可用于 JPanelsetAlignmentXsetAlignmentY 方法位于 JComponent 中,它是 JPanel 的扩展。它有效...我有使用这些方法在 BoxLayout 中对齐 JPanel 的代码。

好的,好吧,在我回答问题时编辑您的问题:)

尝试使用 Box。我发现 Box 类作为容器非常有用。从API:

使用 BoxLayout 对象作为布局的轻量级容器
经理。 Box 提供了几个有用的类方法
使用 BoxLayout 的容器——甚至是非 Box 容器。

如果您还没有看过教程如何使用 BoxLayout 非常有帮助。

You should use setAlignmentX on the panels because it is available for JPanel. The methods setAlignmentX and setAlignmentY are found in JComponent, which JPanel extends. It works...I've got code that uses those methods to align JPanels in a BoxLayout.

Ok, fine, edit your question while I'm answering it :)

Instead of using a JPanel try using a Box. I've found the Box class to be very useful as a container. From the API:

A lightweight container that uses a BoxLayout object as its layout
manager. Box provides several class methods that are useful for
containers using BoxLayout -- even non-Box containers.

If you haven't seen it yet, the tutorial How to Use BoxLayout is very helpful.

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