JMenu - 子菜单不会显示

时间:2015-08-02 04:12:49

标签: java submenu jmenu jmenuitem

我刚开始使用JMenu。单击按钮时,我希望菜单显示子菜单类别,如Dropbox。

所以我的问题是,"为什么子菜单不可见?"

请在下面查看相关代码。

    // Frame title
    frame = new JFrame(NAME);
    frame.setSize(350, 250);
    frame.setVisible(true);

    // Add menu bar
    JMenuBar mb = new JMenuBar();


    // 1.0 Site Menu, S
    JMenu siteMenu = new JMenu("Site");
    siteMenu.setMnemonic(KeyEvent.VK_S);
    mb.add(siteMenu);

    // 1.1 Site->Website, W
    JMenuItem websiteMenu = new JMenuItem("Website", KeyEvent.VK_W);
    mb.add(websiteMenu);

    // 1.2 Site->Forum, F
    JMenuItem forumMenu = new JMenuItem("Forum", KeyEvent.VK_F);
    mb.add(forumMenu);

    // 2.0 Gallery Menu, G
    JMenu galleryMenu = new JMenu("Gallery");
    galleryMenu.setMnemonic(KeyEvent.VK_G);
    mb.add(galleryMenu);

    // 2.1 Gallery->Custom Skins, C
    JMenuItem skinsMenu = new JMenuItem("Custom Skins", KeyEvent.VK_C);
    mb.add(skinsMenu);

    // 3.0 Screenshot Menu, S
    JMenu screenshotMenu = new JMenu("Screenshot");
    screenshotMenu.setMnemonic(KeyEvent.VK_S);
    mb.add(screenshotMenu);


    // Sets the menubar for this frame
    frame.setJMenuBar(mb);

我试图查找问题,但我似乎无法找到它..

1 个答案:

答案 0 :(得分:0)

您正在将JMenuBar个对象直接添加到JMenu。请尝试将它们添加到// 1.0 Site Menu, S JMenu siteMenu = new JMenu("Site"); siteMenu.setMnemonic(KeyEvent.VK_S); mb.add(siteMenu); // 1.1 Site->Website, W JMenuItem websiteMenu = new JMenuItem("Website", KeyEvent.VK_W); siteMenu.add(websiteMenu); // 1.2 Site->Forum, F JMenuItem forumMenu = new JMenuItem("Forum", KeyEvent.VK_F); siteMenu.add(forumMenu); // and so on 对象中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
      CustomCellEventsWithCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

     if (indexPath.row == [events count]) {

         // here you can call the method that appends data to the mutable array
     }

      return cell;
}