好吧,所以我已经在控制台中使用扫描仪进行了搜索功能,但是现在我想从我的GUI创建搜索功能。当文本被输入到JTextField
和JButton
被点击时,我想要一种方法,它将逐行搜索我的文本文件,直到找到搜索条件并将其打印到JOptionPane
中。来自GUI的文本文件搜索
在文本文件中的数据格式如下:
这将是去了解这一点的最好方法是什么?
由于提前
好吧,所以我已经在控制台中使用扫描仪进行了搜索功能,但是现在我想从我的GUI创建搜索功能。当文本被输入到JTextField
和JButton
被点击时,我想要一种方法,它将逐行搜索我的文本文件,直到找到搜索条件并将其打印到JOptionPane
中。来自GUI的文本文件搜索
在文本文件中的数据格式如下:
这将是去了解这一点的最好方法是什么?
由于提前
你已经拥有的搜索方法,所以添加一个动作监听你的按钮,将调用你的方法,是这样的:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String whatToSearch = myTextField.getText();
String result = yourSearchMethod(whatToSearch);
// use the fitting method of JOptionPane to display the result
}
}
看到您的更新,分裂你更好搜索功能,因此它会得到搜索标准作为输入,像:
public class SearchProp {
public String getSearchCriteria()
{
Scanner user = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("Please enter your Search: ");
input = user.next();
}
public void Search(String input) throws FileNotFoundException{
try{
String details, id, line;
int count;
Scanner housenumber = new Scanner(new File("writeto.txt"));
while(housenumber.hasNext())
{
id = housenumber.next();
line = housenumber.nextLine();
if(input.equals(id))
{
JOptionPane.showMessageDialog(null,id + line);
break;
}
if(!housenumber.hasNext())
System.out.println("No Properties with this criteria");
}
}
catch(IOException e)
{
System.out.print("File failure");
}
}
}
现在,当您从控制台运行它,你首先调用getSearchCriteria
,然后Search
。 Search
的输入是getSearchCriteria
的返回值。在您的GUI中,您只需要调用搜索(将JTextField中的文本作为输入)。
我不知道..
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String whatToSearch = myTextField.getText();
String result = yourSearchMethod(whatToSearch);
// use the fitting method of JOptionPane to display the result
}
}
没有问题。让我知道它是否有效。 – MByD 2011-06-01 12:39:36
我明白你建议的ActionListener,但是我目前的搜索类依赖于扫描仪,如果你有机会看看我已经在我编辑的问题中发布了我的当前搜索方法 – 2011-06-01 12:54:25
我看到了,看到了我的更新,输入和实际搜索到不同的方法,这将给你更多的灵活性,因为你现在需要。 – MByD 2011-06-01 12:56:20