How do I define the size and location of components?

2

I created this code in java to train a little and learn but I can not set where JComboBox and JTextField will appear and the size of them like I do? I want to leave them in the middle of the window without being glued to the edge of the window and to each other and not taking the entire window. setSize and setBounds are not working.

Code:

/*Biblioteca */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

class creator {

public static void main(String args[]){

/*Gera os campos, tela, e configurações */
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JTextField texto = new JTextField();
JComboBox combo = new JComboBox();

/*Cria as opções e configurações do JComboBox */
combo.setBackground(Color.WHITE);
combo.addItem("opção1");
combo.addItem("opção2");
combo.addItem("opção3");

/*Adiciona as coisas na tela */
panel.add(texto);
panel.add(combo);

/*Configurações da janela*/
frame.setSize(500, 500);        
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true); 

}

}
    
asked by anonymous 21.10.2017 / 19:29

1 answer

1

To set up size, there are methods setSize , setPreferredSize , where both define the dimensions of the component, but the second the size is only relative. Because most containers use layouts managers , they usually are based on the second method to set the size of the components, although some are also based on setMaximumSize " and setMinimumSize . These methods are inherited from the class JComponent and Component

The position also depends on which layout you are using, each one has a placement feature, the link above is displayed each, you can choose which best suits your purpose, and even combine them if necessary.

Centralizing the components in your code was possible using GridBagLayout , without defining position or size:

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

public class PosicaoDeCompTest {

    public static void main(String args[]) {

        EventQueue.invokeLater(() -> {
            /* Gera os campos, tela, e configurações */
            JFrame frame = new JFrame();
            JPanel panel = new JPanel(new GridBagLayout());

            JTextField texto = new JTextField();
            texto.setColumns(10);
            JComboBox combo = new JComboBox();

            /* Cria as opções e configurações do JComboBox */
            combo.setBackground(Color.WHITE);
            combo.addItem("opção1");
            combo.addItem("opção2");
            combo.addItem("opção3");

            /* Adiciona as coisas na tela */
            panel.add(texto);
            panel.add(combo);

            /* Configurações da janela */
            frame.setSize(500, 500);
            frame.getContentPane().add(panel);

            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        });

    }

}

Output:

    

21.10.2017 / 19:49