gpt4 book ai didi

java - 在 Java 中实现自动完成——我做对了吗?

转载 作者:搜寻专家 更新时间:2023-10-30 21:29:23 24 4
gpt4 key购买 nike

<分区>

算法

  1. 开始
  2. 输入城市名称 - 部分或完整
  3. 如果用户点击回车,从 JTextField 中获取文本
  4. 开始强力搜索。
  5. 如果找到匹配项,将它们放入 Vector 并将其放入 JList
  6. 如果没有找到匹配项,在Vector中添加一个String“No Match Found”
  7. 向用户显示包含结果的 JWindow
  8. 停止

代码:

package test;
import javax.swing.*;

import java.awt.Dimension;
import java.awt.event.*;
import java.util.Vector;

public class AutoCompleteTest extends JFrame{
JTextField city = new JTextField(10);
String enteredName = null;
String[] cities = {"new jersey","new hampshire",
"sussex","essex","london","delhi","new york"};
JList list = new JList();
JScrollPane pane = new JScrollPane();
ResultWindow r = new ResultWindow();
//------------------------------------------------------------------------------
public static void main(String[] args) {
new AutoCompleteTest();
}
//------------------------------------------------------------------------------
public AutoCompleteTest(){
setLayout(new java.awt.FlowLayout());
setVisible(true);
add(city);
// add(pane);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
city.addKeyListener(new TextHandler());
}
//------------------------------------------------------------------------------
public void initiateSearch(String lookFor){
Vector<String> matches = new Vector<>();
lookFor = lookFor.toLowerCase();
for(String each : cities){
if(each.contains(lookFor)){
matches.add(each);
System.out.println("Match: " + each);
}
}
this.repaint();

if(matches.size()!=0){
list.setListData(matches);
r.searchResult = list;
r.pane = pane;
r.initiateDisplay();
}else{
matches.add("No Match Found");
list.setListData(matches);
r.searchResult = list;
r.pane = pane;
r.initiateDisplay();
}

}
//------------------------------------------------------------------------------
public class ResultWindow extends JWindow{
public JScrollPane pane;
public JList searchResult;
//------------------------------------------------------------------------------
public ResultWindow(){

}
//------------------------------------------------------------------------------
public void initiateDisplay(){
pane.setViewportView(searchResult);
add(pane);
pack();
this.setLocation(AutoCompleteTest.this.getX() + 2,
AutoCompleteTest.this.getY()+
AutoCompleteTest.this.getHeight());

// this.setPreferredSize(city.getPreferredSize());
this.setVisible(true);
}
}
//------------------------------------------------------------------------------

class TextHandler implements KeyListener{
@Override
public void keyTyped(KeyEvent e){

}

@Override
public void keyPressed(KeyEvent e){
if(r.isVisible()){
r.setVisible(false);
}
if(e.getKeyChar() == '\n'){
initiateSearch(city.getText());
}
}

@Override
public void keyReleased(KeyEvent e){

}
}
//------------------------------------------------------------------------------
}

输出

enter image description here

问题

显示结果的 JWindow 的大小(它是 JScrollPane 中的 JList)根据结果而变化 - 如果城市name小,JWindow小,城市名大,JWindow大。

我已经尝试了所有可能的组合。我尝试使用 JWindowJListJScrollPanesetPreferredDimension() 但问题不会解决。
我希望它与装饰的 JFrame 的大小相匹配,无论如何

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