我的menuBar没有显示。我是否需要JPanel才能在我的GUI中显示?
private void buildCtrlPanel() {
ctrlPanel = new JPanel();
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
optionsMenu = new JMenu("Options");
JFrame frame = new JFrame();
frame.setJMenuBar(menuBar);
frame.setSize(350, 250);
frame.setVisible(true);
ctrlPanel.setLayout(new FlowLayout());
ctrlPanel.add(menuBar);
ctrlPanel.add(frame);
menuBar.add(fileMenu);
menuBar.add(optionsMenu);
}
答案 0 :(得分:1)
您只能将组件添加到一个容器中。你已经将JMenuBar恰当地添加到JFrame中了 - 但是你还错误地将它添加到JPanel(为什么?)使用FlowLayout,布局与JMenuBars不兼容(为什么呢?)。解决方案:不要这样做。就像你现在一样把它添加到JFrame中,然后保留它。
您似乎也在向JPanel添加一个JFrame - 这是您不应该做的事情,并且这再次暗示您需要在进一步处理之前完成Swing教程。