当前位置: 首页 > 知识库问答 >
问题:

java中的预测文本[复制]

吴丁雷
2023-03-14

可能重复:
在文本字段内键入时如何列出建议

在JavaSwing中,有没有办法在JTextField中获取预测文本?比如,如果我想从用户那里得到城市的名称,那么它应该预测城市。

共有3个答案

高溪叠
2023-03-14

以下是一种可能的实现:

public class Predict
{
    private final static String [] COLORS = new String [] {"red", "orange", "yellow", "green", "cyan", "blue", "violet"};

    public static void main (String [] args)
    {
        final JTextField field = new JTextField ();

        field.getDocument ().addDocumentListener (new DocumentListener()
        {
            @Override
            public void removeUpdate (DocumentEvent e)
            {
                // Do nothing
            }

            @Override
            public void insertUpdate (DocumentEvent e)
            {
                if (e.getOffset () + e.getLength () == e.getDocument ().getLength ())
                    SwingUtilities.invokeLater (new Runnable()
                    {
                        @Override
                        public void run ()
                        {
                            predict (field);
                        }
                    });
            }

            @Override
            public void changedUpdate (DocumentEvent e)
            {
                // Do nothing
            }
        });

        JFrame frame = new JFrame ("Auto complete");
        Container contentPane = frame.getContentPane ();
        contentPane.setLayout (new BorderLayout ());
        contentPane.add (field, BorderLayout.CENTER);
        frame.pack ();
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setVisible (true);
    }

    private static void predict (JTextField field)
    {
        String text = field.getText ();

        String prediction = null;

        for (String color: COLORS)
        {
            if (color.startsWith (text) && !color.equals (text))
            {
                if (prediction != null) return;

                prediction = color;
            }
        }

        if (prediction != null)
        {
            field.setText (prediction);

            field.setCaretPosition (text.length ());
            field.select (text.length (), prediction.length ());
        }
    }
}
刘丰羽
2023-03-14

我的一些程序中有一个自动完成。不幸的是,我在网上找不到这些信息的来源。我发布了我的代码,但我不是这个自动完成文档类的“原始”作者。如果你在互联网上找到它,给我链接,这样就可以把信用给原作者。

public class AutoCompleteDocument extends PlainDocument {

    private final List<String> dictionary = new ArrayList<String>(); 
    private final JTextComponent jTextField;

    public AutoCompleteDocument(JTextComponent field, String[] aDictionary) {
        jTextField = field;
        dictionary.addAll(Arrays.asList(aDictionary));
    }

    public void addDictionaryEntry(String item) {
        dictionary.add(item);
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a)
            throws BadLocationException {
        super.insertString(offs, str, a);
        String word = autoComplete(getText(0, getLength()));
        if (word != null) {
            super.insertString(offs + str.length(), word, a);
            jTextField.setCaretPosition(offs + str.length());
            jTextField.moveCaretPosition(getLength());
        }
    }

    public String autoComplete(String text) {
        for (Iterator<String> i = dictionary.iterator(); i.hasNext();) {
            String word = i.next();
            if (word.startsWith(text)) {
                return word.substring(text.length());
            }
        }
        return null;
    }  
}

然后使用它,只需用类似的东西初始化它

AutoCompleteDocument autoCompleteDoc;

autoCompleteDoc = new AutoCompleteDocument(aJTextField, anArray);
aJTextField.setDocument(autoCompleteDoc);

希望会有所帮助

裘光启
2023-03-14

SwingX提供自动完成功能:http://swingx.java.net/

 类似资料:
  • 本文向大家介绍解释Java MongoDB的预测,包括了解释Java MongoDB的预测的使用技巧和注意事项,需要的朋友参考一下 从MongoDb集合中检索数据时,您只能使用投影选择所需的数据。在Java中,您可以在使用projection()方法从集合中读取文档时投影必要的数据。在的结果上调用此方法,绕过所需的文件名的名称,如下所示: 示例 以下Java示例从集合中读取文档,使用投影,我们仅显

  • 1. 中文文本挖掘预处理特点 首先我们看看中文文本挖掘预处理和英文文本挖掘预处理相比的一些特殊点。 首先,中文文本是没有像英文的单词空格那样隔开的,因此不能直接像英文一样可以直接用最简单的空格和标点符号完成分词。所以一般我们需要用分词算法来完成分词,在文本挖掘的分词原理中,我们已经讲到了中文的分词原理,这里就不多说。 第二,中文的编码不是utf8,而是unicode。这样会导致在分词的时候,和英文

  • 我正在设计一个配置托盘的应用程序,我有一个扩展的类,我用它创建带有旋转文本的JLabels。我在网上找到了几个关于如何这样做的例子,我的旋转工作很好,它不是完美的,但这是一个正在进行的工作。 我现在遇到的问题是我旋转的JLabels中的文本重复了,我不知道为什么。下面是一张图片,显示了每个标签中的重复文本,它在一些地方比在其他地方更突出,例如与高度标签的重复可以清楚地看到。 下面是我的类的源代码,

  • 句子分割text_to_word_sequence keras.preprocessing.text.text_to_word_sequence(text, filters='!"#$%&()*+,-./:;<=>?@[\]^_`{|}~\t\n',

  • 句子分割text_to_word_sequence keras.preprocessing.text.text_to_word_sequence(text, filters=base_filter(), lower=True, split=" ") 本函数将一个句子拆分成单词构成的列表 参数 text:字符串,待处理的文本 filters:需要滤除的字符的列表或连接形成的字符串,例如标

  • 问题内容: 我有一个带有二进制数据的字符串(1110100),我想取出文本以便可以打印它(1110100将打印“ t”)。我尝试了这一点,它类似于我用来将文本转换为二进制的东西,但是根本不起作用: 任何更正或建议将不胜感激。 问题答案: 你可以使用基数2(二进制)将二进制字符串转换为整数: 然后,如果你希望将相应的字符作为字符串: