回 帖 发 新 帖 刷新版面

主题:帮我看一看

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class trySave2{
    public static void main(String args[]){
        FWindow f = new FWindow(); 
        }
}
class FWindow extends JFrame implements ActionListener{
    JFileChooser fileDialog1;
    JMenuBar MenuBar;
    JMenu Menu;
    JMenuItem itemSave,itemOpen;
    JTextArea text;
    //新增
    BufferedReader in;
    FileReader fileReader1;
    BufferedWriter out;
    FileWriter fileWriter1;
    
    FWindow(){
        setSize(300,400);
        text = new JTextArea(10,10);
        add(new JScrollPane(text),BorderLayout.CENTER);
        MenuBar = new JMenuBar();
        Menu = new JMenu("文件");
        itemSave = new JMenuItem("保存文件");
        itemOpen = new JMenuItem("打开文件");
        itemSave.addActionListener(this);
        itemOpen.addActionListener(this);
        Menu.add(itemSave);
        Menu.add(itemOpen);
        MenuBar.add(Menu);
        setJMenuBar(MenuBar);
        fileDialog1 = new JFileChooser();
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==itemSave){
            int state = fileDialog1.showSaveDialog(this);
            if(state==JFileChooser.APPROVE_OPTION){
                try{
                    File file1 = new File(fileDialog1.getCurrentDirectory(),fileDialog1.getSeclectedFile().getName());
                    fileWriter1 = new FileWriter(this);
                    out = new BufferedWriter(fileWriter1);
                    out.writer(text.getText(),0,(text.getText()).length());
                    out.close();
                    fileWriter1.close();
                }
            catch(IOException exp){}
                }
            }
        else if(e.getSource()==itemOpen){
            int state = fileDialog1.showOpenDialog(this);
                if(state==JFileChooser.APPROVE_OPTION){
                    text.setText(null);
                    try{
                        File file1 = new File(fileDialog1.getCurrrentDirectory(),fileDialog1.getSelectedFile().getName());
                        fileReader1 = new FileReader(file1);
                        in = new BufferedReader(fileReader1);
                        String s = null;
                        while((s=in.readLine())!=null){
                            text.append(s+"\n");
                            }
                        in.close();
                        fileReader1.close();
                    }    
                    catch(IOException exp){}
                }
            }
        }
    }



这是我才写的记事本保存打开功能,报错总是说 找不到符号 我找不到错误,但是总是报错,郁闷了
请各位大虾帮我看一看哈

回复列表 (共1个回复)

沙发

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;


public class  trySave2{
    public static void main(String args[]){
        FWindow f = new FWindow(); 
        }
}
class FWindow extends JFrame implements ActionListener{
    JFileChooser fileDialog1;
    JMenuBar MenuBar;
    JMenu Menu;
    JMenuItem itemSave,itemOpen;
    JTextArea text;
    //新增
    BufferedReader in;
    FileReader fileReader1;
    BufferedWriter out;
    FileWriter fileWriter1;
    
    FWindow(){
        setSize(300,400);
        text = new JTextArea(10,10);
        add(new JScrollPane(text),BorderLayout.CENTER);
        MenuBar = new JMenuBar();
        Menu = new JMenu("文件");
        itemSave = new JMenuItem("保存文件");
        itemOpen = new JMenuItem("打开文件");
        itemSave.addActionListener(this);
        itemOpen.addActionListener(this);
        Menu.add(itemSave);
        Menu.add(itemOpen);
        MenuBar.add(Menu);
        setJMenuBar(MenuBar);
        fileDialog1 = new JFileChooser();
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==itemSave){
            int state = fileDialog1.showSaveDialog(this);
            if(state==JFileChooser.APPROVE_OPTION){
                try{
                    File file1 = new File(fileDialog1.getSelectedFile().getCanonicalPath());
                    fileWriter1 = new FileWriter(file1);
                    out = new BufferedWriter(fileWriter1);
                    out.write(text.getText().replaceAll("\n", "\r\n"));
                    out.close();
                    fileWriter1.close();
                }
            catch(IOException exp){}
                }
            }
        else if(e.getSource()==itemOpen){
            int state = fileDialog1.showOpenDialog(this);
                if(state==JFileChooser.APPROVE_OPTION){
                    text.setText(null);
                    try{
                        File file1 = new File(fileDialog1.getSelectedFile().getCanonicalPath());
                        fileReader1 = new FileReader(file1);
                        in = new BufferedReader(fileReader1);
                        String s = null;
                        while((s=in.readLine())!=null){
                            text.append(s+"\n\r");
                            }
                        in.close();
                        fileReader1.close();
                    }    
                    catch(IOException exp){}
                }
            }
        }
    }

我来回复

您尚未登录,请登录后再回复。点此登录或注册