All thanks, who really wanted to help, but I already managed it;) I didn’t do that, I had to build everything linearly, not panels.


Greetings Dear experts, please help to understand. I need to create more panels dynamically on the main created panel.

package test; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import javax.swing.BorderFactory; import javax.swing.GroupLayout; import javax.swing.GroupLayout.SequentialGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class Test implements Runnable { public static void main(String[] args) { SwingUtilities.invokeLater(new Test()); } @Override public void run() { // Окно JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 600); frame.setLocationRelativeTo(null); frame.setLayout(new FlowLayout(FlowLayout.LEFT)); // Основная панель JPanel p = new JPanel(); p.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(0, 0, 0))); p.setPreferredSize(new Dimension(500, 500)); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(p); p.setLayout(layout); GroupLayout.ParallelGroup hor = layout.createParallelGroup(GroupLayout.Alignment.LEADING), vert = layout.createParallelGroup(GroupLayout.Alignment.LEADING, false); SequentialGroup sgh = layout.createSequentialGroup(), sgv = layout.createSequentialGroup(); // Панели которые добавлять на основную панель, где String это название, а int[] это ширина и высота final Map<String, int[]> parts = new ConcurrentHashMap<>(); parts.put("p1", new int[]{ 440, 140 }); parts.put("p2", new int[]{ 120, 200 }); parts.put("p3", new int[]{ 220, 200 }); // Перебераем все панели которые добавляем for (Map.Entry<String, int[]> part : parts.entrySet()) { int[] pSize = part.getValue(); JPanel pp = new JPanel(); pp.setPreferredSize(new Dimension(pSize[0], pSize[1])); pp.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, new Color(0, 0, 0))); sgh.addComponent(pp, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE); hor.addGroup(sgh); sgv.addComponent(pp, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE); vert.addGroup(sgv); } layout.setHorizontalGroup(hor); layout.setVerticalGroup(vert); frame.add(p); frame.setVisible(true); } } 

Some kind of garbage turns out ... The fact is that I need the panels to be added to the main one without going beyond the borders, so that if the element does not fit, you need to fit it below.

Already I’m fighting what day, I just can’t grasp what I’m doing wrong ...? (

The task is not easy. Save what anyone can, if there is another way to draw elements of different sizes without going beyond the boundaries and without stepping on each other, in the allowed places, please tell me.

I tried to find something, nowhere, just words, no code. I want to get something like a layout, just for other purposes, it is necessary that the program remembers each placed element on the field.

    1 answer 1

    It was necessary to use Rectangle and Graphics.