swing flow layout break element

前端 未结 5 1726
清歌不尽
清歌不尽 2020-12-18 21:40

I have panel which is using flow layout.

How can I make break in flow layout? Like
in html. Some special break element or another trick to i

5条回答
  •  囚心锁ツ
    2020-12-18 22:29

    You want to manually divide the components in multiple rows? So you know where you want the linebreak to be.

    In that case I would use 3 panels:

    • 1 panel containing the other 2 panels with a GridLayout with 1 column
    • 2 panels inside the GridLayout, each with a FlowLayout

    Example code:

        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        {
            panel = new JPanel();
            frame.getContentPane().add(panel, BorderLayout.NORTH);
            panel.setLayout(new GridLayout(0, 1, 0, 0));
            {
                panel_1 = new JPanel();
                panel.add(panel_1);
                {
                    lblPanelFlowlayout = new JLabel("Panel 2: FlowLayout");
                    panel_1.add(lblPanelFlowlayout);
                }
            }
            {
                panel_2 = new JPanel();
                panel.add(panel_2);
                {
                    lblPanel = new JLabel("Panel 3: FlowLayout");
                    panel_2.add(lblPanel);
                }
            }
        }
    

    You can add as many new Panels with a FlowLayout as you want. Each time you would do a BR you now set the next panel as active (possibly creationg it dynamically).

提交回复
热议问题