Positioning buttons below menu bar with GridBagLay

2019-03-05 17:35发布

问题:

I want to add my two buttons just below to the JMenuBar. I am using a root JPanel for the entire window of the GUI, and I am adding to it different components, so far, two, the menu bar and a panel that contains two buttons. But I don't know how to place the GridBagConstraints for the panel that contains the buttons in a way that it places just below the menu bar.

This is my code:

JFrame f = new JFrame("Cliente AEMET");
JPanel panel_raiz = new JPanel();//root JPanel
GridBagConstraints gbc = new GridBagConstraints();

public VentanaPrincipal (){


    f.setSize(500, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    panel_raiz.setLayout(new GridBagLayout());
    //textoInfoMunicipio();
    //ventana_aviso(); //descomentar si se quiere ver el JDialog    
    //lista();
    menus(); //the JMenuBar
    botones(); //the buttons
    //lista_desplegable();
    //d.acerca_de();
    f.setContentPane(panel_raiz); //here I put all the components in the root jpanel
    f.setVisible(true); //esto siempre tiene que ir lo ultimo
}
public void menus(){ //this function puts the JMenuBar

    JMenuBar menuBar;
    JMenu archivo, ayuda;
    JMenuItem menuItem;

    //creamos la barra de menus
    menuBar = new JMenuBar();

    //vamos a crear los menus
    archivo = new JMenu("Archivo");
    ayuda = new JMenu("Ayuda");

    menuItem = new JMenuItem("Crear Municipio");
    archivo.add(menuItem);
    menuItem = new JMenuItem("Borrar Municipio");
    archivo.add(menuItem);
    menuItem = new JMenuItem("Salir");
    archivo.add(menuItem);

    menuItem = new JMenuItem("Acerca de");
    ayuda.add(menuItem);

    //los añadimos
    menuBar.add(archivo);
    menuBar.add(ayuda);

    //aqui ya habría que hacer la parte del gridBagLayout
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor  = GridBagConstraints.NORTHWEST;
    gbc.weightx = 1.0;
    gbc.weighty = 0.5;

    panel_raiz.add(menuBar, gbc);
    //f.setContentPane(panel_raiz);
    //f.setJMenuBar(menuBar); esto lo teniamos antes de poner el gridBagLayout

}
public void botones(){

    ImageIcon addBBDD = new ImageIcon("./iconos/database-add-icon.png");
    ImageIcon deleteBBDD  = new ImageIcon("./iconos/database-delete-icon.png");
    JButton b1,b2;
    JPanel panel = new JPanel();


    panel.setLayout(new FlowLayout(FlowLayout.LEFT));
    b1 = new JButton("Municipio");
    b1.setIcon(addBBDD);
    panel.add(b1);
    b2 = new JButton("Municipio");
    b2.setIcon(deleteBBDD);
    panel.add(b2);

    //vamos a poner los constraints del gbl para los botones

    //gbc.gridx = 1;
    gbc.gridy = GridBagConstraints.RELATIVE;
    gbc.gridheight = GridBagConstraints.REMAINDER;
    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.anchor  =GridBagConstraints.LINE_START;
    //gbc.weightx = 0.5;



    panel_raiz.add(panel,gbc);
    //f.setContentPane(panel_raiz); no hace falta, pues en el constructor al final, ponemos todo el panel con lo que fuimos añadiendo en cada metodo

    //esto lo teniamos antes de poner el gridbaglayout
    //f.setContentPane(panel);
    //f.pack();

}

I want to put my buttons just below to the menu bar but I don't know how.

回答1:

panel_raiz.add(menuBar, gbc);

The menubar should NOT be added to the panel, it should be added to the frame:

f.setJMenuBar( menuBar);

But I don't know how to place the GridBagConstraints for the panel that contains the buttons in a way that it places just below the menu bar.

There is no need for a panel using a GridBagLayout. Just:

  1. create a panel
  2. set the layout of the panel to a FlowLayout that is "left aligned".
  3. add buttons to the panel
  4. add the panel to the frame using f.add(panel, BorderLayout.PAGE_START)

Read the section from the Swing tutorial on How to Use Menus for working examples. The tutorial also has a section on Layout Mangers. Keep a link to the tutorial handy for Swing basics.



回答2:

As @Andrew Thompson says, you may split the problem in more than one layout, placing your buttons in a bar and then placing that bar.

Having said that, you could just put back NORTH_WEST as anchor value.