zoukankan      html  css  js  c++  java
  • JAVA记事本的图形用户界面应用程序

    JAVA记事本的图形用户界面应用程序

    整体分析:

    代码实现:

    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JSeparator;
    import javax.swing.JTextArea;
    import java.awt.BorderLayout;
    import java.awt.Font;
    
    /**记事本的图形用户界面应用程序
     * @author 李祖林
     * 2017-6-22
     */
    public class Note1 implements ActionListener{
    
        private JFrame frame;
        JMenuItem New, Open, Save, Close; /*
                                             * New为新建,
                                             * Open为打开 
                                             * Save为保存 
                                             * Close为关闭
                                             */
        JTextArea textArea;
        File fileName = new File("D:\file.txt");    //默认打开文件路径
        
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Note1 window = new Note1();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        public Note1() {
        
            frame = new JFrame();
            frame.setFont(new Font("Dialog", Font.BOLD, 18));
            frame.setBounds(100, 100, 629, 412);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JMenuBar menuBar = new JMenuBar();
            frame.setJMenuBar(menuBar);
            
            JMenu menu = new JMenu("u6587u4EF6");
            menu.setFont(new Font("黑体", Font.BOLD, 18));
            menuBar.add(menu);
            
            New = new JMenuItem("u65B0u5EFA");
            New.setFont(new Font("黑体", Font.BOLD, 18));
            menu.add(New);New.addActionListener(this);
            
            Open = new JMenuItem("u6253u5F00");
            Open.setFont(new Font("黑体", Font.BOLD, 18));
            menu.add(Open);Open.addActionListener(this);
            
            Save = new JMenuItem("u4FDDu5B58");
            Save.setFont(new Font("黑体", Font.BOLD, 18));
            menu.add(Save);Save.addActionListener(this);
            
            JSeparator separator = new JSeparator();
            menu.add(separator);
            
            Close = new JMenuItem("u5173u95ED");
            Close.setFont(new Font("黑体", Font.BOLD, 18));
            menu.add(Close);Close.addActionListener(this);
            
            JMenu menu_1 = new JMenu("u7F16u8F91");
            menu_1.setFont(new Font("黑体", Font.BOLD, 18));
            menuBar.add(menu_1);
            
            JMenuItem menuItem_4 = new JMenuItem("u590Du5236");
            menu_1.add(menuItem_4);
            
            JMenuItem menuItem_5 = new JMenuItem("u7C98u8D34");
            menu_1.add(menuItem_5);
            
            JMenuItem menuItem_6 = new JMenuItem("u526Au5207");
            menu_1.add(menuItem_6);
            
            textArea = new JTextArea();
            frame.getContentPane().add(textArea, BorderLayout.CENTER);
        }
    
        public void actionPerformed(ActionEvent e) {
            if(e.getSource()==Close){
                System.exit(0);
            }
            else if (e.getSource()==Save) {
                saveFile();
            }else if (e.getSource()==Open){
                readFile();
            }else{
                textArea.setText("");
            }
            
            
        }
        /*打开文件函数*/
        public void readFile(){
            try{
                BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));    //缓存字符流
                StringBuffer buffer = new StringBuffer();                    
                String text;
                while((text = bufferedReader.readLine()) != null){
    //                buffer.append(text+"
    ");
                    textArea.append(text+"
    ");
                }
    //            textArea.setText(buffer.toString());
                bufferedReader.close();
            }catch (IOException e) {
                System.err.println("读入文件发生错误!");
            }
        }
        /*保存文件函数*/
        public void saveFile(){
            try{
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));
                String string = textArea.getText();
                bufferedWriter.write(string);
                bufferedWriter.close();
            }catch (IOException e) {
                System.err.println("保存文件发生错误!");
            }
        }
        
    }

    实验结果:

  • 相关阅读:
    VMware虚拟机中常见的问题汇总
    Windows10下安装VMware虚拟机并搭建CentOS系统环境
    myeclipse2017使用总结
    mybatis如何通过接口查找对应的mapper.xml及方法执行详解
    (转)将SVN从一台服务器迁移到另一台服务器(Windows Server VisualSVN Server)
    (转)Maven中的库(repository)详解 ---repository配置查找构件(如.jar)的远程库
    Git知识讲解
    (转)MyEclipse中使用git
    在SpringBoot中添加Logback日志处理
    (转)Spring Boot干货系列:(七)默认日志logback配置解析
  • 原文地址:https://www.cnblogs.com/jdemarryme/p/7066511.html
Copyright © 2011-2022 走看看