public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 200));
JPanel body = new JPanel();
body.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx = 1.0;// 100%
c1.gridx = 0;// column 0
c1.gridy = 0;// row 0
body.add(new JLabel("Inserimento di un nuovo protocollo"), c1);
JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
GridBagConstraints c2 = new GridBagConstraints();
c2.fill = GridBagConstraints.BOTH;
c2.weightx = 1.0; // 100%
c2.gridx = 0; // column 0
c2.gridy = 1; // row 1
body.add(oggetto, c2);
frame.add(body);
frame.setVisible(true);
}
I recommend to use TableLayout since it the best layout to understand and to implement.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(400, 200));
JPanel body = new JPanel();
double[][] dim = {
{20, TableLayout.FILL, 20}, // column widths: 20 distance right/left, TableLayout.FILL usable place
{20, 17, 5, TableLayout.FILL, 20} // row height: 20 distance up/down, 17 place for label, 5 distance between label-textarea
};
body.setLayout(new TableLayout(dim));
JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
body.add(new JLabel("Inserimento di un nuovo protocollo"), "1,1"); // col: 1, row: 1
body.add(oggetto, "1,3");// col: 1(TableLayout.FILL), row: 3 (TableLayout.FILL)
frame.add(body);
frame.setVisible(true);
}
For more components to add define "dim" like this example shows:
double[][] dim = {
{20, TableLayout.FILL, 20},
{20, 17, 5, TableLayout.FILL /*label1,distance,component1*/, 17, 5, TableLayout.FILL/*label2,distance,component2 etc...*/, 20}
};