⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editor.java

📁 Java程序设计大学教程程序源代码
💻 JAVA
字号:
/* * Editor.java * 参见教程295页 示例程序10-5 * Created on 2005年8月1日, 下午2:42 */package jbookch10;import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;import java.io.*;public class Editor extends JFrame implements ActionListener{    public Editor() {        fileChooser.setSize( 400,300 );        newfile.addActionListener(this);        open.addActionListener(this);        save.addActionListener(this);        buttonPanel.add(newfile);        buttonPanel.add(open);        buttonPanel.add(save);        //主面板的BorderLayout布局        mainPanel.setLayout(new BorderLayout(0,0));        mainPanel.add(new JScrollPane(display),"Center");        mainPanel.add(new JLabel("编辑词库文件(每行格式为:英文单词=中文注释):"),"North");        mainPanel.add( buttonPanel,"South");        setContentPane(mainPanel);        setSize(280,240);        setTitle("词库文件编辑器");    }        public void actionPerformed(ActionEvent evt) {        if (evt.getSource()==open ) {            open();//打开文件            return;        }        if (evt.getSource()==save ) {            save();//保存文件            return;        }        if (evt.getSource()==newfile ) {            newfile();//新建文件            return;        }    }        private void newfile(){        display.setText("");        choice = fileChooser.showSaveDialog( this );        if ( choice == JFileChooser.APPROVE_OPTION ) {            chosenFile = fileChooser.getSelectedFile();            writeToFile(chosenFile);        }    }        private void open(){        choice = fileChooser.showOpenDialog( this );        if ( choice == JFileChooser.APPROVE_OPTION ) {            chosenFile = fileChooser.getSelectedFile();        }        readFromFile(chosenFile);//读入文件    }        private void save(){        if (chosenFile == null ) {            newfile();        }        writeToFile(chosenFile);    }        private void readFromFile(File file) {        display.setText("");// 清除编辑区        String fileName=file.getAbsolutePath();        try {            BufferedReader inStream                    = new BufferedReader(new FileReader(fileName));// 新建或打开流            String line = inStream.readLine();            // 读入文本行            while (line != null) {                        //多行文本需要循环读入                display.append(line + "\n");              // 写入到文本域组件中                line = inStream.readLine();               // 读入下一行            }            inStream.close();                             // 关闭流        } catch (FileNotFoundException e) {            JOptionPane.showConfirmDialog(this,fileName +                    "文件找不到!","操作提示",JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);        } catch (IOException e) {            JOptionPane.showConfirmDialog(this,"无法预料的异常: " + e.getMessage(),                    "操作提示",JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);        }    }        private void writeToFile(File file) {        String fileName=file.getAbsolutePath();        try {            FileWriter outStream =  new FileWriter(fileName);            outStream.write(display.getText());            outStream.close();        } catch (IOException e) {            JOptionPane.showConfirmDialog(this,"无法预料的异常: " + e.getMessage(),                    "操作提示",JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);        }     }         public static void main(String args[]) {        Editor editor=new Editor();        editor.setSize( 400,400);        editor.setVisible(true);        editor.addWindowListener(new WindowAdapter() {      // 退出程序            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });    }        private JPanel mainPanel = new JPanel(),            buttonPanel = new JPanel();    private JTextArea display = new JTextArea();    private JButton open = new JButton("打开文件");    private JButton save = new JButton("保存文件");    private JButton newfile = new JButton("新建文件");    private File chosenFile;    private int choice;    private JFileChooser  fileChooser = new JFileChooser();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -