I wanted to put panels in specific positions, so I tried combining some layout managers, but I still have not got the result I need.
I tried to use gridLayout
, so components would not be stretched, but I'm not sure if I'm making a bad use of it.
I tried to do this:
whereblackboardsarecomponents,inthiscaselabels
.
AndtheresultIgotwas:
Cananyoneshowmeorindicateawaytodothis?
Hereisanexampleofthecode:
importjava.awt.Color;importjava.awt.GridLayout;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.SwingUtilities;importstaticjavax.swing.WindowConstants.DISPOSE_ON_CLOSE;publicclassPainelPosicao{publicstaticvoidmain(String[]args){SwingUtilities.invokeLater(()->newPainelPosicao());}publicPainelPosicao(){JFramejFrame=newJFrame();jFrame.setUndecorated(true);jFrame.setContentPane(newPane());jFrame.setSize(500,300);jFrame.setVisible(true);jFrame.setLocationRelativeTo(null);jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);}classPaneextendsJPanel{publicPane(){setLayout(newGridLayout(2,1));JPanelgridPainel=newJPanel();gridPainel.setLayout(newGridLayout(2,1));gridPainel.setBackground(Color.GREEN);JPanelpainel=newJPanel();painel.add(newJLabel("Label 01"));
JPanel painelProgress = new JPanel();
painelProgress.setLayout(new GridLayout(2, 1));
painelProgress.add(new JLabel("Label 01"));
painelProgress.add(new JLabel(".......Label 02 ......"));
gridPainel.add(painel);
gridPainel.add(painelProgress);
add(gridPainel);
setBackground(Color.BLACK);
}
}
}