如何在 CN1 中编写 iPhone 样式的弹出菜单?

How to code an iPhone style popup menu in CN1?

以前可能已经讲过,但我什么都不会google。制作像附图这样的 iPhone 样式的弹出式选择菜单的最佳方法是什么?我已经尝试使用 Dialog,但我还没有找到一种优雅的方式来添加 Commands,因此它们看起来很好,并且同时触发操作和关闭对话框。 ComponentGroup 不支持单独显示取消条目。

查看此示例:

Form hi = new Form("Pop");
Button pop = new Button("Pop");
pop.addActionListener(e -> {
    Dialog dlg = new Dialog();

    // makes the dialog transparent
    dlg.setDialogUIID("Container");
    dlg.setLayout(BoxLayout.y());

    Command optionACmd = new Command("Option A");
    Command optionBCmd = new Command("Option B");
    Command optionCCmd = new Command("Option C");
    Command cancelCmd = new Command("Cancel");

    dlg.add(
            ComponentGroup.enclose(
                    new Button(optionACmd), 
                    new Button(optionBCmd), 
                    new Button(optionCCmd)
                    )).
            add(ComponentGroup.enclose(new Button(cancelCmd)));

    Command result = dlg.showStretched(BorderLayout.SOUTH, true);
    ToastBar.showMessage("Command " + result.getCommandName(), FontImage.MATERIAL_INFO);
});
hi.add(pop);
hi.show();

结果是:

谢谢Shai!

我把它做成了一个组件,以防有人有类似的需求:

class MyPopupMenu extends Dialog {

    private Command cancelCmd = null;

    MyPopupMenu(boolean includeCancel, Command... commands) {
        this(includeCancel?new Command("Cancel"):null, commands);
    }

    MyPopupMenu(Command cancelOptional, Command... commands) {
        super();
        setDialogUIID("Container");
        setLayout(BoxLayout.y());
        setDisposeWhenPointerOutOfBounds(true); //close if clicking outside menu
        ComponentGroup group = new ComponentGroup();
        for (Command cmd : commands) {
            group.add(new Button(cmd));
        }
        add(group);

        this.cancelCmd = cancelOptional;
        if (cancelCmd != null) {
            add(ComponentGroup.enclose(new Button(cancelCmd)));
        }


    /**
     * show the menu and execute the selected Command, 
     * or do nothing if Cancel is selected
     */
    public void popup() {
        Command choice = showStretched(BorderLayout.SOUTH, true);
        if (choice != null && choice != cancelCmd) {
            choice.actionPerformed(null);
        }

    }
}

太棒了,谢谢大家。为什么我的按钮看起来这么小?试图找出需要更改哪种样式以增加高度。更改按钮填充似乎没有任何改变。我使用商业主题作为起点。