final File f = new File("C:\\abc.txt");
         final String txt = URLFileSystem.getContents(f);
         final JFrame frame = new JFrame("文件内容");
         frame.setLayout(new BorderLayout());
         frame.setIconImage(new ImageIcon("D:\\images\\fastpath24.png").getImage());
              
         final JMenuBar MenuBar = new JMenuBar();
         //final JToolBar topToolbar1 = new JToolBar();
         frame.setJMenuBar(MenuBar);
         //frame.getContentPane().add(topToolbar1, BorderLayout.NORTH);
        
         final JMenu file = new JMenu();         final JMenuItem newfile = new JMenuItem();
         newfile.setText("新建");         final JMenuItem open = new JMenuItem();
         newfile.setText("打开");        
         file.add(newfile);
         file.add(open);
        
         MenuBar.add(file);
        
         final JTextPane pane = new JTextPane();
         pane.setBackground(Color.white);
         pane.setFont(new Font("Dialog",Font.PLAIN,12));
         pane.setEditable(false);
         pane.setText(txt);
        
         frame.add(new JScrollPane(pane),BorderLayout.CENTER);
         frame.pack();
         frame.setSize(600,500);
         GraphicUtils.centerWindowOnScreen(frame);
         frame.setVisible(true);
可是结果如下图所示:第一:跟本看不见JMenuBar,但有确实存在,用鼠标单击左上角相应区域也会弹出菜单来
第二:当file菜单中有两个以上选项时,只显示最后添加的那个选项,比如程序中只显示“打开”选项望大家指教!

解决方案 »

  1.   

    是不是frame.setLayout(new BorderLayout());
    这句有问题呢?
      

  2.   

    Menu items, like other components, can be in at most one container. If you try to add a menu item to a second menu, the menu item will be removed from the first menu before being added to the second. For a way of implementing multiple components that do the same thing你container获取了吗
      

  3.   

    frame.setJMenuBar(MenuBar);
    --------->改为
    frame.getContentPane().add("North",MenuBar);效果是一样
      

  4.   

    第二个问题是笔误                final JMenuItem newfile = new JMenuItem();
                    newfile.setText("新建");                final JMenuItem open = new JMenuItem();
                    newfile.setText("打开");
    newfile.setText("打开");改为
    open.setText("打开");即可求解问题一!!!
      

  5.   

    两个问题1、final JMenu file = new JMenu();
    添加   file.setText("文件");   
    就可以了2、另一个问题,楼上已经说了