【问题标题】:Positioning buttons below menu bar with GridBagLayout使用 GridBagLayout 在菜单栏下方定位按钮
【发布时间】:2018-09-05 17:02:33
【问题描述】:

我想将下面的两个按钮添加到JMenuBar。我为 GUI 的整个窗口使用了一个根 JPanel,并且我正在向它添加不同的组件,到目前为止,两个,菜单栏和一个包含两个按钮的面板。但我不知道如何将包含按钮的面板的GridBagConstraints 放置在菜单栏下方。

这是我的代码:

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();

}

我想把按钮放在菜单栏的正下方,但我不知道怎么做。

【问题讨论】:

  • 为什么要更改锚部分?
  • 老实说,我不知道,我在互联网上查看示例,看看它做了什么,我放它只是为了看看是否有变化,但没有任何变化。但是如果你在 menus 函数中引用了锚点,我将它定位在左上角
  • gbc.gridy = GridBagConstraints.RELATIVE; 这应该是关键
  • 在哪里?我有,但没有任何变化
  • 改成gbc.gridy = 1;

标签: java swing layout-manager gridbaglayout


【解决方案1】:
panel_raiz.add(menuBar, gbc);

菜单栏不应该被添加到面板中,它应该被添加到框架中:

f.setJMenuBar( menuBar);

但我不知道如何将包含按钮的面板的 GridBagConstraints 放置在菜单栏下方。

不需要使用 GridBagLayout 的面板。只是:

  1. 创建面板
  2. 将面板的布局设置为“左对齐”的FlowLayout
  3. 向面板添加按钮
  4. 使用f.add(panel, BorderLayout.PAGE_START)将面板添加到框架中

阅读 How to Use Menus 上的 Swing 教程中的部分以获取工作示例。本教程还有一个关于Layout Mangers 的部分。保留一个指向 Swing 基础教程的链接。

【讨论】:

    【解决方案2】:

    正如@Andrew Thompson 所说,您可以将问题拆分为多个布局,将按钮放在一个栏中,然后再放置该栏。

    话虽如此,您可以将 NORTH_WEST 放回作为锚值。

    【讨论】:

      猜你喜欢
      • 2010-11-13
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多