Java TextField 问题

发布于 2024-11-19 09:54:54 字数 349 浏览 2 评论 0原文

我正在尝试构建一个接受长度(例如飞机机翼的跨度)的自定义文本字段类。我可以设置默认单位系统,例如“英寸”、“英尺”、“米”等,但我也希望能够输入默认单位系统中没有的长度。

例如,如果我的默认单位系统是“米”,我希望能够在文本字段中输入“10.8 英尺”,然后从英尺转换为米。

有谁知道是否有此类编码的示例?我搜索并找到了一个只接受数字的文本字段(在 NumericTextField),但这不适合我的需要,因为我想输入“10 ft”或“8.5 m”。

I am trying to build a custom text field class that accepts lengths (the span of aircraft wings, for example). I can set default unit system such as "inches", "feet", "meters" etc. but I would also like to be able to input lengths that are not in the default unit system.

So for example, if my default unit system is "meters", I would like to be able to input "10.8 ft" in my text field and then convert from ft to meters.

Does anyone know if there is an example of this type of coding? I have searched and found a text field that only accepts numerics (in NumericTextField), but this does not suit my needs because I would like to input "10 ft" or "8.5 m".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫瑟鸿黎 2024-11-26 09:54:54

这是一个解决方案:

public class MyCustomField extends JPanel
{
    public static final int METER = 1;
    public static final int FEET = 2;
    private int unit_index;
    public JTextField txt;
    public JLabel label;
    public MyCustomField(int size, int unit_index)
    {
        this.unit_index = unit_index;
        txt = new JTextField(size);
        ((AbstractDocument)txt.getDocument()).setDocumentFilter(new MyFilter());
        switch(unit_index)
        {
            case METER:
            label = new JLabel("m");
            break;

            case FEET:
            label = new JLabel("ft");
            break;

            default:
            label = new JLabel("m");
            break;
        }
        add(txt);
        add(label);
    }
    private class MyFilter extends DocumentFilter
    {
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
        {
            StringBuilder sb = new StringBuilder();
            sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
            sb.insert(offset, text);
            if(!containsOnlyNumbers(sb.toString())) return;
            fb.insertString(offset, text, attr);
        }
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
        {
            StringBuilder sb = new StringBuilder();
            sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
            sb.replace(offset, offset + length, text);
            if(!containsOnlyNumbers(sb.toString())) return;
            fb.replace(offset, length, text, attr);
        }
        private boolean containsOnlyNumbers(String text)
        {
            Pattern pattern = Pattern.compile("\\d*\\.?\\d*");
            Matcher matcher = pattern.matcher(text);
            return matcher.matches();
        }
    }
}

我很快就做到了。如果需要,您可以通过添加更多方法和单元来改进它。

Here is a solution:

public class MyCustomField extends JPanel
{
    public static final int METER = 1;
    public static final int FEET = 2;
    private int unit_index;
    public JTextField txt;
    public JLabel label;
    public MyCustomField(int size, int unit_index)
    {
        this.unit_index = unit_index;
        txt = new JTextField(size);
        ((AbstractDocument)txt.getDocument()).setDocumentFilter(new MyFilter());
        switch(unit_index)
        {
            case METER:
            label = new JLabel("m");
            break;

            case FEET:
            label = new JLabel("ft");
            break;

            default:
            label = new JLabel("m");
            break;
        }
        add(txt);
        add(label);
    }
    private class MyFilter extends DocumentFilter
    {
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
        {
            StringBuilder sb = new StringBuilder();
            sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
            sb.insert(offset, text);
            if(!containsOnlyNumbers(sb.toString())) return;
            fb.insertString(offset, text, attr);
        }
        public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
        {
            StringBuilder sb = new StringBuilder();
            sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
            sb.replace(offset, offset + length, text);
            if(!containsOnlyNumbers(sb.toString())) return;
            fb.replace(offset, length, text, attr);
        }
        private boolean containsOnlyNumbers(String text)
        {
            Pattern pattern = Pattern.compile("\\d*\\.?\\d*");
            Matcher matcher = pattern.matcher(text);
            return matcher.matches();
        }
    }
}

I made this qucikly. You can improve it by adding more methods and units if you need.

一世旳自豪 2024-11-26 09:54:54

JScience provides support for strongly typed physical quantities and operations in org.jscience.physics.amount.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文