[英]In NetBeans how do I add a jMenuBar to a JPanel?
I am having problems and I don't really understand why. 我有问题,我不明白为什么。 I have a JFrame and a JPanel and everything works properly.
我有一个JFrame和一个JPanel,一切正常。 I am trying to add a jMenuBar into the JPanel and I cannot get it to show up.
我正在尝试将jMenuBar添加到JPanel中,但我无法让它显示出来。 It is being placed under "Other Components" and does not show up during runtime.
它被放置在“其他组件”下,并且在运行时不会显示。 any suggestions?
有什么建议么?
edit: It seems that the appropriate answer is NetBeans cannot add a JMenu to a JFrame. 编辑:似乎适当的答案是NetBeans无法将JMenu添加到JFrame。 I wanted to add this to the first post because the appropriate answer below was down-voted.
我想在第一篇文章中添加这个内容,因为下面的相应答案是低调的。
JMenuBar is added to the JFrame by using setJMenuBar(...) method. 通过使用setJMenuBar(...)方法将JMenuBar添加到JFrame。
Small code to help your cause : 小代码,以帮助您的事业:
import javax.swing.*;
public class MenuBarTest extends JFrame
{
public MenuBarTest()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel contentPane = new JPanel();
contentPane.setBackground(java.awt.Color.WHITE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
JMenuItem menuItem = new JMenuItem("Open");
menu.add(menuItem);
menuBar.add(menu);
setContentPane(contentPane);
setJMenuBar(menuBar);
setSize(200, 200);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new MenuBarTest();
}
});
}
}
其中一个聪明的方法是双击JFrame,它在项目栏上,这个出现了新窗口,实际的JFrame在左侧调色板上出现了所有摆动的组件,你只需将项目拖放到此框架代码将由nb自动生成,您还可以通过右键单击该项目来添加事件
For vextorspace who states: 对于指出以下内容的vextorspace:
JMenuBar can only be added to JFrames, JDialogs, and JApplets.
JMenuBar只能添加到JFrames,JDialogs和JApplets中。
This example shows that it is easy to add JMenuBar to a JPanel (or any container for that matter): 此示例显示将JMenuBar添加到JPanel(或任何容器)很容易:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class MenuBarEg {
private static void createAndShowGui() {
final JFrame frame = new JFrame("MenuBar Exampe");
JMenuItem barItem = new JMenuItem(new AbstractAction("Bar") {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(frame, "Hello from bar!");
}
});
JMenu fooMenu = new JMenu("Foo");
fooMenu.add(barItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fooMenu);
JPanel menuBarHoldingPanel = new JPanel(new BorderLayout());
menuBarHoldingPanel.add(menuBar, BorderLayout.PAGE_START);
JPanel mainPanel = new JPanel(new GridLayout(0, 1));
// rigid area just as a place-holder
mainPanel.add(Box.createRigidArea(new Dimension(400, 150)));
mainPanel.add(menuBarHoldingPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Not only is this easy to do, there are many cases where this is desirable. 这不仅易于实现,而且在许多情况下这是可取的。
Since a JMenuBar derives from JComponent it can be added to any container (usually one using BorderLayout to the BorderLayout.PAGE_START position), it is most commonly added to JApplet, JDialog, JFrame, JInternalFrame, JRootPane via the setJMenuBar(...) method. 由于JMenuBar派生自JComponent,它可以添加到任何容器(通常是使用BorderLayout到BorderLayout.PAGE_START位置的容器),它最常通过setJMenuBar(...)方法添加到JApplet,JDialog,JFrame,JInternalFrame,JRootPane 。
http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
Just a small addition : 只是一小部分:
A menu bar contains one or more menus and has a customary, platform-dependent location — usually along the top of a window. 菜单栏包含一个或多个菜单,并具有通常的平台相关位置 - 通常位于窗口顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.