我想以編程方式更改標題欄中命令的文本,但不會發生。爲什麼在下面的代碼中命令名稱「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