使用 GridBagLayout 将按钮定位在菜单栏下方
Positioning buttons below menu bar with GridBagLayout
我想将我的两个按钮添加到 JMenuBar
。我在 GUI 的整个 window 中使用根 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();
}
我想把我的按钮放在菜单栏的正下方,但我不知道怎么做。
正如@Andrew Thompson 所说,您可以将问题分解为多个布局,将您的按钮放在一个栏中,然后再放置那个栏。
话虽如此,您可以放回 NORTH_WEST 作为锚值。
panel_raiz.add(menuBar, gbc);
不应将菜单栏添加到面板,而应将其添加到框架:
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.
不需要使用 GridBagLayout 的面板。只是:
- 创建面板
- 将面板布局设置为
FlowLayout
即 "left aligned"。
- 向面板添加按钮
- 使用
f.add(panel, BorderLayout.PAGE_START)
将面板添加到框架
阅读有关 How to Use Menus 的 Swing 教程部分以获取工作示例。本教程还有一个关于 Layout Mangers
的部分。保留一份 link 教程以方便学习 Swing 基础知识。
我想将我的两个按钮添加到 JMenuBar
。我在 GUI 的整个 window 中使用根 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();
}
我想把我的按钮放在菜单栏的正下方,但我不知道怎么做。
正如@Andrew Thompson 所说,您可以将问题分解为多个布局,将您的按钮放在一个栏中,然后再放置那个栏。
话虽如此,您可以放回 NORTH_WEST 作为锚值。
panel_raiz.add(menuBar, gbc);
不应将菜单栏添加到面板,而应将其添加到框架:
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.
不需要使用 GridBagLayout 的面板。只是:
- 创建面板
- 将面板布局设置为
FlowLayout
即 "left aligned"。 - 向面板添加按钮
- 使用
f.add(panel, BorderLayout.PAGE_START)
将面板添加到框架
阅读有关 How to Use Menus 的 Swing 教程部分以获取工作示例。本教程还有一个关于 Layout Mangers
的部分。保留一份 link 教程以方便学习 Swing 基础知识。