• 第十四周课程总结&实验报告(简单记事本的实现)


    记事本:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    import static javax.swing.KeyStroke.*;
    
    public class hua implements ActionListener{
        JFrame frame;
        JMenuBar bar;
        JMenu fileMenu, editMenu,formatMenu,viewMenu,helpMenu;
        JMenuItem newItem, openItem, saveItem, closeItem;
        ImageIcon newIcon, openIcon, saveIcon, closeIcon;
        JScrollPane scroll;
        JTextArea area;
        JFileChooser chooser;
        File file;
    
    
        public hua() {
            frame = new JFrame("ZY");
            bar = new JMenuBar();
            fileMenu = new JMenu("文件(F)");
            editMenu = new JMenu("编辑(E)");
            formatMenu= new JMenu("格式(O)");
            viewMenu=new JMenu("查看(V)");
            helpMenu= new JMenu("帮助(H)");
            newIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "A.png");
            openIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "B.png");
            saveIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "C.png");
            closeIcon = new ImageIcon("d:" + File.separator + "Test" + File.separator + "D.png");
            newItem = new JMenuItem("新建(N)", newIcon);
            openItem = new JMenuItem("打开(O)", openIcon);
            saveItem = new JMenuItem("另存为(A)", saveIcon);
            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_A);
            closeItem.setMnemonic(KeyEvent.VK_X);
    
            newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
            openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
            saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
            closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
    
            fileMenu.add(newItem);
            fileMenu.add(openItem);
            fileMenu.add(saveItem);
            fileMenu.addSeparator();
            fileMenu.add(closeItem);
            newItem.addActionListener((ActionListener) this);
            openItem.addActionListener((ActionListener) this);
            saveItem.addActionListener((ActionListener) this);
            closeItem.addActionListener((ActionListener) this);
    
            bar.add(fileMenu);
            bar.add(editMenu);
            bar.add(formatMenu);
            bar.add(viewMenu);
            bar.add(helpMenu);
            frame.setJMenuBar(bar);
            frame.add(scroll);
            frame.setVisible(true);
            frame.pack();
        }
    
        public void actionPerformed(ActionEvent event) {
            Object obj = event.getSource();
            if (obj instanceof JMenuItem) {
                JMenuItem item = (JMenuItem) obj;
                //加入动作监听
                if (item == newItem) {
                    new hua();
                }
                else if(item==openItem){
                    JFileChooser chooser=new JFileChooser();
                    chooser.showOpenDialog(null);
                    file=chooser.getSelectedFile();//返回选择的文件
                    try {
                        FileInputStream fis=new FileInputStream(file);
                        try {
                            byte[] b=new byte[fis.available()];
                            fis.read(b);
                            area.append(new String(b));
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
    
                    }
                }
                else if (item==saveItem){
                    JFileChooser chooser = new JFileChooser();
                    chooser.showSaveDialog(null);
                    file=chooser.getSelectedFile();
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                            try {
                                FileOutputStream fos=new FileOutputStream(file);
                                byte[] b = area.getText().getBytes();
                                try {
                                    fos.write(b);
                                } catch (IOException ex) {
                                    ex.printStackTrace();
                                }
                                try {
                                    fos.close();
                                } catch (IOException ex) {
                                    ex.printStackTrace();
                                }
                            } catch (FileNotFoundException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                }
    
    
                else  if(item==closeItem){
                    System.exit(1);
                }
            }
        }
    }

    测试运行:

    public class huahai {
        public static void main(String[] args) {
            new hua();
        }
    }

    实验结果:

    学习总结:

    这一周完成了记事本和学习了一些数据库库,了解到了一些不怎么清晰的框架,听老师讲析了一些数据库包的下载地址,如何去使用,

    总体而言Java的学习是越来越烧脑,因为很多地方需要详细去了解,这东西估计是熟能生巧,相信更多的练习会让我有很大的进步。

  • 相关阅读:
    搭建非域AlwaysOn win2016+SQL2016
    从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    从0开始搭建SQL Server AlwaysOn 第一篇(配置域控)
    四、基于Windows 2012配置SQL Server 2014 AlwaysOn
    三、安装SQLserver 2014(For AlwaysOn)
    二、 Windows 2012配置故障转移(For SQLServer 2014 AlwaysOn)
    Mybatis-SQL语句构建器类及日志
    Mybatis-JavaAPI
  • 原文地址:https://www.cnblogs.com/lll0719/p/11960562.html
Copyright © 2020-2023  润新知