简体   繁体   English

同时在Linux / Gnome中将文本右对齐并包装在SWT标签中

[英]Simultaneously right-align and wrap text in an SWT label in Linux/Gnome

I have a problem which appears when using Ubuntu Linux/Gnome, but seemingly not on Windows (as seen in one of the answers), that I can't use SWT.RIGHT | SWT.WRAP 我有一个使用Ubuntu Linux / Gnome时出现的问题,但似乎不在Windows上(如答案之一所示),我无法使用SWT.RIGHT | SWT.WRAP SWT.RIGHT | SWT.WRAP together, when creating a new label, in order to both make the text right-aligned and wrappable. SWT.RIGHT | SWT.WRAP在创建新标签时一起使用,以使文本右对齐和可包装。

Background: 背景:

I'm using a simple 2-column Grid layout in a wizard, to create a dynamic number of rows with one label and one textfield per row. 我在向导中使用了一个简单的2列网格布局,以创建动态数量的行,每行带有一个标签和一个文本字段。 I want the labels to be of fixed size and with the text inside it right-aligned, like so: 我希望标签的大小固定,并且其中的文本右对齐,如下所示:

      label 1: [Text field 1]
Another label: [Text field 2]
 Another label
  with wrapped [Text field 3]
         text: 

The gridlayout is created like so: 网格布局的创建如下:

GridLayout gl = new GridLayout();
gl.numColumns = 2;
composite.setLayout(gl);

and this is how I try to create the rows (with one label and one textfield per row): 这就是我尝试创建行(每行具有一个标签和一个文本字段)的方式:

for (String labelText : labelTexts) {
    Label fieldLabel = new Label(this.composite, SWT.RIGHT | SWT.WRAP | SWT.BORDER );
    fieldLabel.setText(labelText);
    GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
    labelGridData.widthHint = 160;
    fieldLabel.setLayoutData(labelGridData);

    Text textField = new Text(this.composite, SWT.BORDER);
    GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
    textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
    textGridData.widthHint = 248;
    textField.setLayoutData(textGridData);
}

The problem is this part: SWT.RIGHT | SWT.WRAP 问题是这部分: SWT.RIGHT | SWT.WRAP SWT.RIGHT | SWT.WRAP above, since it does not work to use them both simultaneously. 上面的SWT.RIGHT | SWT.WRAP ,因为不能同时使用它们。 So how can I get around that, and both right-align the text in the labels, and make the text wrap? 那么如何解决这个问题,又使标签中的文本右对齐,然后使文本自动换行?

I tried to recreate the problem on my Vista machine and using the eclipse 3.6. 我试图在Vista计算机上并使用eclipse 3.6重新创建该问题。 But it worked for me. 但这对我有用。

Here is the output: 这是输出:

在此处输入图片说明

The code I have used is as follows: 我使用的代码如下:

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class RightAlign 
{
    public static void main(String[] args)
    {

        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        shell.setLayout(layout);
        shell.setText("Alignment Test");

        String[] labelTexts = new String[]{"label 1:","Another label:","Another label with wrapped text:"};

        for (String labelText : labelTexts) 
        {
            Label  fieldLabel = new Label(shell,  SWT.RIGHT |SWT.WRAP | SWT.BORDER );
            fieldLabel.setText(labelText);
            GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
            labelGridData.widthHint = 100;
            fieldLabel.setLayoutData(labelGridData);

            Text textField = new Text(shell, SWT.BORDER);
            GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
            textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
            textGridData.widthHint = 248;
            textField.setLayoutData(textGridData);
        }

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();

    }
}

The only thing I changed is the labelGridData.widthHint from 160 to 100 . 我唯一更改的是从160100labelGridData.widthHint The longest label was < 160 and hence was not getting wrapped. 最长的标签是< 160 ,因此没有被包裹。

Short answer: 简短答案:

Use the StyledText instead of the Label - all things equal. 使用StyledText代替Label-所有事物都相等。

rightAlign

Detailed answer: 详细答案:

There are a number of bugs ( 120256 being a good entry point) in GTK about this "feature" most of them marked duplicate of each other, most of them harking back to gtk+ 2.4/2.6 some marked fixed. 关于此“功能”,GTK中存在许多错误( 120256是一个不错的入口点),其中大多数标记为彼此重复,大多数回溯到gtk + 2.4 / 2.6,有些标记为已修复。 However, in 2011 gtk2+ version 2.22 (for Maverick Market), bugs seem to be remarkably resilient. 但是,在2011年gtk2 +版本2.22(针对Maverick Market)中,错误似乎具有显着的弹性。

Having read this bug report, I grepped the whole SWT source tree for the wordwrap keyword and it looks like Steve Northover boys have worked around the bug much better with the StyledText Control than with the Label Control. 阅读了该错误报告后,我为wordwrap关键字弄乱了整个SWT源代码树,看起来Steve Northover的男孩们通过StyledText控件比使用Label控件更好地解决了该错误。

I reused Favonius test case (thx a lot Favonius ;-), and that apparently was good omen indeed. 我重用了Favonius测试用例(很多Favonius ;-),这显然是个好兆头。

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class RightAlign2
{
    public static void main(String[] args)
    {

        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        shell.setLayout(layout);
        shell.setText("Alignment Test");

        String[] labelTexts = new String[]{"label 1:","Another label:","Another label with wrapped text:"};

        for (String labelText : labelTexts)
        {
            StyledText  fieldText = new StyledText(shell, SWT.RIGHT |SWT.WRAP | SWT.BORDER );
            fieldText.setText(labelText);
            GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
            labelGridData.widthHint = 100;
            fieldText.setLayoutData(labelGridData);

            Text textField = new Text(shell, SWT.BORDER);
            GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
            textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
            textGridData.widthHint = 248;
            textField.setLayoutData(textGridData);
        }

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM