I have created 2 JMenus with various JButtons, JCheckboxes and a submenu. They are compiling and running ok. The problem is that they filling the full length of the JFrame, instead of appearing as two menus in a JMenubar which is what I need.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MenuExample extends JFrame { private JMenuBar menuBar; private JMenu menu, subMenu; private JMenuItem menuItem; private JRadioButtonMenuItem rbMenuItem; private JCheckBoxMenuItem cbMenuItem; public MenuExample() { menuBar = new JMenuBar(); menu = new JMenu("A Menu"); menu.setMnemonic('A'); menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items"); menuBar.add(menu); menuItem = new JMenuItem("A text-only menu item"); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything"); menu.add(menuItem); menuItem = new JMenuItem("Both text and icon", new ImageIcon("image/cross.gif")); menuItem.setMnemonic('B'); menu.add(menuItem); menu.addSeparator(); ButtonGroup group = new ButtonGroup(); rbMenuItem = new JRadioButtonMenuItem("A radio button menu item"); rbMenuItem.setSelected(true); rbMenuItem.setMnemonic('r'); group.add(rbMenuItem); menu.add(rbMenuItem); rbMenuItem = new JRadioButtonMenuItem("Another one"); rbMenuItem.setMnemonic('o'); group.add(rbMenuItem); menu.add(rbMenuItem); menu.addSeparator(); cbMenuItem = new JCheckBoxMenuItem("A check box menu item"); cbMenuItem.setMnemonic('c'); menu.add(cbMenuItem); cbMenuItem = new JCheckBoxMenuItem("Another one"); cbMenuItem.setMnemonic('h'); menu.add(cbMenuItem); menu.addSeparator(); subMenu = new JMenu("A submenu"); subMenu.setMnemonic('s'); menu.add(subMenu); menuItem = new JMenuItem("An item in the submenu"); menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK)); subMenu.add(menuItem); menuItem = new JMenuItem("Another item"); subMenu.add(menuItem); subMenu = new JMenu("Another Menu"); menuBar.add(menu); menuBar.add(subMenu); add(menuBar); } public static void main(String[] args) { MenuExample frame = new MenuExample(); frame.setTitle("Menu Example"); frame.setSize(300, 250); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } }