Java 使用GridBagLayout在菜单栏下方定位按钮
我想将下面的两个按钮添加到Java 使用GridBagLayout在菜单栏下方定位按钮,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我想将下面的两个按钮添加到JMenuBar。我在GUI的整个窗口中使用rootJPanel,并向其中添加了不同的组件,到目前为止,有两个,菜单栏和一个包含两个按钮的面板。但是我不知道如何将GridBagConstraints放置在包含按钮的面板上,使其刚好位于菜单栏下方 这是我的代码: JFrame f = new JFrame("Cliente AEMET"); JPanel panel_raiz = new JPanel();//root JPanel GridBagConstraints
JMenuBar
。我在GUI的整个窗口中使用rootJPanel
,并向其中添加了不同的组件,到目前为止,有两个,菜单栏和一个包含两个按钮的面板。但是我不知道如何将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();
}
我想把我的按钮放在菜单栏的正下方,但我不知道如何操作。正如@Andrew Thompson所说,您可以将问题分成多个布局,将按钮放在一个栏中,然后再放在该栏中 话虽如此,你可以把NORTH_WEST作为锚定值
panel_raiz.add(menuBar, gbc);
菜单栏不应添加到面板,而应添加到框架:
f.setJMenuBar( menuBar);
但我不知道如何将包含按钮的面板的GridBagConstraints放置在菜单栏下方
不需要使用GridBagLayout的面板。只是:
FlowLayout
f.add(panel,BorderLayout.PAGE\u START)将面板添加到框架中
阅读上Swing教程中的部分以了解工作示例。本教程还有一节介绍
布局管理器
。保留一个指向Swing基础教程的链接。为什么要更改锚定部分?老实说,我不知道,我在互联网上查看了一些示例,看看它做了什么,我把它放在这里只是为了看看是否有什么变化,但什么都没变。但是如果您在菜单函数中引用锚定,我会将其定位到左上角gbc.gridy=GridBagConstraints.RELATIVE代码>这应该是键在哪里?我有,但没有任何改变,将其更改为gbc.gridy=1代码>