如何在SWT DateTime小部件中的DD MM或YYYY字段上设置边距?

时间:2016-03-29 14:06:35

标签: java datetime swt datetimepicker

我正在使用org.eclipse.swt.widgets.DateTime小部件的实例让用户在用户界面中设置日期,但我在结果上正确设置边距和样式时遇到问题。我正在使用SWT.DATESWT.DROP_DOWN并从开箱即用的小部件中获取我需要的所有内容,这很棒,但是如何让输入中的文本具有边距,因此它不会t从输入框的边缘开始?

或者,我可以在某种程度上在DateTime内部合成中设置填充吗?我已经尝试了平常,但只是成功地在Composite本身设置了一个余量......

现在,它看起来像这样:

Image of my current situation, no margin

使用此代码进行实例化:

    (...)
    dateInputComposite = new Composite(parent, SWT.LEFT);

    Label datumLabel = new Label(dateInputComposite, SWT.LEFT);
    dateLabel.setText(DATE_LABEL_TEXT);
    dateLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_FILL));

    DateTime dateInputWidgetWidget = new DateTime(dateInputComposite, SWT.DATE | SWT.DROP_DOWN);
    // create bindings for dateInputWidgetWidget  here

    new DateTime(dateInputComposite, SWT.DATE | SWT.DROP_DOWN);
    GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(3).applyTo(dateInputComposite);

1 个答案:

答案 0 :(得分:2)

如果您不介意使用Nebula's CDateTime,那么您可以定义自己的模式并将其设置为:" dd/MM/yyyy"(注意开头的空格字符)以添加一些人为的边距/填充。

以下是一个例子:

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new FillLayout());

    CDateTime dateTime = new CDateTime(shell, CDT.DATE_MEDIUM | CDT.DROP_DOWN);
    dateTime.setPattern(" dd/MM/yyyy");
    dateTime.setSelection(new Date());

    shell.pack();
    shell.open();

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

看起来像这样:

enter image description here

或者使用" dd / MM / yyyy "

在每个组件周围添加填充

enter image description here