📄 inputdialog1.java
字号:
/**
* @作者:陈刚
* @Email:glchengang@yeah.net
* @Blog:http://blog.csdn.net/glchengang
*/
package jface.dialog;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class InputDialog1 {
public static void main(String[] args) {
try {
InputDialog1 window = new InputDialog1();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");
InputDialog dialog = new InputDialog(shell, "标题", "请输入值", "1", new MyValidator());
if (dialog.open() == InputDialog.OK) {
String vlaueStr = dialog.getValue();
System.out.println("输入值:" + vlaueStr);
}
shell.layout();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* 值的验证类
*/
class MyValidator implements IInputValidator {
/**
* 返回null值表示值(newText)合法 返回其它字符符串(包括""这样的空字符)表示值不合法
*/
public String isValid(String newText) {
float value = 0;
try {
value = Float.valueOf(newText).floatValue();
} catch (java.lang.NumberFormatException e) {
return "请输入数值";
}
if (value > 0 && value < 100)
return null;//返回null表示newText合法
else
return "请输入大于0小于100的数";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -