gpt4 book ai didi

java - 如何验证 swt 文本小部件是十进制值

转载 作者:行者123 更新时间:2023-12-04 06:13:18 25 4
gpt4 key购买 nike

在 swt 中,文本小部件允许任何字符串。
但是在其中输入 Decimal 值的最合适的 SWT 小部件是什么?

我找到了两个答案:

  • 首先,实现VerifyKeyListener 和VerifyListener,使用法式十进制表示法,但实现简单易行:

  • package test.actions;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import org.eclipse.swt.custom.VerifyKeyListener;
    import org.eclipse.swt.events.VerifyEvent;
    import org.eclipse.swt.events.VerifyListener;
    import org.eclipse.swt.widgets.Text;

    public final class AmountVerifyKeyListener implements VerifyListener, VerifyKeyListener {

    private static final String REGEX = "^[-+]?[0-9]*[,]?[0-9]{0,2}+$";

    private static final Pattern pattern = Pattern.compile(REGEX);

    public void verifyText(VerifyEvent verifyevent) {
    verify(verifyevent);
    }

    public void verifyKey(VerifyEvent verifyevent) {
    verify(verifyevent);
    }

    private void verify (VerifyEvent e) {
    String string = e.text;
    char[] chars = new char[string.length()];
    string.getChars(0, chars.length, chars, 0);

    Text text = (Text)e.getSource();

    if ( ( ",".equals(string) || ".".equals(string) ) && text.getText().indexOf(',') >= 0 ) {
    e.doit = false;
    return;
    }

    for (int i = 0; i < chars.length; i++) {
    if (!(('0' <= chars[i] && chars[i] <= '9') || chars[i] == '.' || chars[i] == ',' || chars[i] == '-')) {
    e.doit = false;
    return;
    }


    if ( chars[i] == '.' ) {
    chars[i] = ',';
    }
    }

    e.text = new String(chars);

    final String oldS = text.getText();
    String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
    Matcher matcher = pattern.matcher(newS);
    if ( !matcher.matches() ) {
    e.doit = false;
    return;
    }

    }
    }

    以及与 verifyKeyListener 关联的主类:

    package test.actions;

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;

    public class TestMain {

    public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));

    final Text text = new Text(shell, SWT.NONE);

    text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    text.addVerifyListener(new AmountVerifyKeyListener() ) ;

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
    }

    }
  • 使用来自星云项目的 FormattedText:http://eclipse.org/nebula/

  • 有人看到另一种解决方案吗?

    最佳答案

    一种更简洁更简单的方法是使用 Double.parse String() 并捕获 NumberFormatException 以确定其中的文本是否可以转换为 double 值。

    关于java - 如何验证 swt 文本小部件是十进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7535163/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com