使用GridBagLayout在菜单栏下方定位按钮
我想将下面的两个按钮添加到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(); }
我想把我的按钮放在菜单栏的下方,但我不知道怎么做。
panel_raiz.add(menuBar, gbc);
不应将菜单栏添加到面板中,应将其添加到框架中:
f.setJMenuBar( menuBar);
但我不知道如何将GridBagConstraints放置在包含按钮的面板上,使其位于菜单栏下方。
不需要使用GridBagLayout的面板。 只是:
- 创建一个面板
- 将面板的布局设置为“左对齐”的
FlowLayout
。 - 向面板添加按钮
- 使用
f.add(panel, BorderLayout.PAGE_START)
将面板添加到框架
阅读Swing教程中有关如何使用菜单的部分的工作示例。 本教程还有一个关于Layout Mangers
的部分。 保持Swing基础知识教程的链接。
正如@Andrew Thompson所说,您可以将问题分解为多个布局,将按钮放在条形图中,然后放置该条形图。
话虽如此,你可以把NORTH_WEST作为锚定值。