将JMenuBar添加到JPanel?
问题描述:
我有一个JMenuBar和一个JPanel。我想将JMenuBar添加到JPanel。我该怎么做?
I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so?
答
你可以使用 BorderLayout ,并使用
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);
JMenuBar是一个JComponent,可以像任何其他JComponent一样添加到Container中。
JMenuBar is a JComponent and can be added to a Container like any other JComponent.