2015-08-28 71 views
0

我是编程新手,这是我第一次使用GUI。这项任务是在文件中输入一个文件的名称和一个特定的字符,然后按下count键并在结果框中输入该号码出现的次数。我开始在主要代码中编写代码,然后尝试将代码传输到GUI,但是我遇到了转换问题。我不知道如何改变或改变。我已经玩了大约三天了,任何帮助都非常感激。这是迄今为止的GUI代码,我没有删除这篇文章的一些预生成代码。GUI文件读取的Java特定字母计数器

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

考虑提供一个[runnable示例](https://stackoverflow.com/help/mcve),它演示了您的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混淆和更好的响应 – MadProgrammer

+1

接下来,请问一个实际问题,指定您遇到的与您的代码示例相关的问题 – MadProgrammer

回答

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方法那里。了解如何在框架中创建和添加组件,学习如何将逻辑添加到组件中。

我希望这会有所帮助。