下面是代码:JFrame中不显示的JMenuBar
private void initComponent() {
// TODO Auto-generated method stub
setTitle("Pizza Order System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1000, 900));
menu = new JMenu();
menuItem = new JMenuItem();
menuItem.setText("About");
menu.add(menuItem);
Pane = new JDesktopPane();
menuBar = new JMenuBar();
menuBar.add(menu);
mainPanel = new JPanel();
GroupLayout panelLayout = new GroupLayout(mainPanel);
mainPanel.setLayout(panelLayout);
setJMenuBar(menuBar);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(Pane)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(mainPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(5, 5, 5)
.addComponent(mainPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Pane, GroupLayout.DEFAULT_SIZE, 804, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}
对于我的代码,我已经初始化菜单,菜单项,菜单栏,添加菜单项到菜单,添加菜单到菜单栏,并设置菜单栏已经,看起来像一切看起来不错,但JmenuBar不想出现....
其实这并没有回答这个问题。另外我不认为OP要做记事本。我认为你的小部件的命名也很糟糕。 – pzaenger
是的,我同意你的观点,但是你可以把JMenuItem命名为你的愿望......并且你只能使用语句setJmenuBar(mb);在costructer结束时,它会出现.. – Surajchamp
该OP已使用'setJMenuBar(menuBar);',因此这不是问题。 – pzaenger