在這段代碼中,我絕對不知道爲什麼我的菜單欄不起作用,對於這裏的每個人都可能看起來微不足道,但我會非常感激,如果有人能告訴我爲什麼我的菜單欄不顯示。沒有理由菜單欄不工作?
[獎金]另外,我不知道爲什麼「添加(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);
}
}
加(Canvas)的添加
當畫布是一個'Canvas'沒有工作,因爲它是一個重量級* * AWT組件,而不是一個輕量級擺動組件。總之,除了'Container'外,你不應該使用java.awt中擴展'Component'的類。 – ControlAltDel