Pregunta

Estoy tratando de que GridbagLayout con dos paneles sea del 40% y el 60% del marco, mientras que puedo tener componentes dentro de ellos y es problemático.

Cuando no coloco el botón dentro del panel, funciona como lo quiero.

No estoy seguro de lo que estoy haciendo mal y he intentado mover la creación del botón a donde se crea el panel en la GridbagLayout, pero aún no funcionó.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test{

public void display(){
    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900,650);
    frame.setVisible(true);

    JPanel test = new JPanel();
    test.setLayout(new GridBagLayout());
    GridBagConstraints c= new GridBagConstraints();

    JPanel left= new JPanel();
    JPanel right= new JPanel();

    c.fill = GridBagConstraints.VERTICAL - GridBagConstraints.HORIZONTAL;
    c.weightx = 0.4;
    c.gridx = 1;
    c.weighty = 1;
    test.add(left,c);
    c.weightx = .6;
    c.gridx = 2;
    test.add(right,c);

    JButton button= new JButton("A button");
    left.add(button,c);//If I do not add this, then it shows how I want it to be

    frame.add(test);
   }
}
¿Fue útil?

Solución

Lo que pasa con los pesos es que describen qué hacer con el espacio adicional. Los componentes tienen sus dimensiones preferidas, min y máximas que el administrador de diseño utiliza cuando calcula el diseño. El GridbagLayout luego divide el espacio adicional usando estos pesos. En su caso, creo que el espacio que está dividido es igual a 900 botton.getPreferredSize (). Ancho. Estás dividiendo quizás 800 píxeles en 320 y 480.

Otros consejos

Aquí hay un ejemplo de un panel creado con GridbagLayout: (no se preocupe por Swing Factory, simplemente cree un componente en su lugar)

private void buildSourcePanel() {

  JPanel pnlSource = new JPanel();

  GridBagLayout gbl_pnlSource = new GridBagLayout();
  gbl_pnlSource.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0};
  gbl_pnlSource.columnWidths = new int[]{0, 0, 100, 100, 25};
  pnlSource.setLayout(gbl_pnlSource);

  final JLabel lblFolderMask = swingFactory.createLabel(" SOURCE DIRECTORY ", null, null, SwingConstants.LEFT, SwingConstants.CENTER, true);
  pnlSource.add(lblFolderMask, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 0), 0, 0));

  txtSource = swingFactory.createTextField(null, "txtSource", null, SystemColor.textHighlight, SwingConstants.LEFT, false, true, "Source Directory");
  pnlSource.add(txtSource, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));

  final JButton btnBrowse = new JButton("Browse...");
  btnBrowse.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5)));
  btnBrowse.setFont(new Font("Verdana", Font.BOLD, 14));
  pnlSource.add(btnBrowse, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));

  final JButton btnClear = new JButton("Clear...");
  btnClear.setBorder(new CompoundBorder(new LineBorder(new Color(0, 0, 0), 0, false), new EmptyBorder(5, 5, 5, 5)));
  btnClear.setFont(new Font("Verdana", Font.BOLD, 14));
  pnlSource.add(btnClear, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 0, 5, 5), 0, 0));

  lblStatus = swingFactory.createLabel(null, null, null, SwingConstants.CENTER, SwingConstants.CENTER, false);
  pnlSource.add(lblStatus, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0, 10, 1, new Insets(5, 5, 5, 5), 0, 0));
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top