import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class PopupMenuFrame extends javax.swing.JFrame {

    public PopupMenuFrame() {
        

        // Unnecessary; will be called by default:
        // super();

        // Unnecessary; a JFrame's layout is already a BorderLayout
        // c.setLayout(new java.awt.BorderLayout());

        JButton button = new JButton("Click me");
        button.setPreferredSize(new Dimension(200, 200));
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showOptionPane();
            }
        });

        // See the javadoc; this method is not recommended:
        // getContentPane().add(BorderLayout.CENTER, button);
        // Use this one instead:
        getContentPane().add(button, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        PopupMenuFrame frame = new PopupMenuFrame();

        // setSize() may be ok for a test program, but I guess you know
        // that pack() is necessary in "real life".  Far better to set
        // the button's preferred size and let the layout manager
        // figure out the panel's size.
        // frame.setSize(new java.awt.Dimension(200,200));
        frame.pack();
        frame.setVisible(true);
    }

    private void showOptionPane() {

        // You can specify the layout manager in the JPanel's
        // constructor.  This saves the creation of a FlowLayout object
        // that would be discarded.
        
        // final JPanel pane = new JPanel();
        // pane.setLayout(new BorderLayout());
        final JPanel pane = new JPanel(new BorderLayout());

        JButton button = new JButton("Click me");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showPopupMenu(pane);
            }
        });

        // See comment above regarding add() methods.
        // pane.add(java.awt.BorderLayout.CENTER, button);
        pane.add(button, BorderLayout.CENTER);
        JOptionPane.showConfirmDialog(this, pane);
    }

    private void showPopupMenu(Component c) {
        JPopupMenu menu = new JPopupMenu();

        // Let's add some text to the button so we can see which part
        // of the panel it is:
        // JButton button = new JButton("");
        JButton button = new JButton("Some text");

        // But why are you adding a button here and not a JMenuItem?
        // And why do you want to set the size so large?  (I set it a
        // bit smaller so that I could see it all on my screen.)
        button.setPreferredSize(new Dimension(500,500));

        // If you comment the add(button) line out, the menu looks ok.
        // Why do you want to add a button to a popup menu?  Isn't a
        // menu item essentially like a button anyway?
        menu.add(button);

        // Let's add some menu items to the popup menu to see what they
        // would look like:
        for (int i = 0; i < 4; i++) {
            menu.add(new JMenuItem("Menu item " + i));
        }
        
        menu.show(c, 0, 0);
    }
}
