读入文件

时间:2015-08-28 06:59:01

标签: java swing

我是编程新手,这是我第一次使用GUI。分配是输入文件的名称和要在文件中计数的特定字符,然后按计数按钮,在结果框中,它将为您提供该数字出现的次数。我首先在main中开始编码然后尝试将代码转移到GUI,但是我在转换时遇到了问题。我不知道如何改变或改变什么。我已经玩了大约三天了,任何帮助都非常感谢。这是GUI的代码到目前为止,我确实删除了这篇文章的一些预生成代码。

import java.util.Scanner;
import java.io.*;
import javax.swing.*;
public class LetterCounterUI extends javax.swing.JFrame {
}                       

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    //Scanner keyboard = new Scanner(System.in);//used to get user input
    String fileName = jTextField1.getText();//getting file name from user
    String letter = jTextField2.getText();//specific letter to search for
}                                           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    File file = new File(jTextField1.getText());//opening file to read data
    Scanner data = new Scanner(file);

    String letter = jTextField2.getText();

    int tally = 0;//keeps track of number of times letter occurs

    while(data.hasNext())//will read file while it still has data to read
    {
        //making sure the computer is reading the correct file
        String oneLine = data.nextLine();//reads in one line

        for (int i =0; i<oneLine.length(); i++){
            char myChar = oneLine.charAt(i);//getting each character line by line
            String myString = ""+ myChar;//converting single character to a string 
            if (letter.matches(myString)){
                tally++;
            }
        }

    }
    data.close();//closes file

    jTextField3.setText(String.valueOf(tally));//converts tally to a string
}                                        

1 个答案:

答案 0 :(得分:0)

让我帮助你使用GUI。

package com.thegreatcounter;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

// Your JFrame object.
public class MainPanel extends JFrame {

    // the application entry point.
    public static void main(String[] args){
        // instantiate your UI
        MainPanel mp = new MainPanel();
        // make it visible
        mp.setVisible(true);
    }

    // Declare your components
    public JTextField dfFileName = new JTextField();
    public JTextField dfStringToFind = new JTextField();
    public JButton pbCount = new JButton("Count");

    // Constructor here, where you initialize your UI components.       
    public MainPanel() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("String counter");
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(new JLabel("File"));
        panel.add(dfFileName);
        panel.add(new JLabel(" Text To Count "));
        panel.add(dfStringToFind);
        panel.add(pbCount);
        this.setContentPane(panel);

        dfFileName.setPreferredSize(new Dimension(400,20));
        dfStringToFind.setPreferredSize(new Dimension(200,20));
        pbCount.setPreferredSize(new Dimension(100,20));

        pack();

        // add logic to the push button.
        pbCount.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                int count = countStringOccurrence(dfFileName.getText(), dfStringToFind.getText());
                JOptionPane.showMessageDialog(MainPanel.this, "We have found " + count + " occurence", "Count", JOptionPane.INFORMATION_MESSAGE);
            }
        });
    }

    /**
    * To count the string occurrence within the file.
    * @param file The full path to the file to be checked.
    * @param strToFind The string of which its occurrence will be checked.
    * @return Number of occurrence of strToFind inside the specified file.
    */
    public int countStringOccurrence(String file, String strToFind) {
        // put your logic here, to open and read the file
        // and return the number of string occurence in that file.
        // dont forget to close the file.
        return 0;
    }
}

现在,您的任务是将字符串计数器的逻辑迁移到那里的countStringOccurrence方法。

了解如何构建JFrame,了解如何创建组件并将其添加到框架中,了解如何将逻辑添加到组件中。

我希望这会有所帮助。