以编程方式更改命令文本

时间:2017-10-12 08:45:01

标签: codenameone

我想以编程方式更改标题栏中的命令文本,但它没有发生。为什么命令名称“aaa”在以下代码中没有变为“bbb”?

labourChargeSumCommand = new Command("") {

    @Override
    public void actionPerformed(ActionEvent evt) {

    }
};
labourChargeSumCommand.setCommandName("aaa");
getToolbar().addCommandToRightBar(labourChargeSumCommand);

cb1.addActionListener(e -> {
    if (cb1.isSelected()) {
        labourChargeSumCommand.setCommandName("bbb");
        getToolbar().revalidate();
    }
});

更新:我的所有代码

public class MyApplication {

    private Form current;
    private Resources theme;
    Command labourChargeSumCommand;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        hi.show();

        labourChargeSumCommand = new Command("") {

            @Override
            public void actionPerformed(ActionEvent evt) {

            }
        };
        labourChargeSumCommand.setCommandName("aaa");
        hi.getToolbar().addCommandToRightBar(labourChargeSumCommand);

        Button bb = new Button("bb");
        bb.addActionListener(e -> {
            if (true) {
                labourChargeSumCommand.setCommandName("bbb");
                System.out.println(labourChargeSumCommand.getCommandName());
                hi.getToolbar().revalidate();
                hi.getToolbar().repaint();
            }
        });
        hi.add(bb);
    }

}

这里我添加了一个btn并将代码保存在其动作监听器中,这就是全部。

2 个答案:

答案 0 :(得分:1)

change command text programatically

I just comment this code //hi.show(); add it at the end. Becuase of this revalidate() not worked, So that labourChargeSumCommand.setCommandName("bbb"); text not updated.

public class MyApplication {

    private Form current;
    private Resources theme;
    Command labourChargeSumCommand;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        //hi.show();
        labourChargeSumCommand = new Command("") {

            @Override
            public void actionPerformed(ActionEvent evt) {

            }
        };
        labourChargeSumCommand.setCommandName("aaa");
        hi.getToolbar().addCommandToRightBar(labourChargeSumCommand);

        Button bb = new Button("bb");
        bb.addActionListener(e -> {
            if (true) {
                labourChargeSumCommand.setCommandName("bbb");
                System.out.println(labourChargeSumCommand.getCommandName());
                hi.getToolbar().revalidate();
                hi.getToolbar().repaint();
            }
        });
        hi.add(bb);
        hi.show();
    }

}

答案 1 :(得分:1)

将命令名称添加到工具栏后设置命令名称不会更改文本。

我所做的是创建一个新命令并查找添加的命令的索引,并将其替换为新命令。

这不是那么有效,也不是最好的方式,但它对我有用。

我们说我们将命令添加到右侧栏及其工具栏容器中的最后一个组件(您可以通过组件检查器找到它的位置)

private void switchCommand(Toolbar t, Command cmd) {
    try {
        int pos = t.getComponentCount() - 1;
        Button cmdButton = new Button(cmd.getCommandName());
        cmdButton.setUIID("TitleCommand");
        cmdButton.setCommand(cmd);
        t.replaceAndWait(t.getComponentAt(pos), cmdButton, null);
        t.getComponentForm().revalidate();
    } catch (Exception ex) {
    }
}

然后我这样做:

labourChargeSumCommand = Command.create("aaa", null, evt -> {});
getToolbar().addCommandToRightBar(labourChargeSumCommand);

cb1.addActionListener(e -> {
    if (cb1.isSelected()) {

        labourChargeSumCommand = Command.create("bbb", null, evt -> {});
        switchCommand(getToolbar(), labourChargeSumCommand);
    }
});


public class MyApplication {

    private Form current;
    private Resources theme;
    Command labourChargeSumCommand;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        hi.show();

        labourChargeSumCommand = Command.create("aaa", null, evt -> {});
        hi.getToolbar().addCommandToRightBar(labourChargeSumCommand);

        Button bb = new Button("bb");
        bb.addActionListener(e -> {
            if (true) {
                labourChargeSumCommand = Command.create("bbb", null, evt -> {});
                switchCommand(getToolbar(), labourChargeSumCommand);
                System.out.println(labourChargeSumCommand.getCommandName());
            }
        });
        hi.add(bb);
    }

}