6
點擊按鈕後,我需要替換網格單元格的內容。例如:有標籤,我需要用文本替換它。 GridLayout有可能嗎?我需要使用SWT。如何替換SWT GridLayout中的網格單元格的內容?
點擊按鈕後,我需要替換網格單元格的內容。例如:有標籤,我需要用文本替換它。 GridLayout有可能嗎?我需要使用SWT。如何替換SWT GridLayout中的網格單元格的內容?
您可以簡單地處置Label
並將新的Text
代替。 GridLayout
使用兒童的z順序來確定網格中的位置,因此您需要使用Text
上的moveAbove()
和moveBelow()
以使其位於正確的位置。然後在父母上撥打layout()
。例如:
Text text = new Text(label.getParent(), SWT.BORDER);
text.moveAbove(label);
label.dispose();
text.getParent().layout();
下面是一個簡單的小工具,說明正是我的意思:
public class ReplaceWidgetComposite
extends Composite
{
private Label label;
private Text text;
private Button button;
public ReplaceWidgetComposite(Composite parent, int style)
{
super(parent, style);
setLayout(new GridLayout(1, false));
label = new Label(this, SWT.NONE);
label.setText("This is a label!");
button = new Button(this, SWT.PUSH);
button.setText("Press me to change");
button.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected(SelectionEvent e)
{
text = new Text(ReplaceWidgetComposite.this, SWT.BORDER);
text.setText("Now it's a text!");
text.moveAbove(label);
label.dispose();
button.dispose();
ReplaceWidgetComposite.this.layout(true);
}
});
}
}
它不能正常工作。此代碼會導致在標籤後插入文本單元格,但我想要替換標籤。 dispose()以某種方式不會從網格中移除標籤,因爲它仍然可見。 – alhcr
處理小部件應將其從其父項中移除。嘗試調用'parent.layout(true)'。 –
它看起來不像這樣工作。我有shell.layout(true);而且我知道它是有效的,因爲插入了文本,我可以看到它,但標籤仍然可見。你在什麼地方使用它,它的工作是否正確? – alhcr