I am trying to add a JMenuBar using the GridBagLayout organizer, and am unable to do so.
Could someone explain to me how to do it?
I have a basic exercise where I have added label and buttons for testing. So far, I have no problems, but when it comes to adding the menu bar, I get lost.
This is my code:
public class ClaseGridBagLayout extends JFrame implements ActionListener{
GridBagLayout admin = new GridBagLayout();
GridBagConstraints subLayout;
//instancio el menú
JMenuBar miMenu;
JMenu mArchivo;
JMenuItem iCopiar;
JMenuItem iPegar;
JMenu mAyuda;
JMenuItem iAyuda;
//instancio los label del ejercicio
JLabel lblTitulo;
JLabel lblFila1Dos;
JLabel lblFila2Uno;
JLabel lblFila2Tres;
JLabel lblFila3Dos;
//instancio los botones del ejercicio
JButton btnFila1Uno;
JButton btnFila1tres;
JButton btnFila2Dos;
JButton btnFila3Uno;
JButton btnFila3Tres;
JButton btnCerrar;
//cnstructor de la clase
public ClaseGridBagLayout(){
super(" La clase ClaseGridBagLayout");
//creo los distintos métodos
metodoCargarConfiguracion();
metodoCargarIconos();
metodoCargarMenu();
metodoCargarComponentesPasivos();
metodoCargarComponentesActivos();
//le doy la medida necesaria para los elemtos que se carguen
pack();
//lo hago visible
setVisible(true);
}
//como ejemplo le doy función al botón cerrar
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCerrar){
System.exit(0);
}
}
//aquí cargo la configuración general con el controlador GridBagLayout
private void metodoCargarConfiguracion() {
this.getContentPane().setLayout(new GridBagLayout());
//creo el gestor para las posiciones en pantalla
subLayout = new GridBagConstraints();
setLookAndFeel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void metodoCargarIconos() {
}
//cargo label y los posiciono por la ventana
private void metodoCargarComponentesPasivos() {
lblTitulo = new JLabel("Título");
subLayout.gridx = 0;
subLayout.gridy = 1;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(lblTitulo, subLayout);
lblFila1Dos = new JLabel("1 - 2");
subLayout.gridx = 2;
subLayout.gridy = 2;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(lblFila1Dos, subLayout);
lblFila2Uno = new JLabel("2 - 1");
subLayout.gridx = 1;
subLayout.gridy = 3;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(lblFila2Uno, subLayout);
lblFila2Tres = new JLabel("2 - 3");
subLayout.gridx = 3;
subLayout.gridy = 3;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(lblFila2Tres, subLayout);
lblFila3Dos = new JLabel("3 - 2");
subLayout.gridx = 2;
subLayout.gridy = 4;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(lblFila3Dos, subLayout);
}
//cargo botones y los posiciono por la ventana
private void metodoCargarComponentesActivos() {
btnFila1Uno = new JButton("1 - 1");
subLayout.gridx = 1;
subLayout.gridy = 5;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(btnFila1Uno, subLayout);
btnFila1tres = new JButton("1 - 3");
subLayout.gridx = 3;
subLayout.gridy = 5;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(btnFila1tres, subLayout);
btnFila2Dos = new JButton("2 - 2");
subLayout.gridx = 2;
subLayout.gridy = 6;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(btnFila2Dos, subLayout);
btnFila3Uno = new JButton("3 - 1");
subLayout.gridx = 1;
subLayout.gridy = 7;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(btnFila3Uno, subLayout);
btnFila3Tres = new JButton("3 - 3");
subLayout.gridx = 3;
subLayout.gridy = 7;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(btnFila3Tres, subLayout);
btnCerrar = new JButton("Cerrar");
btnCerrar.addActionListener(this);
subLayout.gridx = 2;
subLayout.gridy = 8;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(btnCerrar, subLayout);
}
//método principal
public static void main(String[] args){
ClaseGridBagLayout miClase = new ClaseGridBagLayout();
}
//método sin función con el diseño por defecto
private void setLookAndFeel() {
try{
UIManager.setLookAndFeel("");
}
catch (Exception e){
}
}
//aquí intento cargar el menú sin éxito
private void metodoCargarMenu() {
//creo la barra de menús
miMenu = new JMenuBar();
//creo el menú y sus item
mArchivo = new JMenu();
iCopiar = new JMenuItem();
iPegar = new JMenuItem();
mAyuda = new JMenu();
iAyuda = new JMenuItem();
//agrego el menú a la barra
miMenu.add(mArchivo);
miMenu.add(mAyuda);
//agrego los item al menú
mArchivo.add(iCopiar);
mArchivo.add(iPegar);
mAyuda.add(iAyuda);
//agrego la barra al panel principal, pero no se como agregarlo al constraint del GridBagLayout. La verdad, no tengo idea de como se ha de hacer
this.setJMenuBar(miMenu);
subLayout.gridx = 0;
subLayout.gridy = 0;
subLayout.weightx = 1;
subLayout.weighty = 1;
this.getContentPane().add(miMenu, subLayout);
}
}
The truth is that I'm a bit lost with this topic of the bar, different tests and none of them work.
I've tried adding a JPanel, and a layout inside the JPanel, adding the bar to the layout, but I can't do it either.
//creo el panel
JPanel fila1 = new JPanel();
//creo el gestor de la ventana del panel
GridLayout f1 = new GridLayout(2, 1);
//agrego el menú al gestor del panel
fila1.add(miMenu);
//agrego el gestor al panel
fila1.setLayout(f1);
//agrego el panel al panel principal GridBagLayout gestionado por el constraint subLayout;
this.getContentPane().add(fila1, subLayout);
Could someone give me light on this?
The JMenu Bar should not be added to the JPanel, it should be added to the JFrame this.setJMenuBar(myMenu);
Take a look at this link , it will help you a lot.
Voucher
After looking at the link that Jua'n Albert'o suggested and searching and searching, I was able to add the menu by changing the initial configuration a little
Now I have left like this: