gpt4 book ai didi

java - 如何解决java swing中的一键按下延迟问题?

转载 作者:太空宇宙 更新时间:2023-11-04 12:02:19 26 4
gpt4 key购买 nike

我正在尝试开发自动建议文本字段。当我在文本字段中输入时,它是延迟一键并建议晚一键**

When i am typing aaa it is querying as 'aa' only and suggest according to that in list.When i am pressing any key after aaa (extra key Press) then it will querying as aaa

private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(10, 11, 414, 239);
frame.getContentPane().add(panel);
panel.setLayout(null);

textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {

try {

addListItem();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
textField.setBounds(71, 24, 86, 20);
panel.add(textField);
textField.setColumns(10);

list = new JList<String>();

list.setBounds(71, 55, 86, 97);
list.setVisible(false);
list.getScrollableTracksViewportHeight();

panel.add(list);
}

public void addListItem() throws Exception {

list.setVisible(true);

Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdemo", "root", "root");
ps = c.prepareStatement(
"SELECT stud_name FROM student_info where stud_name like '" + textField.getText().trim() + "%' ");
ResultSet s = ps.executeQuery();

model = new DefaultListModel<String>();
while (s.next()) {
model.addElement(s.getString("stud_name"));
System.out.println(s.getString("stud_name"));

}
list.setModel(model);

}

最佳答案

使用keyReleased而不是KeyAdapter类的keyPressed方法。

关于java - 如何解决java swing中的一键按下延迟问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40782499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com