public void createTopMenu(String menuName) { JFrame frame = new JFrame(); frame.setTitle("Create a new menu"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(200,200)); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = frame.getSize(); frame.setLocation((screenSize.width - frameSize.width) / 2,(screenSize.height - frameSize.height) /2 ); //Create and set up the content pane. TopMenu contentPane = new TopMenu(frame, menuName); contentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(contentPane); frame.validate(); //Display the window. frame.pack(); frame.setVisible(true); System.out.println(frame.getSize()); } /////////////////////////////////////////////// class TopMenu extends JPanel { JFrame frame; JPanel foodListPanel; JButton addItemButton; TopMenu(JFrame frame, String menuName) { super(new BorderLayout()); this.frame = frame; //frame.setSize(new Dimension(200,200)); JPanel topPanel = new JPanel(); JLabel menuNameLabel = new JLabel(menuName, JLabel.LEFT); topPanel.add(menuNameLabel); ActionListener buttonListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == addItemButton) { String s = JOptionPane.showInputDialog(null, "Enter Menu Item:", "Menu Item", JOptionPane.QUESTION_MESSAGE); foodListPanel.add(new JLabel(s)); updateUI(); } } }; JPanel bottomPanel = new JPanel(); addItemButton = new JButton("Add Item"); addItemButton.addActionListener(buttonListener); JButton doneButton = new JButton("Done"); bottomPanel.add(addItemButton); bottomPanel.add(doneButton); frame.validate(); foodListPanel = new JPanel(); foodListPanel.setLayout(new GridLayout(7, 0)); //JScrollPane scrollPane = new JScrollPane(); //scrollPane.add(foodListPanel); add(topPanel, BorderLayout.NORTH); add(foodListPanel, BorderLayout.CENTER); add(bottomPanel, BorderLayout.SOUTH); } }