I'm putting together a test layout with GribBagLayout, but I'm having difficulty positioning the elements. The frame has two panel . I want the items on the left panel (Label 1, text 1 ...) to have a small spacing from the left and Label 1 also have a small spacing from the top, as it is "glued". My main doubt is about the weight , because I imagine that it is with him that I arrange 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