当我按下打开时,会出现另一个窗口,其中JFileChooser
再次选择该文件,当我再次尝试选择该文件时,它只是给我“用户取消打开命令”消息。该程序停止工作,并以未知异常结束。
这是我的代码:
import java.awt.FlowLayout;
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.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileC extends JFrame {
File file;
JFileChooser chooser = new JFileChooser();
JTextArea TArea = new JTextArea();
public FileC()
{
super.setLayout(new FlowLayout());
super.add(chooser);
super.add(TArea);
FileNameExtensionFilter filter = new FileNameExtensionFilter ( "text files " , "txt");
chooser.setFileFilter(filter);
ActionHandler handler = new ActionHandler ();
chooser.addActionListener(handler);
}
private class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e) {
int returnVal = chooser.showOpenDialog(FileC.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
} else {
JOptionPane.showMessageDialog(null,"Open command cancelled by user.");
}
try {
BufferedReader in = new BufferedReader ( new FileReader (file));
String s = in.readLine();
while (s != null)
{
TArea.append(s);
s = in.readLine();
}
}catch (FileNotFoundException z){ JOptionPane.showMessageDialog(null, "file not found"); }
catch (IOException z) { z.printStackTrace(); }
}
}
}
新代码
public class FileC extends JFrame {
File file;
JFileChooser chooser = new JFileChooser();
JTextArea TArea = new JTextArea();
JButton b = new JButton("open");
public FileC()
{
TArea.setRows(10);
TArea.setColumns(20);
super.setLayout(new FlowLayout());
super.add(TArea);
super.add(b);
FileNameExtensionFilter filter = new FileNameExtensionFilter ( "text files " , "txt");
chooser.setFileFilter(filter);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
chooser.showOpenDialog(FileC.this);
}
});
chooser.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int returnVal = chooser.showOpenDialog(FileC.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
} else {
JOptionPane.showMessageDialog(null,"Open command cancelled by user.");
}
try {
BufferedReader in = new BufferedReader ( new FileReader (file));
String s = in.readLine();
while (s != null)
{
TArea.append(s);
s = in.readLine();
}
in.close();
}catch (FileNotFoundException z){ JOptionPane.showMessageDialog(null, "file not found"); }
catch (IOException z) { z.printStackTrace(); }
}
});
}
}
答案 0 :(得分:2)
作为一个例子......
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;
public class FileChooserExample {
public static void main(String[] args) {
new FileChooserExample();
}
public FileChooserExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JButton open;
private JTextArea textArea;
private JFileChooser chooser;
public TestPane() {
setLayout(new BorderLayout());
open = new JButton("Open");
textArea = new JTextArea(20, 40);
add(new JScrollPane(textArea));
add(open, BorderLayout.SOUTH);
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (chooser == null) {
chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
chooser.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
return "Text Files (*.txt)";
}
});
}
switch (chooser.showOpenDialog(TestPane.this)) {
case JFileChooser.APPROVE_OPTION:
try (BufferedReader br = new BufferedReader(new FileReader(chooser.getSelectedFile()))) {
textArea.setText(null);
String text = null;
while ((text = br.readLine()) != null) {
textArea.append(text);
textArea.append("\n");
}
textArea.setCaretPosition(0);
} catch (IOException exp) {
exp.printStackTrace();
JOptionPane.showMessageDialog(TestPane.this, "Failed to read file", "Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
}
});
}
}
}
请查看How to use file choosers了解详情