3
I’m putting together a layout for testing with Gribbaglayout, but I’m having difficulties with the positioning of the elements. The frame has two panel. I want the items on the left panel (Label 1, text 1...) to have a small left spacing and Label 1 to have a small upper spacing as it is "stuck". My main question is as to Weight, because I imagine that it is with him that I put this but I could not. Below the photo and the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Teste{
private JButton btnCalc;
private JLabel origemText;
private JLabel destinoText;
private JPanel panel1, panel2;
private JLabel labelPanel2;
private JTextField texto1;
private JTextField texto2;
public Teste() {
JFrame frame = new JFrame();
frame.setTitle("teste");
frame.setSize(1000, 500);
frame.getContentPane().setLayout(new BorderLayout());
origemText = new JLabel("Label 1");
texto1 = new JTextField("Texto 1");
destinoText = new JLabel("Label 2");
texto2 = new JTextField("Texto 2");
btnCalc = new JButton("Botão 1");
GridBagConstraints cons = new GridBagConstraints();
panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
panel1.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
panel1.setPreferredSize(new Dimension(180, 200));
cons.anchor = GridBagConstraints.NORTHWEST;
cons.gridx = 0;
cons.gridy = 0;
cons.weighty = 0.1;
panel1.add(origemText, cons);
cons.gridx = 0;
cons.gridy = 1;
cons.weighty = 0.2;
cons.weightx = 0.1;
panel1.add(texto1, cons);
cons.gridx = 0;
cons.gridy = 2;
cons.weighty = 0.1;
panel1.add(destinoText, cons);
cons.gridx = 0;
cons.gridy = 3;
cons.weighty = 0.6;
panel1.add(texto2, cons);
cons.gridx = 0;
cons.gridy = 4;
cons.weighty = 9.0;
panel1.add(btnCalc, cons);
panel2 = new JPanel();
panel2.setLayout(new GridBagLayout());
panel2.setSize(200, 300);
labelPanel2 = new JLabel("Panel 2");
panel2.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
panel2.add(labelPanel2);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.WEST, panel1);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Teste();
}
}
EDIT
Worked out, buddy. I had already read this Weight function, but I was using it and I saw that it was kind of organizing the spacing, but really that’s what you said. Half frame is with setResizable(false), is it right to use Weight in my code? Or would not even need?
– Ablon
@Ablon Se is with
setResizable(false)
, I see no reason to use theweight
. Also, I suggest you do not set the size usingframe.setSize()
- the ideal is that you let the elements choose their sizes, and use theframe.pack()
in the end.– Daniel
Yes, but if I remove the Weight, although I can determine the distance between the elements with the insets, they are organized in the center of that Jpanel. I updated with print
– Ablon
@Ablon O
weight
does what you want, but he does it for the wrong reason. It’s important you imagine how your layout will be at the end. The idea of using the layout managers is usually to let the elements "position themselves" within them. If this field will have only these five elements and they need to stay at the top, I suggest you use another layout, like theBorderLayout
and put them onNorth
. But if you are eventually to fill the entire field, you need to add new elements so that they occupy their definitive positions.– Daniel
@Ablon Remember you can put
JPanel
insideJPanel
, with different layouts! AGridBagLayout
within aBorderLayout
may be what you seek (think better than "patch" with theweight
).– Daniel
@Ablon believes that your initial doubt has already been resolved, however as you develop you end up evolving your project and finding new doubts. I advise you to mark that answer as accepted and if you need to start another topic with your new question. However, take a look at this topic before to see if it helps you a little: Positioning of components using Gridbaglayout
– Math
Thanks to you!
– Ablon