1

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

[Bonus] 另外,我不知道为什么“add(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);
    }

}
4

1 回答 1

3

添加

MainWindow.setJMenuBar(MenuBar);

在最后

于 2016-02-06T22:35:51.743 回答