【问题标题】:Toolbar overlapping with Label of a component工具栏与组件的标签重叠
【发布时间】:2021-07-05 21:26:06
【问题描述】:

我已启用并设置表单的工具栏。表单有一个带有标签集的 TextArea 组件,用作标题。导航到该屏幕时不显示标签。我认为它是重叠的。是这样吗?我错过了什么? 工具栏的全局设置设置为 false。

    Form aFrm = new Form("aTitle", new BoxLayout(BoxLayout.Y_AXIS));
    //Set up the toolbar using a static utility method.
    GGUtil.setTheAppToolbar(aFrm);        
    //Create a TextArea to display the information from a Resource file.
    TextArea aTxtArea = new TextArea(GGUtil.openResourceFileToString(null, "/atextfile.txt"));
    //Signal virtual keyboard whether editing is allowed.
    aTxtArea.setConstraint(TextArea.UNEDITABLE);
    //Disable editing.
    aTxtArea.setEditable(false);
    //Grow in rows to display the content
    aTxtArea.setGrowByContent(true);
    //Create and set the text of the Label
    Label txtAreaLabel = new Label("labelTitle");
    //Set the Label for the TextArea
    aTxtArea.setLabelForComponent(txtAreaLabel);
    //Add the TextArea to the Form.
    aFrm.add(aTxtArea);
    ...

遵循设置工具栏的静态实用方法的代码。

    //Create a Toolbar obj for this Form.
    Toolbar appToolbar = new Toolbar();
    //Enable Toolbar for this Form only, setting the tile at the moment.
    aForm.setToolbar(appToolbar);
    //Set the title of the Toolbar.
    appToolbar.setTitle("Toolbar Title");
    //Create Container to hold "sandwich" menu options.
    Form sandwichMenu = new Form("Settings", new BoxLayout(BoxLayout.Y_AXIS));
    //Center the Toolbar
    appToolbar.setTitleCentered(true);
    //Create and add a "Close" button using another static utility method.
    GGUtil.addCloseRightSettings(appToolbar, sandwichMenu, "Close");
    //Add the "Settings" Form in the "sandwich" menu option.
    appToolbar.addComponentToRightSideMenu(sandwichMenu);
    ...

Simulator screenshot

【问题讨论】:

  • 你能在问题中添加一个最小的示例代码,重现问题吗?可以加个截图吗?
  • @FrancescoGalgani 请立即查看。
  • 我刚刚添加了一个答案

标签: codenameone


【解决方案1】:

setLabelForComponent 方法并非旨在为 TextArea 添加标题。我不确定它的实际用处是什么,但是,看看它的实现(在这里找到:https://github.com/codenameone/CodenameOne/blob/master/CodenameOne/src/com/codename1/ui/Component.java),它的使用似乎仅限于涉及代码的东西(据我所知,代码只有在标签上才有意义和按钮)。老实说,我从未使用过setLabelForComponent

如果您想在 TextArea 上方添加带有标题功能的文本,只需将其添加到带有 aFrom.add(txtAreaLabel) 的 TextArea 之前的表单中,并使用 CSS 设置样式。

【讨论】:

  • 好吧,我正在阅读 Codename One Javadoc,setLabelForComponent 的描述是:“允许我们指示与此组件关联的标签,从而提供与此组件相关的视觉反馈,例如,当组件接收到时启动代码重点。”无论如何,我将放弃这种方法,因为它似乎注定要用于其他用途,尽管有冲突。谢谢。
  • 顺便说一下,表单已经设置了标题,因为表单构造函数中传递了一个字符串参数。怎么也显示不出来?我们不得不求助于 Label 解决方案,因为这也不起作用......
  • 这是一种类似于 Swing 中的方法,例如如果你在一个表格中,我们不能开始标记所有的标签,因为它看起来很奇怪而且很吵。这对于可访问性也很重要,例如当您关注文本区域时,您希望屏幕阅读器读取该标签。但是,我们不能自己将它添加到表单中,因为我们不知道布局,也不知道您可能想要它在哪里(左、上、下等)。所以这只是对 GUI 的提示
  • 亲爱的@ShaiAlmog,感谢您用Form 标题澄清问题。
  • 太棒了。仅供参考,如果答案是正确的,您应该接受它,这会给您和 Francesco 加分。这些有助于在堆栈溢出时做更多的事情......
猜你喜欢
  • 1970-01-01
  • 2011-02-20
  • 2016-03-03
  • 2016-08-11
  • 2017-02-21
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
  • 2017-03-24
相关资源
最近更新 更多