【问题标题】:SWT Label with horizontal line not showing correctly带有水平线的 SWT 标签未正确显示
【发布时间】:2014-03-06 08:04:29
【问题描述】:

我正在尝试在 2 列 GridLayout 中显示标签。我似乎无法同时显示文本和下方的水平线:

public void foo(){
     popupShell = new Shell(Display.getDefault(), SWT.NO_TRIM | SWT.ON_TOP | SWT.MODELESS);
     //popupShell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
     popupShell.setLayout(createNoMarginLayout(2, false));

     Label history  = new Label(popupShell, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
     history.setText("History");
     history.setVisible(true);
     Label fill  = new Label(popupShell, SWT.NONE);
     fill.setSize(0,30);
}

public static GridLayout createNoMarginLayout(int numColumns, boolean makeColumnsEqualWidth) {
     GridLayout layout = new GridLayout(numColumns, makeColumnsEqualWidth);
     layout.verticalSpacing          = 0;
     layout.horizontalSpacing   = 0;
     layout.marginTop           = 0;
     layout.marginBottom             = 0;

     layout.marginLeft               = 0;
     layout.marginRight              = 0;
     layout.marginWidth              = 0;
     layout.marginHeight             = 0;
     return layout;
}

我得到的只是没有文字的那一行。

我做错了什么?

【问题讨论】:

    标签: java swt label


    【解决方案1】:

    具有SWT.SEPARATOR 样式的Label 不显示任何文本值。您必须使用单独的控件来显示文本。

    来自Label 源,表明setTextSWT.SEPARATOR 完全忽略:

    public void setText (String string) {
      checkWidget();
      if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
    
      if ((style & SWT.SEPARATOR) != 0) return;
    
      ...
    

    【讨论】:

    • 谢谢 Greg,我在初始标签上设置了 SWT.NONE,并添加了两个额外的标签,带有 SWT.HORIZONTAL。它现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 2012-03-30
    • 1970-01-01
    相关资源
    最近更新 更多