我想将下面的两个按钮添加到JMenuBar
。我正在使用根JPanel
对于 GUI 的整个窗口,我向其中添加了不同的组件,到目前为止,有两个,菜单栏和包含两个按钮的面板。但我不知道如何放置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 教程中的部分如何使用菜单 https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html用于工作示例。本教程还有一节介绍Layout Mangers
。保留一个指向 Swing 基础知识教程的链接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)