我想以编程方式更改标题栏中命令的文本,但不会发生。为什么在下面的代码中命令名称“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,并保持其作用监听器里的代码,这就是全部。
首先调试** cb1.isSelected()**返回** true ** – 2017-10-12 08:49:50
yeahh,它返回true。我想要做的是当我选择复选框时,标题栏中的命令应该改变。它适用于setTitle()但不在这里 –
尝试调用getToolbar()。repaint();在getToolbar()。revalidate();之后 – 2017-10-12 09:06:50