2
I wanted to put panels in specific positions, so I tried to combine some layout managers, but I still didn’t get the result I need.
I tried to use the gridLayout
, so the components wouldn’t be stretched, but I’m not sure I’m making a wrong use of it.
I tried to do that:
where the black boards are components, in that case labels
.
And the result I got was:
Can anyone show me or indicate a way to do that?
Follow an example of the code:
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
public class PainelPosicao {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new PainelPosicao());
}
public PainelPosicao() {
JFrame jFrame = new JFrame();
jFrame.setUndecorated(true);
jFrame.setContentPane(new Pane());
jFrame.setSize(500, 300);
jFrame.setVisible(true);
jFrame.setLocationRelativeTo(null);
jFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
class Pane extends JPanel {
public Pane() {
setLayout(new GridLayout(2, 1));
JPanel gridPainel = new JPanel();
gridPainel.setLayout(new GridLayout(2, 1));
gridPainel.setBackground(Color.GREEN);
JPanel painel = new JPanel();
painel.add(new JLabel("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);
}
}
}
I don’t understand what you want to do. Want to put the two bottom Abels in the black frame?
– user28595
Actually, it was for when gray was inside the frame, I don’t know why he didn’t stay. The Abels I wanted to center under the gray quandro
– Javinha
Do you need that kind of panel there? I can do without it, it is quite useless in the code
– user28595
You can do it all there with just one panel. Using Gridbaglayout.
– user28595
Can yes, I’ll match the other question I asked, then just do what you explained to me there
– Javinha
I made a more simplistic approach using only a jpanel, as I had commented. See in the answer, it is as an alternative.
– user28595
@Articuno The two turned out excellent, it was exactly what I needed ! Thank you
– Javinha