简体   繁体   English

包装元素java swing

[英]Wrap element java swing

I have this example where the panels(with title,desc and button) are placed next to one another 我有这个示例,其中的面板(带有标题,desc和按钮)彼此相邻放置 在此处输入图片说明

This is the code 这是代码

 public class Example extends JFrame {

    Example() {

        setLayout(new FlowLayout());
        for (int i = 0; i < 4; i++)
            add(new MyPanel());

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    class MyPanel extends JPanel {

        MyPanel() {

            setLayout(new BorderLayout());
            add(new JLabel("title", SwingConstants.CENTER), BorderLayout.PAGE_START);
            add(new JLabel("<html>WWWWWW WWWWWWW<p>WWWWWWWWW<p>RRRRRRR RRRRR RRRRRRR"));
            add(new JButton("Details"), BorderLayout.PAGE_END);
        }
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new Example());
    }
}

how to wrap the panels every time? 每次如何包裹面板?

在此处输入图片说明

Just use a different layout manager -- please read up on them. 只需使用其他布局管理器-请仔细阅读。 A new GridLayout(0, 1) for variable number of rows and 1 column would work. 一个new GridLayout(0, 1)适用于可变的行数和1列。 Also consider using a BoxLayout that uses the constraint BoxLayout.PAGE_AXIS . 还可以考虑使用使用约束BoxLayout.PAGE_AXIS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM