模糊表单自动填写代码

 

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.text.JTextComponent;

public class text {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JComboBox bar = new JComboBox();
        Component com = bar.getEditor().getEditorComponent();
        final String[] datas = new String[]{"123123", "234123", "345123", "456123", "567123", "678123"};
        final JTextComponent component = (JTextComponent) com;
        final JList p = new JList(datas);
        component.addKeyListener(new KeyAdapter() {

            @Override
            public void keyReleased(KeyEvent e) {
                if (component.getText() == null || component.getText().length() == 0) {
                    return;
                }
                for (int i = 0; i < datas.length; i++) {
                    if (datas[i].toLowerCase().startsWith(component.getText())) {
                        p.setSelectedValue(datas[i], true);
                        String str = component.getText();
                        component.setText(datas[i]);
                        component.setSelectionStart(str.length());
                        component.setSelectionEnd(datas[i].length());
                    }
                }
            }
        });
        bar.setEditable(true);
        frame.getContentPane().add(bar, BorderLayout.NORTH);
        frame.getContentPane().add(new JScrollPane(p), BorderLayout.CENTER);
        frame.setDefaultCloseOperation(3);
        frame.setVisible(true);
    }
}

阅读更多
文章标签: string import class null
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭