我试图在JTextArea中打开一个文件然后写入并读取它。我终于使用FileReader在JTextArea中打开它,然后打破它尝试合并FileWriter。现在我再也无法在文本区域打开它。我看到过显示FileChooser打开特定文件的示例,但我希望用户能够传递变量,以便用户可以使用FileChooser打开他们浏览的任何文件。当我破坏代码时,我正在为我的OpenLister方法添加文件阅读器。将FileReader和FileWriter放在同一个ActionListener中是否常见?任何一个好的例子和建议的方向将非常感激。我复制了下面的代码。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ClassChooser extends JFrame implements ActionListener
{
//create a label
private JLabel response;
File file;
//menu tabs
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
String line;
//create a file chooser
private JFileChooser fc;
BufferedReader br;
//create a text area
JTextArea ta = new JTextArea();
//constructors
public ClassChooser
{
//create scroll pane
JScrollPane scrollPane = new JScrollPane(ta);
ta.setText("Enter text to see scroll bars.");
//create a panel
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
//call functions to create drop down menu's
createFileMenu();
createEditMenu();
createHelpMenu();
//create menu bar and add drop down menu's
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setContentPane(content);
this.setTitle("File Chooser");
this.setVisible(true);
this.setSize(600,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createFileMenu()
{
JMenuItem item;
fileMenu = new JMenu("File");
item = new JMenuItem("New");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Open");
item.addActionListener(new OpenListener());
fileMenu.add(item);
item = new JMenuItem("Save");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Rename");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Delete");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Make Directory");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item = new JMenuItem("Exit");
item.addActionListener(this);
fileMenu.add(item);
}
public void createEditMenu()
{
JMenuItem item;
editMenu = new JMenu("Edit");
item = new JMenuItem("Cut");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Copy");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Paste");
item.addActionListener(this);
editMenu.add(item);
}
public void createHelpMenu()
{
JMenuItem item;
helpMenu = new JMenu("Help");
item = new JMenuItem("Welcome");
item.addActionListener(this);
helpMenu.add(item);
item = new JMenuItem("Help Contents");
item.addActionListener(this);
helpMenu.add(item);
}
private class OpenListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
fc = new JFileChooser();
// directories only to be selected
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setSelectedFile(fc.getCurrentDirectory() );
fc.setDialogTitle("Directory Chooser");
fc.setMultiSelectionEnabled(false);
int retVal = fc.showOpenDialog(ClassChooser.this);
//File file;
if(retVal == fc.APPROVE_OPTION)
{
file = fc.getSelectedFile();
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while(line != null)
{
ta.append(line + "\n");
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
public static void main(String[] args)
{
ClassChooser fce = new ClassChooser;
String filename = File.separator + "tmp";
}
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String menuName;
menuName = e.getActionCommand();
if(menuName.equals("Exit"))
{
System.exit(0);
}
else
{
response.setText("Menu Item '" + menuName + "' is selected.");
}
}
}
答案 0 :(得分:3)
您的代码实际上会打开文件,但您将在文本区域中附加,而不会清除以前加载的文件的内容。
因此,在OpenListener
类actionPerformed
方法中添加ta.setText("")
作为第一个语句,然后继续加载文件内容。
代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ClassChooser extends JFrame implements ActionListener {
// create a label
private JLabel response;
File file;
// menu tabs
private JMenu fileMenu;
private JMenu editMenu;
private JMenu helpMenu;
String line;
// create a file chooser
private JFileChooser fc = null;
BufferedReader br;
// create a text area
JTextArea ta = new JTextArea();
private String currentFileBeingEdited = null;
// constructors
public ClassChooser() {
// create scroll pane
JScrollPane scrollPane = new JScrollPane(ta);
ta.setText("Enter text to see scroll bars.");
// create a panel
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
// call functions to create drop down menu's
createFileMenu();
createEditMenu();
createHelpMenu();
// create menu bar and add drop down menu's
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
this.setContentPane(content);
this.setTitle("File Chooser");
this.setVisible(true);
this.setSize(600, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void createFileMenu() {
JMenuItem item;
fileMenu = new JMenu("File");
item = new JMenuItem("New");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Open");
item.addActionListener(new OpenListener());
fileMenu.add(item);
item = new JMenuItem("Save");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Rename");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Delete");
item.addActionListener(this);
fileMenu.add(item);
item = new JMenuItem("Make Directory");
item.addActionListener(this);
fileMenu.add(item);
fileMenu.addSeparator();
item = new JMenuItem("Exit");
item.addActionListener(this);
fileMenu.add(item);
}
public void createEditMenu() {
JMenuItem item;
editMenu = new JMenu("Edit");
item = new JMenuItem("Cut");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Copy");
item.addActionListener(this);
editMenu.add(item);
item = new JMenuItem("Paste");
item.addActionListener(this);
editMenu.add(item);
}
public void createHelpMenu() {
JMenuItem item;
helpMenu = new JMenu("Help");
item = new JMenuItem("Welcome");
item.addActionListener(this);
helpMenu.add(item);
item = new JMenuItem("Help Contents");
item.addActionListener(this);
helpMenu.add(item);
}
private class OpenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//ADDED ONLY THIS LINE
ta.setText("");
fc = new JFileChooser();
// directories only to be selected
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setSelectedFile(fc.getCurrentDirectory());
fc.setDialogTitle("Directory Chooser");
fc.setMultiSelectionEnabled(false);
int retVal = fc.showOpenDialog(ClassChooser.this);
// File file;
if (retVal == fc.APPROVE_OPTION) {
file = fc.getSelectedFile();
currentFileBeingEdited = file.getAbsolutePath();
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (line != null) {
ta.append(line + "\n");
try {
line = br.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
ClassChooser fce = new ClassChooser();
String filename = File.separator + "tmp";
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String menuName;
menuName = e.getActionCommand();
if (menuName.equals("Exit")) {
System.exit(0);
} else if("Save".equalsIgnoreCase(menuName)){
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(currentFileBeingEdited));
pw.println(ta.getText());
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally {
if(pw != null){
pw.close();
}
}
} else {
response.setText("Menu Item '" + menuName + "' is selected.");
}
}
}