Why is it that when I put on the "Jcombobox" component of Swing, my show looks like this?

Asked

Viewed 75 times

0

inserir a descrição da imagem aqui

code I used

import javax.swing.*;

public class ComboBox extends JFrame{

JComboBox<String> combo = new JComboBox<String>();

public ComboBox(){
    add(combo);
    combo.addItem("Brasil");
    combo.addItem("Argentina");

    setTitle("Título da Janela");
    setSize(400,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);

}

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

   }

}

sorry if the question was stupid, is that I was following java lessons on swing and the appearance of this component was different...it was more beautiful, have I fix it?

  • 1

    You have to set the Jframe layout

  • can you give me an example? I’m very lay yet ;-;

1 answer

1

In java-swing, using the Layout Managers to organize the components as needed, and when we do not inform any, Jframe by default uses Borderlayout, which is basically a layout that organizes the components automatically, using coordinates, as shown below:

inserir a descrição da imagem aqui

When adding the component without handling the layout, it is added with the coordinate BorderLayout.CENTER, and according to the definition of the layout itself in the java documentation, the component will fill the entire space of the defined area, as each coordinate of the above figure, in the case of the central, if there is nothing else on the screen, will complete it.

If you do not want this behavior, test different layouts, in the link above there is a list of them and how each works.

Browser other questions tagged

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