从 GUI 搜索文本文件

发布于 2024-11-11 15:15:07 字数 231 浏览 3 评论 0原文

好的,我已经在控制台中使用扫描仪创建了一个搜索功能,但我现在想从我的 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 JTextFieldand 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染柒℉ 2024-11-18 15:15:07

您已经有了搜索方法,因此向您的按钮添加一个操作侦听器,它将调用您的方法,例如:

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,然后调用 SearchSearch的输入是getSearchCriteria的返回值。在 GUI 中,您只需要调用搜索(使用 JTextField 中的文本作为输入)。

You already have the search method, so add an action listener to your button, that will call your method, something like:

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
    }
}

Seeing your update, you better split the search function, so it will receive the search criteria as an input, something like:

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");
       }
   }
}

Now, when you run it from console, you first call the getSearchCriteria and then Search. The input of Search is the return value of getSearchCriteria. In your GUI you only need to call search (with the text from the JTextField as an input).

雨落星ぅ辰 2024-11-18 15:15:07

我不知道..

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
    }
}

i don't know..

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文