2017-10-12 61 views
3

我想以編程方式更改標題欄中命令的文本,但不會發生。爲什麼在下面的代碼中命令名稱「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,並保持其作用監聽器裏的代碼,這就是全部。

+0

首先調試** cb1.isSelected()**返回** true ** – 2017-10-12 08:49:50

+0

yeahh,它返回true。我想要做的是當我選擇複選框時,標題欄中的命令應該改變。它適用於setTitle()但不在這裏 –

+0

嘗試調用getToolbar()。repaint();在getToolbar()。revalidate();之後 – 2017-10-12 09:06:50

回答

1

改變命令文本編程

我只是評論這個代碼//hi.show();在最後加上它。因此 重新驗證()沒有工作,所以,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(); 
    } 

} 
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); 
    } 

} 
相關問題