从 GUI 搜索文本文件
好的,我已经在控制台中使用扫描仪创建了一个搜索功能,但我现在想从我的 GUI 创建一个搜索功能。当文本输入到 JTextField
并单击 JButton
时,我想要一种方法,可以逐行搜索我的文本文件,直到找到搜索条件并将其打印出来到 JOptionPane
中。
文本文件中的数据格式如下:
最好的方法是什么?
提前致谢
Ok so i have made a search function using scanner in console, but i would now like to create a search function from my GUI. When text is entered into a JTextField
and JButton
is clicked i would like a method which would search my text file line by line until it finds the the searched for criteria and prints it into a JOptionPane
.
Data in text file is formatted as follows:
what would be the best way to go about this?
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经有了搜索方法,因此向您的按钮添加一个操作侦听器,它将调用您的方法,例如:
看到您的更新,您最好拆分搜索功能,因此它将接收搜索条件作为输入,例如:
现在,当您从控制台运行它时,您首先调用
getSearchCriteria
,然后调用Search
。Search
的输入是getSearchCriteria
的返回值。在 GUI 中,您只需要调用搜索(使用 JTextField 中的文本作为输入)。You already have the search method, so add an action listener to your button, that will call your method, something like:
Seeing your update, you better split the search function, so it will receive the search criteria as an input, something like:
Now, when you run it from console, you first call the
getSearchCriteria
and thenSearch
. The input ofSearch
is the return value ofgetSearchCriteria
. In your GUI you only need to call search (with the text from the JTextField as an input).我不知道..
i don't know..