我正在尝试在 SWT 的文本字段中显示历史记录。
历史记录会生成一个新的 shell,其中有两个 Composites。第一个显示标题部分,其中包含标签“历史记录”和下方的水平线。第二个是页脚部分,显示实际数据。这是可行的,但水平线不会在整个可用水平空间中扩展(即:“填充”)(即:直到复合“结束”)。
public void mouseDown(final MouseEvent e) {
findSegmentText.setText("");
if(0 == lastSegmentIds.size()) return;
if(disableToolTipsButton.getSelection()) return; // context help is not desired
if(null != popupShell) popupShell.dispose();
popupShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.ON_TOP | SWT.MODELESS);
//popupShell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
popupShell.setLayout(createNoMarginLayout(1, false));
/* Header */
compositeSearchSegments1 = new Composite(popupShell, SWT.NONE);
compositeSearchSegments1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
compositeSearchSegments1.setLayout(createNoMarginLayout(1, false));
/* Footer */
compositeSearchSegments2 = new Composite(popupShell, SWT.NONE);
compositeSearchSegments2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
compositeSearchSegments2.setLayout(createNoMarginLayout(2, false));
Label history = new Label(compositeSearchSegments1, SWT.NONE);
history.setText("History");
Label line1 = new Label(compositeSearchSegments1, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
for(int i = lastSegmentIds.size()-1 ; i>=0;i--) { // iterate backwards, higher indexes have newer selected links
final String lastSegmentId = lastSegmentIds.get(i);
l.setText(lastSegmentId);
Button b = new Button(compositeSearchSegments2, SWT.NONE);
b.setVisible(true);
b.setText(">>");
b.setSize(10,10);
b.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
findSegmentText.setText(lastSegmentId);
searchAndDisplayLink(Long.decode(findSegmentText.getText()));
}
});
}
Point point = optionsComposite.toDisplay(e.x + optionsComposite.getBounds().x +findSegmentText.getLocation().x, e.y + optionsComposite.getBounds().y+findSegmentText.getLocation().y );
popupShell.setLocation(point.x-125, point.y-10);
popupShell.pack();
popupShell.open();
findSegmentText.setFocus();
}
Run Code Online (Sandbox Code Playgroud)
产生的结果是:

我想扩展该线以填充可用的水平空间。我应该怎么办?谢谢。
由于您使用的是,因此在分隔符中GridLayout添加 a可以解决问题:GridDataLabel
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Label first = new Label(shell, SWT.NONE);
first.setText("short");
Label separator = new Label(shell, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
separator.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Label second = new Label(shell, SWT.NONE);
second.setText("looooooooooooooooooooooooooooooooooooong");
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
Run Code Online (Sandbox Code Playgroud)
看起来像这样:

| 归档时间: |
|
| 查看次数: |
1541 次 |
| 最近记录: |