PessoALL, boa noite!
Gostaria de um esclarecimento acerca da diferença dos códigos abaixo:
Esse eu adiciono meus componentes em um JFrame.public class AgendaGUI extends JFrame {
JButton addButton;
JButton findButton;
JButton showButton;
JButton exitButton;
//JLabel label1;
//JLabel label2;
public AgendaGUI() {
super(">>> AGENDA <<<");
setLayout(new GridBagLayout());
GridBagConstraints gbl = new GridBagConstraints();
gbl.insets = new Insets(20,20,0,0);
gbl.anchor = GridBagConstraints.NORTHWEST;
addButton = new JButton("Add");
add(addButton, gbl);
findButton = new JButton("Find");
gbl.gridy = 1;
add(findButton, gbl);
showButton = new JButton("Show");
gbl.gridy = 2;
add(showButton, gbl);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
AgendaGUI ag = new AgendaGUI();
}
}
public GUI2() {
super(">>> AGENDA <<<");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new GridBagLayout());
GridBagConstraints gbl = new GridBagConstraints();
gbl.insets = new Insets(20,20,0,0);
gbl.anchor = GridBagConstraints.NORTHWEST; //essa linha aparentemente não serve p/ nada, quando deveria ajustar os componentes na tela
addButton = new JButton("Add");
c.add(addButton, gbl);
findButton = new JButton("Find");
gbl.gridy = 1;
c.add(findButton, gbl);
showButton = new JButton("Show");
gbl.gridy = 2;
c.add(showButton, gbl);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
GUI2 ag = new GUI2();
}
}
Pele menos até aqui não consegui notar nenhuma diferença na interface que é gerada.
Então, qual a diferença entre add ao JFrame e ao Container?
Obrigado!