java 我可以使用 FlowLayout 在 JPanel 中获得多行吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10210222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:02:45  来源:igfitidea点击:

Can I get multiple rows in JPanel with FlowLayout

javaswingjpanellayout-managerflowlayout

提问by Daniel Jab?oński

I need solution similar to GridLayoutbut without resizing components in JPanel.

我需要类似于GridLayout但不调整JPanel.

Everything works great with JFrame, but I need to put those components into JPanelinstead JFrame.

一切的伟大工程同JFrame,但我需要把这些组件集成到JPanel代替JFrame

回答by trashgod

I've seen two approaches that may suit your requirement:

我见过两种可能符合您要求的方法:

  • Nest each component in a JPanelhaving FlowLayout, which respects the component's preferred size, as shown here.

  • Use the HORIZONTAL_WRAPor VERTICAL_WRAPorientation of JList, as shown here.

  • 窝在每个组件JPanel具有FlowLayout,其尊重组件的优选尺寸,如图所示在这里

  • 使用HORIZONTAL_WRAPVERTICAL_WRAP取向JList,如图所示这里

回答by wattostudios

The following link might help you to choose the most appropriate layout for your needs. Its the Java Tutorial called "A Visual Guide to Layout Manager", which shows nice pictures of each layout and what they look like...

以下链接可能会帮助您选择最适合您需要的布局。它的 Java 教程称为“布局管理器的可视化指南”,其中显示了每个布局的精美图片及其外观......

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Otherwise, if you say GridLayoutis similar to what you need, you could always write your own MyGridLayoutclass (that extends GridLayout) and overwrite the method that does the autoresizing.

否则,如果您说GridLayout的与您需要的相似,您始终可以编写自己的MyGridLayout类(即扩展类GridLayout)并覆盖执行自动调整大小的方法。

回答by Ravinda lakshan

Just Override preferredLayoutSize() in flowlayout and set Maximum size to it. set Alignment as LEADING and set it to your JPanel. You'll get what you want

只需覆盖 flowlayout 中的 preferredLayoutSize() 并将最大大小设置为它。将 Alignment 设置为 LEADING 并将其设置为您的 JPanel。你会得到你想要的

private FlowLayout getFlowLayout(int maximumSize)
      {
        if (flowLayout == null)
        {
          flowLayout = new FlowLayout()
          {
            @Override 
            public Dimension preferredLayoutSize(Container target)
            {
              Dimension dimension = super.preferredLayoutSize(target);
              dimension.width = Math.min(maximumSize, dimension.width);
              return dimension;
            }
          };
          flowLayout.setAlignment(FlowLayout.LEADING);
        }
        return flowLayout;
      }