没有理由菜单栏不起作用?

时间:2016-02-06 22:30:07

标签: java swing jpanel jmenubar jpanelmenu

在这段代码中,我完全不知道为什么我的菜单栏不起作用,对于每个人来说可能看似微不足道和愚蠢,但如果有人能告诉我为什么我的菜单,我会非常感激酒吧没有显示..

[Bonus]另外,我不知道为什么"添加(Canvas);"不起作用,但无论如何我设法通过一些研究找到了解决方案,但如果可能的话,对此的解释将是有用的。

package drawer;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import java.util.*;

public class Drawer {
public static void main(String[] args) {

        JMenuBar MenuBar; // My Menu code, which doesn't work..
        MenuBar = new JMenuBar();

        JMenu FileMenu = new JMenu("File");
        MenuBar.add(FileMenu);

        JMenuItem FileSaveMenu = new JMenuItem("Save");
        FileMenu.add(FileSaveMenu);


        JMenuItem FileLoadMenu = new JMenuItem("Load");
        FileMenu.add(FileLoadMenu);
        JMenuItem FileExitMenu = new JMenuItem("Exit");
        FileMenu.add(FileExitMenu);


        JMenu HelpMenu = new JMenu("Help");  
        JMenuItem FileAboutMenu = new JMenuItem("About");
        HelpMenu.add(FileAboutMenu); 

        JFrame MainWindow = new JFrame(); 
        FlowLayout layoutObj = new FlowLayout();
        MainWindow.setLayout(layoutObj);
        MainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainWindow.setSize(1600, 900); 
        MainWindow.setVisible(true);      


        JPanel ControlPanel = new JPanel();
        ControlPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));     
        ControlPanel.setBorder(new TitledBorder(new EtchedBorder(), "Control Panel"));
        ControlPanel.setPreferredSize(new Dimension(200, 750));              
        MainWindow.getContentPane().add(ControlPanel);

        JPanel Canvas = new JPanel();
        Canvas.setLayout(new FlowLayout(FlowLayout.CENTER)); 
        Canvas.setBorder(new TitledBorder(new EtchedBorder(), "Canvas")); 
        Canvas.setPreferredSize(new Dimension(1300, 750)); 
        MainWindow.getContentPane().add(Canvas); // Where "add(Canvas);" doesn't work and this was the solution after researching..  

        JPanel MessageArea = new JPanel();
        MessageArea.setLayout(new FlowLayout(FlowLayout.CENTER));     
        MessageArea.setBorder(new TitledBorder(new EtchedBorder(), "Messages"));
        MessageArea.setPreferredSize(new Dimension(1500, 100));
        MainWindow.getContentPane().add(MessageArea);
    }

}

1 个答案:

答案 0 :(得分:3)

添加

MainWindow.setJMenuBar(MenuBar);

最后