How do I define the size and place of components?

Asked

Viewed 499 times

2

I created this code in java to train a little and learn but I can not set the place where the JComboBox and the JTextField will appear and the size of them as I do? I want to leave them in the middle of the window without being stuck to the edge of the window and each other and without them grabbing the whole 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); 

}

}
  • Where and how you want to position them?

  • type in the middle of the window with the textfield first above the size combobox that does not catch the whole window.

1 answer

1


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

The position also depends on which layout is being used, each one has a positioning feature, in the link above is presented each, you can choose which one best serves your goal, and even combine them if necessary.


Centralizing the components in your code was possible using GridBagLayout, without setting 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);
        });

    }

}

Exit:

inserir a descrição da imagem aqui

Note that managers take care of component size most of the time, so it is not necessary to set fixed values for this, let the layout do this work for you.

  • setSize and setPreferredSize are not working.

  • @Bitsofbytes see the edition.

  • only 1 question has to join 2 layouts in the same Jpanel?

  • @Bitsofbytes impossible. Each panel can have only one layout.

  • ok vlw by the help ^_^

  • mals bother dnv more as I send Jcombobox down from textField?

  • @Bitsofbytes recommend that you access the links I left in the question to study the functioning of this layout. You’re asking questions that the links explain. It’s critical to know what you’re dealing with, if you really want to create good screens, and for that, you need to understand how the layout works. This is the official tutorial link for Gridbaglayout: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.