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

} 
相关问题