Codenameone 以编程方式更改命令文本
我想以编程的方式更改标题栏中命令的文本,但没有这样做。为什么在下面的代码中命令名“aaa”没有更改为“bbb”Codenameone 以编程方式更改命令文本,codenameone,Codenameone,我想以编程的方式更改标题栏中命令的文本,但没有这样做。为什么在下面的代码中命令名“aaa”没有更改为“bbb” labourChargeSumCommand = new Command("") { @Override public void actionPerformed(ActionEvent evt) { } }; labourChargeSumCommand.setCommandName("aaa"); getToolbar().addCommandToRigh
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,并将代码保存在其动作侦听器中,仅此而已
以编程方式更改命令文本
我只是对这段代码进行注释//hi.show()将其添加到末尾。因此
revalidate()未工作,因此labourChargeSumCommand.setCommandName(“bbb”)文本未更新
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();
}
}
将命令添加到工具栏后设置命令名不会更改文本 我要做的是创建一个新命令,查找添加命令的索引,并用新命令替换它 这不是很有效,也不是最好的方法,但这是一种适合我的方法 假设我们将命令添加到右栏,并将其作为工具栏容器中的最后一个组件(您可以通过组件检查器找到它的位置): 然后我会这样做:
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);
}
}
首先,debug cb1.isSelected()返回trueyeahh,它返回true。我想做的是,当我选中复选框时,标题栏中的命令应该会更改。它适用于setTitle(),但不适用于此处尝试调用getToolbar().repaint();在getToolbar()之后。重新验证();不,不工作尝试
工具栏.findCommandComponent(命令).setText(…)
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);
}
}