/ / JPanels en JPanel - java, swing, layout, jpanel, mezcla

JPanels en JPanel - java, swing, layout, jpanel, mezcla

Tengo un problema con Java JPanels. Me gustaría poner 2 JPanels con diferentes diseños en un JPanel que también tiene un diseño. ¿Es incluso posible hacerlo funcionar?

    BibP()
setLayout(new GridLayout(5, 1)); //The big JPanel
add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));

Tanto A como B son clases que se extienden como JPanels yNo importa lo que haya cambiado o comentado, B siempre aparece en 1 fila. Tengo 4 elementos agregados a A y 14 a B (JLabels y JTextAreas) y no hay mucho código en ellos solo los agregados y algunos cálculos.

El problema podría estar en el JFrame donde estoy tratando de poner el gran JPanel.

    JFrame.this.add(new BigP(),BorderLayout.CENTER);

Editar:

    public class BigP extends JPanel{
//Labels and TextAres

public class A extends JPanel{
public A(){
setBorder(new EmptyBorder(0, -50, 0, 0));
//get date and add to textareas
//add the label and textareas
}
}

public class B extends JPanel{
public B (){
setBorder(new EmptyBorder(0, -50, 0, 0));
setBackground(Color.red);
//colum longs for text areas less then 5
//add labels and textareas
}
}

public BigP(){
setLayout(new GridLayout(5, 1));
setBorder(new EmptyBorder(3,-160,0,0));

add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));
}
}

Gracias por ayudar.

Después de algunos intentos:

Si usara esto:

    add(new B(), new GridLayout(7,2));

Obtengo esto en B cuando imprimí el diseño:

java.awt.FlowLayout [hgap = 5, vgap = 5, align = center]

Si configuro el diseño en B:

    setLayout(new GridLayout(7, 2));

La información es correcta:

java.awt.GridLayout [hgap = 0, vgap = 0, filas = 7, cols = 2]

Pero solo hay 2 JTextAreas visibles donde deberían haber 14 elementos.

Respuestas

1 para la respuesta № 1

¿Es incluso posible hacerlo funcionar?

Sí, es comúnmente conocido como diseños compuestos.

El problema que tiene es que está tratando de suministrar el LayoutComo restricciones para el administrador de diseño de contenedores actual (que en su caso, su suerte no esperaba) ...

En su lugar, suministre el administrador de diseño directamente a los paneles secundarios ... algo así como ...

setLayout(new GridLayout(5, 1)); //The big JPanel

JPanel a = new JPanel(new FlowLayout(4));
JPanel b = new JPanel(new GridLayout(7,2));
add(a);
add(b);

Echa un vistazo a Disposición de componentes dentro de un contenedor para más detalles...

Actualizar con el ejemplo

Ejemplo simple ejecutable, utilizando etiquetas como marcadores de posición.

enter image description here

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class ExampleLayout {

public static void main(String[] args) {
new ExampleLayout();
}

public ExampleLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JPanel mainPane = new JPanel(new GridLayout(5, 1));
mainPane.add(new APanel());
mainPane.add(new BPanel());

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class APanel extends JPanel {

public APanel() {
setLayout(new FlowLayout(4));
for (int index = 0; index < 4; index++) {
add(new JLabel(Integer.toString(index)));
}
setBorder(new LineBorder(Color.RED));
}

}

public class BPanel extends JPanel {

public BPanel() {
setLayout(new GridLayout(7, 2));
for (int index = 0; index < 14; index++) {
add(new JLabel(Integer.toString(index)));
}
setBorder(new LineBorder(Color.BLUE));
}

}

}

Esta setBorder(new EmptyBorder(3,-160,0,0)); También se ve mal. EmptyBorder NUNCA debe tener valores negativos


0 para la respuesta № 2
 Is it even possible to make it work?

Sí, es posible hacer esto. En la linea siguiente

JFrame.this.add(new BigP(),BorderLayout.CENTER);

Sugeriría hacer

mainFrame.getContentPane.add(new BigP());

y configura el diseño de tu mainFrame antes de esto

mainFrame.setLayout(new GridLayout(5, 1));