bien, así que esto resuelve un gran parte de mis preocupaciones con respecto al uso del Administrador de maquetas y la adición de las fronteras a los grupos de componentes:
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
public class TestingGround extends JFrame {
private JTextField tLenUnit, tLenResult, tAreUnit, tAreResult;
private JComboBox<String> cLenInUnit, cLenOutUnit, cAreInUnit, cAreOutUnit;
private JPanel lenMicro, lenMicro1, lenMicro2, lenNormal, lenMacro, area, volume;
private Border bGreyLine, bTitled1, bTitled2;
private TestingGround() {
setTitle("Testing Ground for an Application");
setVisible(true);
setResizable(true);
setLocationRelativeTo(null);
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
lenMicro = new JPanel();
lenMicro.setLayout(new GridBagLayout());
lenMicro1 = new JPanel();
lenMicro1.setLayout(new GridBagLayout());
bGreyLine = BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1, true);
bTitled1 = BorderFactory.createTitledBorder(bGreyLine, "Length", TitledBorder.LEFT, TitledBorder.TOP);
lenMicro1.setBorder(bTitled1);
tLenUnit = new JTextField("0.0");
tLenUnit.setColumns(10);
lenMicro1.add(tLenUnit, new GBC(0, 0, 2, 1).setAnchor(GBC.WEST).setInsets(5, 5, 5, 5));
cLenInUnit = new JComboBox<String>();
cLenInUnit.addItem("");
cLenInUnit.addItem("First");
cLenInUnit.addItem("Second");
cLenInUnit.setSelectedIndex(0);
cLenInUnit.setPreferredSize(new Dimension(120, 25));
lenMicro1.add(cLenInUnit, new GBC(2, 0, 3, 1));
tLenResult = new JTextField("");
tLenResult.setColumns(10);
lenMicro1.add(tLenResult, new GBC(5, 0, 2, 1).setInsets(5, 5, 5, 5));
cLenOutUnit = new JComboBox<String>();
cLenOutUnit.addItem("First");
cLenOutUnit.addItem("Second");
cLenOutUnit.setSelectedIndex(1);
cLenOutUnit.setPreferredSize(new Dimension(120, 25));
lenMicro1.add(cLenOutUnit, new GBC(7, 0, 1, 1));
// Area part:
lenMicro2 = new JPanel();
lenMicro2.setLayout(new GridBagLayout());
bTitled2 = BorderFactory.createTitledBorder(bGreyLine, "Area", TitledBorder.LEFT, TitledBorder.TOP);
lenMicro2.setBorder(bTitled2);
tAreUnit = new JTextField("0.0");
tAreUnit.setColumns(10);
lenMicro2.add(tAreUnit, new GBC(0, 1, 2, 1).setAnchor(GBC.WEST).setInsets(5, 5, 5, 5));
cAreInUnit = new JComboBox<String>();
cAreInUnit.addItem("");
cAreInUnit.addItem("One sqm");
cAreInUnit.addItem("Two sqm");
cAreInUnit.setSelectedIndex(0);
cAreInUnit.setPreferredSize(new Dimension(120, 25));
lenMicro2.add(cAreInUnit, new GBC(2, 1, 3, 1));
tAreResult = new JTextField("");
tAreResult.setColumns(10);
lenMicro2.add(tAreResult, new GBC(5, 1, 2, 1).setInsets(5, 5, 5, 5));
cAreOutUnit = new JComboBox<String>();
cAreOutUnit.addItem("One sqm");
cAreOutUnit.addItem("Two sqm");
cAreOutUnit.setSelectedIndex(1);
cAreOutUnit.setPreferredSize(new Dimension(120, 25));
lenMicro2.add(cAreOutUnit, new GBC(7, 1, 1, 1));
// Joining all lenMicroX panels into one:
lenMicro.add(lenMicro1, new GBC(0, 0, 8, 1).setAnchor(GBC.FIRST_LINE_START).setInsets(5, 5, 5, 5).setIpad(10, 10));
lenMicro.add(lenMicro2, new GBC(0, 1, 8, 1).setAnchor(GBC.LINE_START).setInsets(5, 5, 5, 5).setIpad(10, 10));
volume = new JPanel();
volume.setLayout(null);
// Panel definition --begin:
JTabbedPane tPane = new JTabbedPane();
tPane.addTab("Length & Area", null, lenMicro, "Length & Area units");
tPane.addTab("Volume", null, volume, "Volume units");
add(tPane);
// Panel --end.
}
public static void main(String[] args) {
TestingGround app = new TestingGround();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.pack();
app.setVisible(true);
}
}
GridBagConstraints clase de ayuda se toma aquí para allá m Cay Horstmann's y Gary Cornell's Core Java y se pueden encontrar aquí: http://www.horstmann.com/articles/GBC.java.
he conseguido averiguar a partir del ejemplo de Gagandeep que los paneles se pueden poner en la parte superior de, llamémoslo una madre de pantalla. De esta forma, cada uno de los paneles secundarios puede tener su propio borde.
Segunda cosa: decidí usar GridBagLayout, ya que parece ser la más precisa para ser utilizado en este y otros proyectos de la mía.Bueno, todavía tengo que aprender mucho al respecto (por ejemplo, para alinear los componentes con algunas líneas ;-) [edit: acaba de agregar setPreferredSize() a JComboBoxes; hace que el diseño se vea mejor ;-)]).
Gracias a todos por su ayuda y valiosos consejos!
diría - LayoutManager utilizar en el 99,99% de los casos. La única excepción podría ser si proporciona algunos objetos aleatorios, p. usuario dibujando formas. – StanislavL
¿Podría adjuntar algunas capturas de pantalla en lugar de describir la situación? –
La API dice que coloque el borde en el contenedor que contiene el componente. –