我有一个扩展 jface.dialogs.Dialog
的类。在该对话框中有一个保存按钮。当用户按下那个按钮时,我需要从一些 swt.widgets.Text
字段中读取值,但是文本字段已经被处理掉了。
Tôi đã làm gì sai?
public class MyNewDialog extends Dialog {
private Text txt;
public MyNewDialog(Shell parentShell) {
super(parentShell);
}
@Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(new GridLayout(2, false));
txt = new Text(container, SWT.BORDER);
txt.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
return container
}
@Override
protected void createButtonsForButtonBar(Composite parent) {
Button saveButton = createButton(parent, IDialogConstants.OK_ID, "Save", true);
saveButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent p_e) {
String string = txt.getText() //widget is disposed exception
}
}
}
由于您正在为按钮使用 IDialogConstants.OK_ID
,因此您可以使用 okPressed()
方法。无需添加特定的监听器。
@Override
protected void okPressed()
{
value = txt.getText();
super.okPressed();
}
然后创建一个getter方法方法来返回giá trị
变量:
public String getValue()
{
giá trị trả về;
}
Tôi là một lập trình viên xuất sắc, rất giỏi!