我的MenuBar为什么不显示?

时间:2016-05-27 05:42:55

标签: java codenameone mobile-application

我有一个简单的测试用例,包含两个表单和一个侧面菜单。当我显示第一个表单时,侧面菜单丢失。在我转到向导表单并返回后,侧面菜单显示应该如此。但是,当首次启动应用程序时,我无法显示侧边菜单。

我应该注意,开发人员指南中的MenuBar组件没有任何内容。如果还有另一种方法我应该使用它,那么MenuBar javadoc应该这样说。

import com.codename1.ui.*;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.ui.plaf.UIManager;

public class Playground {

  private Form current;
  private MainForm mainForm;
  public void init(Object context) {
    UIManager.initFirstTheme("/theme");
    Toolbar.setGlobalToolbar(true); // Didn't help.
    mainForm = new MainForm();
  }

  public void start() {
    if (current != null) {
      current.show();
      return;
    }

    Form hi = mainForm;
    hi.show();
    current = hi;
  }

  public void stop() {
  }

  public void destroy() {
  }

  private class MainForm extends Form {
    MainForm() {
      super("Test of Hamburger Menu");
      setLayout(new BoxLayout(BoxLayout.Y_AXIS));
      Button spacer = new Button("Wizard");
      spacer.addActionListener((evt)-> showWizard());
      add(spacer);
      Label normalLabel = new Label("Normal text");
      add(normalLabel);

      Toolbar toolbar = new Toolbar();
      setToolbar(toolbar); // I apparently need to do this or set GlobalToolbar to true, or the menu icon won't appear at all.
//      MenuBar menubar = toolbar.getMenuBar(); // Didn't help.
      MenuBar menubar = getMenuBar();
      Command dummyOne = new Command("One");
      Command dummyTwo = new Command("Two");
      menubar.addCommand(dummyOne);
      menubar.addCommand(dummyTwo);
    }
  }

  private void showWizard() {
    Form wizardForm = new Form("Wizard");
    wizardForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    Button done = new Button("Done");
    done.addActionListener((evt)-> backToMain());
    wizardForm.add(done);
    wizardForm.show();
  }

  private void backToMain() {
    mainForm.show();
  }

}

1 个答案:

答案 0 :(得分:1)

您应该使用工具栏添加命令。

toolbar.addCommandToSideMenu(dummyOne);