展开全部
******************************************************************
新建类SpaceErrorCount.java,代62616964757a686964616fe78988e69d8331333262356137码如下:
******************************************************************
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
/**
* @author Godwin
* @version 2010-05-19
*/
public class SpaceErrorCount extends JFrame {
JTextArea text;
JTextArea result;
public SpaceErrorCount() {
this.setTitle("空格的规范度");
// 文本框
text = new JTextArea(6, 50);
text.setLineWrap(true);
JScrollPane textScroll = new JScrollPane(text);
text.setBorder(BorderFactory.createBevelBorder(1));
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.setBorder(BorderFactory.createTitledBorder("输入或导入的文本"));
textPanel.add(textScroll);
// 结果框
result = new JTextArea(6, 50);
result.setLineWrap(true);
JScrollPane resultScroll = new JScrollPane(result);
result.setBorder(BorderFactory.createBevelBorder(1));
JPanel resultPanel = new JPanel(new BorderLayout());
resultPanel.setBorder(BorderFactory.createTitledBorder("空格的规范度结果"));
resultPanel.add(resultScroll);
// 导入文本和结果框
JPanel allPanel = new JPanel();
allPanel.setLayout(new GridLayout(2, 1));
allPanel.add(textPanel);
allPanel.add(resultPanel);
// 按钮
JButton impButton = new JButton("导入文本");
JButton calcButton = new JButton("计算规范度");
JButton outputButton = new JButton("导出结果");
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(impButton);
buttonPanel.add(calcButton);
buttonPanel.add(outputButton);
// 添加
this.add(allPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
// this.setSize(400, 300);
this.pack();
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension screen = tool.getScreenSize();
this.setLocation(screen.width / 2 - this.getWidth() / 2, screen.height
/ 2 - this.getHeight() / 2);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 导入文本
impButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText();
}
});
// 截取
calcButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] inputs = text.getText().trim().split("\n");
TreeSet currentLineErrorPositions = new TreeSet();
StringBuffer lineOutput = new StringBuffer();
for (int lines = 0; lines
currentLineErrorPositions.clear();
byte[] lineBytes = inputs[lines].getBytes();
for (int i = 1; i
if (lineBytes[i] == ' ') {
if ((!(lineBytes[i - 1] == ','))
&& (!(lineBytes[i - 1] == ';'))
&& (!(lineBytes[i + 1] == ';'))) {
currentLineErrorPositions.add(i);
}
} else if (lineBytes[i] == ',') {
if (lineBytes[i - 1] == ' ') {
currentLineErrorPositions.add(i - 1);
}
if (!(lineBytes[i + 1] == ' ')) {
currentLineErrorPositions.add(i + 1);
}
} else if (lineBytes[i] == ';') {
if (!(lineBytes[i - 1] == ' ')) {
currentLineErrorPositions.add(i);
}
if (!(lineBytes[i + 1] == ' ')) {
currentLineErrorPositions.add(i + 1);
}
}
}
// 添加此行错误位置和本行文本
if (currentLineErrorPositions.size() > 0) {
lineOutput.append((lines + 1) + ": [");
Iterator it = currentLineErrorPositions
.iterator();
int i = 0;
while (it.hasNext()) {
lineOutput.append(it.next());
i++;
if (i
lineOutput.append(", ");
}
}
lineOutput.append("] ");
lineOutput.append(inputs[lines]);
lineOutput.append("\n");
}
}
if (lineOutput.length() == 0) {
result.setText("全部正确!");
} else {
result.setText(lineOutput.toString().trim());
}
}
});
outputButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
outputText();
}
});
}
// 导入文本
public void inputText() {
try {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(this);
File file = chooser.getSelectedFile();
BufferedReader br = new BufferedReader(new FileReader(file));
String s;
text.setText("");
while ((s = br.readLine()) != null) {
text.append(s + "\n");
}
text.setText(text.getText().trim());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
public void outputText() {
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(this);
File file = chooser.getSelectedFile();
String fileName = file.getName();
if (fileName != null && fileName.trim().length() > 0) {
if (!(fileName.endsWith(".txt"))) {
fileName = fileName.concat(".txt");
}
}
file = new File(file.getParent().concat(File.separator)
.concat(fileName));
if (file.exists()) {
int i = JOptionPane.showConfirmDialog(this, "该文件已经存在,确定要覆盖吗?");
if (i != JOptionPane.YES_OPTION) {
return;
}
}
try {
file.createNewFile();
FileWriter fw = new FileWriter(file);
fw.write(result.getText());
fw.flush();
fw.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "错误信息",
JOptionPane.WARNING_MESSAGE);
return;
}
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new SpaceErrorCount();
}
}
******************************************************************
运行结果如下:
******************************************************************