读入文件的GUI Java特定字母计数器
我是编程新手,这是我第一次使用GUI。赋值是输入文件名和要在文件中计数的特定字符,然后按计数按钮,在结果框中,它将给出该数字出现的次数。我首先在main中开始编码,然后尝试将代码转移到GUI中,但是转换过程中遇到了问题。我不知道如何改变或改变什么。我已经玩了大约三天了,非常感谢您的帮助。这是GUI的代码。到目前为止,我删除了一些为这篇文章预先生成的代码读入文件的GUI Java特定字母计数器,java,swing,Java,Swing,我是编程新手,这是我第一次使用GUI。赋值是输入文件名和要在文件中计数的特定字符,然后按计数按钮,在结果框中,它将给出该数字出现的次数。我首先在main中开始编码,然后尝试将代码转移到GUI中,但是转换过程中遇到了问题。我不知道如何改变或改变什么。我已经玩了大约三天了,非常感谢您的帮助。这是GUI的代码。到目前为止,我删除了一些为这篇文章预先生成的代码 import java.util.Scanner; import java.io.*; import javax.swing.*; public
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
}
让我来帮助您使用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;
}
}
现在,您的任务是将string counter的逻辑迁移到countStringOccurrence方法中
了解如何构造JFrame,了解如何创建组件并将其添加到框架中,了解如何将逻辑添加到组件中
我希望这能有所帮助。考虑提供一份能证明您的问题的报告。这不是一个代码转储,而是您正在做的一个示例,它突出了您所遇到的问题。这将减少混乱并获得更好的响应接下来,问一个实际问题,指定与代码示例相关的问题