嗨,我输了一个错误。我目前正在使用Eclipse火星及其窗口构建器来浏览文本文件然后读取它并拾取它的一些部分并显示它。文本文件看起来像这样。
90 hello world
90 sunny day
89 mercedes benz
我的代码:
package ecu;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import java.util.*;
import java.io.*;
public class file {
JFileChooser fc= new JFileChooser();
StringBuilder sb = new StringBuilder();
public void PickMe() throws Exception{
if(fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
try {
BufferedReader buff = new BufferedReader(new FileReader(fc.getSelectedFile()));
try{
String readBuff = buff.readLine();
String section = "";
while (buff!=null){
if(readBuff.equals("90")){
sb.append(readBuff);
}
buff.readLine();
}
}
finally {
buff.close();
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
else {
sb.append("file not choosen");
}
}
}
所以,我希望90后的任何内容都能被写出来。 GUI代码如下所示:
package ecu;
import java.awt.EventQueue;
import javax.swing.filechooser.*;
import javax.swing.plaf.FileChooserUI;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class exjobb {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
exjobb window = new exjobb();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public exjobb() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(27, 11, 359, 142);
frame.getContentPane().add(textArea);
JButton browse = new JButton("Browse");
browse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
file of = new file();
try{
of.PickMe();
}
catch (Exception e) {
e.printStackTrace();
}
textArea.setText(of.sb.toString());
}
});
browse.setBounds(27, 191, 89, 23);
frame.getContentPane().add(browse);
}
}
我有一个文字区域,我希望我已经整理出来的文字显示在那里。然后我运行这个程序,它不起作用。我赞美所有可以得到的帮助!