[求助]为什么我的JFileChooser的窗口一按打开就报错
如下代码,我选中文件后一按打开就报错,初学者,求教各位大神什么原因!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.*;
import java.io.*;
public class A16_11 extends JFrame implements ActionListener{
	JPanel mb;
	JTextArea ta;
	JButton an1,an2;
	JScrollPane sp;
	JFileChooser fc1,fc2;
	public static void main(String[] args){
		A16_11 test=new A16_11();
	}
	public A16_11() {
		try {//设置JFileChooser的外观为当前windows风格
			if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1){
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		mb=new JPanel();
		ta=new JTextArea();
		sp=new JScrollPane(ta);
		an1=new JButton("打开");
		an1.setActionCommand(an1.getName());
		an1.addActionListener(this);
		an2=new JButton("另存为");
		an2.setActionCommand(an2.getName());
		an2.addActionListener(this);
		this.add(sp,BorderLayout.CENTER);
		this.add(mb,BorderLayout.SOUTH);
		mb.add(an1); mb.add(an2);
		this.setTitle("打开与保存");
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(true);
		this.setVisible(true);
	}
	public void loadFile(String name)throws Exception{
		BufferedReader br=new BufferedReader(new FileReader(name));
		String aline;
		while((aline=br.readLine())!=null){
			ta.append(aline+"/n");
		}
		br.close();
	}
	public void actionPerformed(ActionEvent e){
		if(an1.getActionCommand().equals("打开")){
			FileNameExtensionFilter filter = new FileNameExtensionFilter(
					"文本文档(*.txt)",new String[]{"TXT"});
			fc1=new JFileChooser();
			fc1.setDialogTitle("打开文件");
			fc1.setMultiSelectionEnabled(false);
			fc1.setFileHidingEnabled(false);
			fc1.setAcceptAllFileFilterUsed(false);
			fc1.setFileFilter(filter); 
			fc1.setFileSelectionMode(JFileChooser.FILES_ONLY);
			fc1.showOpenDialog(this);
			if(fc1.APPROVE_OPTION==fc1.showOpenDialog(this)){
				String fileDir=fc1.getSelectedFile().getPath();
				String fileName=fc1.getName();
				String file=fileDir+fileName;
				try {
					this.loadFile(file);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
------解决思路----------------------忘了说这个小问题了。。
fc1.setFileSelectionMode(JFileChooser.FILES_ONLY);
// 之前你调用了两次fc1.showOpenDialog(this),打开文件对话框会弹出两次。。
// 调用一次fc1.showOpenDialog(this)后,会返回这个对话框关闭时的结果ret,
// 直接比较结果就行了。。
 int ret = fc1.showOpenDialog(this);