陈建华的博客
专注web开发
java swing 模拟百度搜索提示
2015-09-21 15:40:14   阅读1246次

实现方式如下:


import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
public class Test extends JFrame
{
    public Test() {
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args)
    {
        Test test=new Test();
        test.setSize(450,330);
        test.setLocation((test.getToolkit().getScreenSize().width-450)/2,(test.getToolkit().getScreenSize().height-330)/2);
        test.setDefaultCloseOperation(EXIT_ON_CLOSE);
        test.setVisible(true);
    }
    private void jbInit() throws Exception {
        this.getContentPane().setLayout(null);
        jLabel1.setText("Search:");
        jLabel1.setBounds(new Rectangle(39, 97, 72, 31));
        this.setTitle("百度搜索");
        jButton2.setBounds(new Rectangle(258, 232, 88, 31));
        jButton2.setText("exit");
        jButton2.addActionListener(new Test_jButton2_actionAdapter(this));
        jButton1.setBounds(new Rectangle(70, 232, 91, 32));
        jButton1.setText("confirm");
        jButton1.addActionListener(new Test_jButton1_actionAdapter(this));
        jList1.setBounds(new Rectangle(132, 128, 209, 88));
        jList1.addMouseListener(new Test_jList1_mouseAdapter(this));
        jList1.setVisible(false);
        jTextField1.addMouseListener(new Test_jTextField1_mouseAdapter(this));
        this.getContentPane().add(jButton1);
        this.getContentPane().add(jButton2);
        this.getContentPane().add(jList1);
        this.getContentPane().add(jTextField1);
        this.getContentPane().add(jLabel1);
        jTextField1.setBounds(new Rectangle(132, 95, 210, 33));
        jTextField1.getDocument().addDocumentListener(new Test_jTextField1_actionAdapter(this));
    }
    Set<String> hset=new HashSet<String>();
    JLabel jLabel1 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JList jList1 = new JList();
   
    public void jButton1_actionPerformed(ActionEvent e)
    {
        if(!jTextField1.getText().equals(""))
        {
            hset.add(jTextField1.getText());
        }
        jTextField1.setText("");
    }
   
    public void jButton2_actionPerformed(ActionEvent e)
    {
        ;
    }
   
    public void jTextField1_insertUpdate(DocumentEvent e)
    {
        Vector<String> vs=new Vector<String>();
        for(String elem: hset)
        {
            if(jTextField1.getText()=="")
            {
                jList1.setVisible(false);
            }
            if(jTextField1.getText().indexOf(jTextField1.getText(),0)==elem.indexOf(jTextField1.getText(),0))
            {
                vs.add(elem);
                jList1.setVisible(true);
            }
            jList1.setListData(vs);
        }
    }
   
    public void jTextField1_removeUpdate(DocumentEvent e)
    {
        if(jTextField1.getText().equals(""))
        {
            jList1.setVisible(false);
        }
    }
   
    public void jTextField1_mouseClicked(MouseEvent e)
    {
        if(jTextField1.getText().equals(""))
        {
            Vector<String> vs=new Vector<String>();
            for(String elem: hset)
            {
                vs.add(elem);
                jList1.setVisible(true);
            }
            jList1.setListData(vs);
        }
    }
   
    public void jList1_mouseClicked(MouseEvent e)
    {
        //判断单击次数
        if(e.getClickCount()==2)
        {
            jTextField1.setText(jList1.getSelectedValue().toString());
            jList1.setVisible(false);
        }
    }
}
class Test_jList1_mouseAdapter extends MouseAdapter {
    private Test adaptee;
    Test_jList1_mouseAdapter(Test adaptee) {
        this.adaptee = adaptee;
    }
    public void mouseClicked(MouseEvent e) {
        adaptee.jList1_mouseClicked(e);
    }
}
class Test_jTextField1_mouseAdapter extends MouseAdapter {
    private Test adaptee;
    Test_jTextField1_mouseAdapter(Test adaptee) {
        this.adaptee = adaptee;
    }
    public void mouseClicked(MouseEvent e) {
        adaptee.jTextField1_mouseClicked(e);
    }
}
class Test_jTextField1_actionAdapter implements DocumentListener
{
    private Test adaptee;
    Test_jTextField1_actionAdapter(Test adaptee)
    {
        this.adaptee=adaptee;
    }
    public void insertUpdate(DocumentEvent e)
    {
        adaptee.jTextField1_insertUpdate(e);
    }
    public void removeUpdate(DocumentEvent e)
    {
        adaptee.jTextField1_removeUpdate(e);
    }
    public void changedUpdate(DocumentEvent e) {
    }
}
class Test_jButton2_actionAdapter implements ActionListener {
    private Test adaptee;
    Test_jButton2_actionAdapter(Test adaptee) {
        this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
    }
}
class Test_jButton1_actionAdapter implements ActionListener {
    private Test adaptee;
    Test_jButton1_actionAdapter(Test adaptee) {
        this.adaptee = adaptee;
    }
    public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
    }
}

 




-----------------------------------------------------
转载请注明来源此处
原地址:#

-----网友评论----
1楼:asdf 发表于 2015-10-02 23:14:06
swing好久没玩了 事件监听的写法优于oc逊色js
2楼:自由职业 发表于 2015-11-02 11:47:22
自由职业 www.renwuyi.com
-----发表评论----
微网聚博客乐园 ©2014 blog.mn886.net 鲁ICP备14012923号   网站导航