第十四周课程总结&实验报告

简单记事本的实现

实验代码

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.*;

import javax.swing.*;

public class TextPad implements ActionListener {

    JFrame frame;
    JMenuBar bar;
    JMenu fileMenu, editMenu;
    JMenuItem newItem, openItem, saveItem, saveasItem, closeItem;
    ImageIcon newIcon, openIcon, saveIcon, saveasIcon, closeIcon;
    JScrollPane scroll;
    JTextArea area;
    JFileChooser chooser;
    File file;

    public TextPad() {
        frame=new JFrame("记事本");
        bar=new JMenuBar();
        fileMenu=new JMenu("文件");
        editMenu=new JMenu("编辑");
        newIcon=new ImageIcon();
        openIcon=new ImageIcon();
        saveIcon=new ImageIcon();
        saveasIcon=new ImageIcon();
        closeIcon=new ImageIcon();
        newItem=new JMenuItem("新建(N)", newIcon);
        openItem=new JMenuItem("打开(O)", openIcon);
        saveItem=new JMenuItem("保存(S)", saveIcon);
        saveasItem=new JMenuItem("另存为(A)", saveasIcon);
        closeItem=new JMenuItem("关闭(X)", closeIcon);
        area=new JTextArea();
        scroll=new JScrollPane(area);

        newItem.setMnemonic(KeyEvent.VK_N);
        openItem.setMnemonic(KeyEvent.VK_O);
        saveItem.setMnemonic(KeyEvent.VK_S);
        saveasItem.setMnemonic(KeyEvent.VK_A);
        closeItem.setMnemonic(KeyEvent.VK_X);

        newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
        openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
        saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
        saveasItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK));
        closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));

        fileMenu.add(newItem);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(saveasItem);
        fileMenu.addSeparator();
        fileMenu.add(closeItem);

        newItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        saveasItem.addActionListener(this);
        closeItem.addActionListener(this);

        bar.add(fileMenu);
        bar.add(editMenu);

        frame.setJMenuBar(bar);
        frame.add(scroll);
        frame.setSize(400,300);
        frame.setVisible(true);

    }

    public void actionPerformed(ActionEvent event){
        
        Object obj=event.getSource();
        if(obj instanceof JMenuItem) {
            JMenuItem item=(JMenuItem) obj;
            if(item == newItem) {
                new TextPad();
            }else if(item == openItem) {
                chooser = new JFileChooser();
                chooser.showOpenDialog(null);
                file = chooser.getSelectedFile();
                try {
                    FileInputStream fis= new FileInputStream(file);
                        byte[] b = new byte[fis.available()];
                        fis.read(b);
                        area.append(new String(b));
                        fis.close();
                    
                } catch (FileNotFoundException e) {
                    
                    e.printStackTrace();
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }
            }else if(item == saveItem) {
                if(file!=null) {
                    try {
                        PrintStream out = new PrintStream(new FileOutputStream(file));
                        out.print(this.area.getText());
                        out.close();
                    } catch (FileNotFoundException e) {                        
                        e.printStackTrace();
                    }
                }
                
            }else if(item == saveasItem) {
                chooser.showSaveDialog(null);
                file = chooser.getSelectedFile();
                if(!file.exists()) {
                    try {
                        if(!file.exists()) {
                        file.createNewFile();
                        }
                        FileOutputStream fos=new FileOutputStream(file);
                        byte[] b = area.getText().getBytes();
                        fos.write(b);
                        fos.close();
                    } catch (IOException e) {
                        
                        e.printStackTrace();
                }
            }
        }else if(item == closeItem) {
            System.exit(1);
        }
    }
    
}
}

package test;

public class Test {
	 public static void main(String[] args) {
	        new TextPad();
	    }

}

JDBD的主要操作

posted on 2019-11-29 14:25  hmdrgql  阅读(150)  评论(0编辑  收藏  举报