Spacing with GridBagLayout

3

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

    
asked by anonymous 25.10.2015 / 02:20

1 answer

3

What you are looking for is Insets :

cons.insets = new Insets(20, 20, 0, 0);

Image example with Insets zeroed:

Thesameimage,onlynowusingInsets:

weight,contrarytowhatyouthought,isusedtodealwiththesizeoftheelements,whenthewindowisresized.Theleftimagebelowhasweightof0.5foreachimage.Theoneontherighthas0.4thefirst,and0.6thesecond,causingonetoberesizedwhenresized:

Followthecodeusedifyouwanttotestit:

importjava.awt.*;importjavax.swing.*;importjavax.imageio.ImageIO;importjava.io.*;importjava.net.*;importjava.awt.image.BufferedImage;classTesteInsetsextendsJPanel{JLabelsoLogo,soLogo2;BufferedImageimg,img2;publicTesteInsets(){setLayout(newGridBagLayout());try{URLurl=newURL("http://i.imgur.com/xIUCtPo.png");
        URL url2 = new URL("http://i.imgur.com/kmNQziR.png");

        img = ImageIO.read(url);
        img2 = ImageIO.read(url2);

        } catch (IOException e) {
        }

        soLogo = new JLabel();
        soLogo2 = new JLabel();
        soLogo.setIcon(new ImageIcon(new ImageIcon(img).
          getImage().getScaledInstance(175, 175, Image.SCALE_SMOOTH)));
        soLogo2.setIcon(new ImageIcon(new ImageIcon(img2).
          getImage().getScaledInstance(175, 175, Image.SCALE_SMOOTH)));

        GridBagConstraints c = new GridBagConstraints();

         c.weightx = 0.6;
         c.gridx = 0;
         c.gridy = 0;
         c.insets = new Insets(15, 15, 15, 0);
         add(soLogo, c);

         c.weightx = 0.4;
         c.gridx = 1;
         c.gridy = 0;
         c.insets = new Insets(15, 15, 15, 15);
         add(soLogo2, c);

    }

    public static void criarInterface() {

    JFrame janela = new JFrame("Teste Insets");
    janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent painelConteudo = new TesteInsets();

    painelConteudo.setOpaque(true);
    janela.setContentPane(painelConteudo);
    janela.setResizable(true);
    janela.pack();
    janela.setVisible(true);

    }

    public static void main(String[] Args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                criarInterface();
            }
        });
    }

}

Visit the Javadoc for more examples.

    
26.10.2015 / 08:20