I created a java code to position objects in the window of type JComboBox
and JTextField
and I want them to be one underneath the other. The code I tried is this:
/*Biblioteca */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
class creator {
public static void main(String args[]){
EventQueue.invokeLater(() -> {
/*Cria o layout*/
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
/* Gera os campos, tela, e configurações */
JFrame frame = new JFrame();
JPanel panel = new JPanel(layout);
JTextField texto = new JTextField();
texto.setColumns(15);
JComboBox combo = new JComboBox();
/* Cria as opções e configurações do JComboBox */
combo.setBackground(Color.WHITE);
combo.addItem("op\u00e7\u00e3o1");
combo.addItem("op\u00e7\u00e3o2");
combo.addItem("op\u00e7\u00e3o3");
combo.addItem("op\u00e7\u00e3o4");
combo.addItem("op\u00e7\u00e3o5");
combo.addItem("op\u00e7\u00e3o6");
combo.addItem("op\u00e7\u00e3o7");
/* Adiciona as coisas na tela */
panel.add(texto);
panel.add(combo, c);
/* Configurações da janela */
frame.setTitle("Layout");
frame.setSize(500, 500);
frame.getContentPane().add(panel);
/*seta como o arquivo fecha e sua visibilidade */
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
});
}
}