[英]Menu bar not showing in JFrame
我在我的JFrame中添加了一個自定義JPane和一個自定義JMenuBar。 JPane顯示很好,但JMenuBar卻沒有。 我在不同的類中定義了框架和菜單欄
這是框架類:public class pixelFrame {//此框架將保留一個pixelPane用於藝術創作。 這還將包含在另一個文件中定義的菜單欄。 pixelPane editArea; pixelMenuBar menuBar; public pixelFrame(){EventQueue.invokeLater(new Runnable(){//在運行時創建新線程
@Override
public void run() { //when the program runs, do the following
JFrame frame = new JFrame("Pixel Art Creator"); //create a new JFrame (similar to a regular window), and give it the following title
editingArea = new pixelPane();
menuBar = new pixelMenuBar();
frame.add(editingArea); //add a pixelPane named editingArea to the JFrame
frame.setJMenuBar(menuBar); //adds a menu to the JFrame
frame.pack(); //make the window big enough to fit all components (in this case, editingArea)
frame.setLocationRelativeTo(null); //set window to the center of the screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Stop the thread and terminate the program.
frame.validate();
frame.setVisible(true); //You can see the window.
menuBar.setVisible(true);
}
});
}
public static void main(String[] args)
{
pixelPane.gridEnabled = true; //changing another variable from pixelPane just for testing.
new pixelFrame(); //create an instance of pixelFrame.
}
}
這是菜單欄:
public class pixelMenuBar extends JMenuBar {
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menuBar;
JMenu file, tool;
JMenuItem save, load, changeColor;
JCheckBoxMenuItem gLines;
public pixelMenuBar() {
menuBar = new JMenuBar();
//creates a "File" menu in the menu bar
file= new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
menuBar.add(file); //add the menu to the menu bar
//creates a "Tools" menu in the menu bar
tool = new JMenu("Tools");
tool.setMnemonic(KeyEvent.VK_T);
menuBar.add(tool);
//Menu items that go under the File menu
save = new JMenuItem("Save File"); //save and export the image as an .svg file
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_S)); //a Ctrl+S hotkey. Handy dandy!
file.add(save);//adds the Save button to the File menu
load = new JMenuItem("Load File"); //load the file into BufferedImage, make it into a JLabel and add it to the panel.
load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_L));//a Ctrl+L hotkey. Also handy dandy!
file.add(load);//adds the Load button to the File menu
//Menu items that will go under the Tools menu
gLines = new JCheckBoxMenuItem("Gridlines"); //creates a new checkbox for enabling grid lines. will toggle pixelPane.gridEnabled
gLines.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_G));
tool.add(gLines);//adds the gridlines tool to the Tools menu
tool.addSeparator(); //adds a separating line in the Tools menu. Organization.
changeColor = new JMenuItem("Change Color");//creates a new menu item that will let the user the color of the pixel. Uses a color picker
tool.add(changeColor);//adds Change Color to the Tools menu
}
}
我沒有在UI中正確添加內容嗎? 我再次檢查了我使用的是addJMenuBar()而不是addMenuBar()。
首先,類名應該以大寫字母開頭。 您見過Java API中沒有的類嗎? 遵循Java約定。 通過例子學習。
public class pixelMenuBar extends JMenuBar
{
...
public pixelMenuBar()
{
menuBar = new JMenuBar();
您的類是一個“ JMenuBar”,但是您在該類中要做的第一件事就是創建一個JMenuBar。
不要創建JMenuBar!
只需將菜單項添加到JMenuBar類本身即可:
//menuBar.add(file); //add the menu to the menu bar
add(file); //add the menu to the menu bar
實際上,甚至沒有必要使用PixelMenuBar類,因為您沒有向JMenuBar添加任何新功能。 只需將一個方法添加到您的主類,如createMenuBar(...)
,即可創建JMenuBar並添加JMenu / JMenuItem對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.