Java | PTA练习:简单文本编辑器

Java | PTA练习:简单文本编辑器,第1张

Java | PTA练习:简单文本编辑器 题目要求

编写简单文本编辑器,该程序可以新建、打开、编辑和保存文本文件。当用户点击New时,新建一个文件,用户可以编辑文件内容,然后点击Save保存文件。用户点击Open时,选择一个已有文件,然后可对文件进行编辑。示例输出如图所示。

 

 代码示例
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.JFileChooser;
import javax.swing.Jframe;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.Jtextarea;



public class FileChooserTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Jframe frame = new FileChooserframe();
        frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}

class FileChooserframe extends Jframe{
    private static final int DEFAULE_WIDTH=600;
    private static final int DEFAULT_HEIGHT=400;
    private JFileChooser jfc;
    private Jtextarea jta;
    private JScrollPane scrollPane;
    public FileChooserframe() {
        setTitle("FileChooserTest");
        setSize(DEFAULE_WIDTH, DEFAULT_HEIGHT);
        jfc = new JFileChooser();
        jfc.setCurrentDirectory(new File("."));
        jta = new Jtextarea(8,40);
        add(jta);
        jta.setVisible(false);
        scrollPane=new JScrollPane(jta);
        add(scrollPane);

        JMenuBar jmb = new JMenuBar();
        setJMenuBar(jmb);

        JMenu menu = new JMenu("File");
        jmb.add(menu);

        JMenuItem newItem = new JMenuItem("New");
        menu.add(newItem);
        newItem.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        jta.setVisible(true);
                        jta.setText("");
                        setTitle("new");
                    }
                }
                );

        JMenuItem openItem = new JMenuItem("Open");
        menu.add(openItem);
        openItem.addActionListener(
                new ActionListener() {
                    @SuppressWarnings("static-access")
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        jta.setVisible(true);
                        int result = jfc.showOpenDialog(null);
                        if(result == jfc.APPROVE_OPTION) {
                            String name = jfc.getSelectedFile().getPath();
                            jta.setText("");
                            setTitle(name);
                            try {
                                FileInputStream fin = new FileInputStream(name);
                                InputStreamReader reader = new InputStreamReader(fin,"utf-8");
                                BufferedReader br = new BufferedReader(reader);
                                String line;
                                while ((line = br.readLine()) != null) {
                                    jta.append(line+"n");
                                }
                                br.close();
                                reader.close();
                                fin.close();
                            } catch (FileNotFoundException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }

                        }
                    }
                }
                );
        JMenuItem saveItem = new JMenuItem("Save");
        menu.add(saveItem);
        saveItem.addActionListener(
                new ActionListener() {

                    @SuppressWarnings("static-access")
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        // TODO Auto-generated method stub
                        jta.setVisible(true);
                        int result = jfc.showSaveDialog(null);
                        if(result == jfc.APPROVE_OPTION) {
                            File file = jfc.getSelectedFile();
                            if (!file.exists()){
                                try {
                                    file.createNewFile();
                                } catch (IOException exception) {
                                    exception.printStackTrace();
                                }
                            }
                            try {
                                FileOutputStream fout = new FileOutputStream(file);
                                OutputStreamWriter osw = new OutputStreamWriter(fout, "utf-8");
                                osw.append(jta.getText().trim());
                                osw.close();
                                fout.close();
                            } catch (Exception exception) {
                                exception.printStackTrace();
                            }
                        }
                    }
                }
                );
        JMenuItem exitItem = new JMenuItem("Exit");
        menu.add(exitItem);
        exitItem.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event)
                {
                    System.exit(0);
                }
            }
        );
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5573536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存