如何在JAVA GUI中创建类似于Google搜索样式的搜索栏 [英] How to create a search bar similar to Google search style in JAVA GUI

查看:227
本文介绍了如何在JAVA GUI中创建类似于Google搜索样式的搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的程序中创建类似于谷歌搜索栏的搜索功能,当用户输入时,它实际搜索数据库并在 JTextField下方的弹出列表中显示当前结果。我是java GUI编程的新手,因此我不清楚所有java组件,因此很难找到满足我需求的合适组件,尤其是我需要用于文本字段下方弹出下拉列表的组件。我希望有些专家能给我一些见解。

I am trying to create a search feature in my program similar to google search bar, where when user is typing it actually searches the database and displays the current result in a pop-out list below the JTextField. I am new to java GUI programming therefore I am unclear about all the java components therefore it is hard to find suitable components which fulfil my needs, especially the component I need to use for the pop out drop down list below the text field. I hope some experts can show me some insight.

推荐答案

SwingX API有助于解决这个问题。您可以使用以下代码为可编辑的ComboBox实现自动完成功能。

SwingX API would helpful to solve this issue. You can use the following code to implement auto complete feature to editable ComboBox.

import javax.swing.*;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import java.awt.*;
public class Demo {

    JFrame frame = new JFrame("");
    AutoCompleteDecorator decorator;
    JComboBox combobox;

    public Demo() {
        combobox = new JComboBox(new Object[]{"","Ester", "Jordi",
            "Jordina", "Jorge", "Sergi"});
        AutoCompleteDecorator.decorate(combobox);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        frame.add(combobox);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        Demo d = new Demo();
    }
}

这篇关于如何在JAVA GUI中创建类似于Google搜索样式的搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆