📄 mydialog2.java
字号:
package cn.com.chengang.jface.dialog;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MyDialog2 extends Dialog {
private String textValue; // 用来保存Text值的变量
private Text text;// 将文本写为类实例变量,否则其他方法无法访问它
public MyDialog2(Shell parentShell) {
super(parentShell);
}
public String getTextValue() {
return this.textValue;
}
public void setTextValue(String value) {
this.textValue = value;
}
// 在这个方法里构建Dialog中的界面内容
protected Control createDialogArea(Composite parent) {
Composite topComp = new Composite(parent, SWT.NONE);
topComp.setLayout(new RowLayout());
new Label(topComp, SWT.NONE).setText("请输入:");
text = new Text(topComp, SWT.BORDER);
// 把textValue设给Text作为初值,这时要注意对textValue作空值判断,给文本框设置空值是会出错的
text.setText(textValue == null ? "" : textValue);
text.setLayoutData(new RowData(100, -1));
return topComp;
}
// 单击对话框底部按钮会执行此方法,参数buttonId是被单击按钮的ID值。
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID)// 如果单击确定按钮,则将值保存到变量
textValue = text.getText();
super.buttonPressed(buttonId);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -