美文网首页
记事本-功能待添加

记事本-功能待添加

作者: 谜若 | 来源:发表于2016-11-01 18:20 被阅读14次

IO流、图形化用户界面、事件监听

import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Jsb extends JFrame implements ActionListener
{
    JMenuBar cd;
    JMenu cd1,cd2;
    JMenuItem cdx1,cdx2;
    JTextArea wby;
    JScrollPane gd;
    
    public static void main(String[] args)
    {
        Jsb js=new Jsb();
    }
    Jsb()
    {
        cd=new JMenuBar();
        cd1=new JMenu("文件(F)");
        cd1.setMnemonic('F');
        cd2=new JMenu("编辑(E)");
        cd2.setMnemonic('E');
        
        cdx1=new JMenuItem("打开",new ImageIcon("photo/dk.jpg"));
        cdx1.addActionListener(this);
        cdx1.setActionCommand("open");
        
        cdx2=new JMenuItem("保存",new ImageIcon("photo/bc.jpg"));
        cdx2.addActionListener(this);
        cdx2.setActionCommand("save");
        
        wby=new JTextArea();
        
        gd=new JScrollPane(wby);
        
        cd1.add(cdx1);  cd1.add(cdx2);
        cd.add(cd1);    cd.add(cd2);
        
        this.setJMenuBar(cd);   this.add(gd);
        
        ImageIcon tp1=new ImageIcon("photo/jsb.jpg");
        this.setIconImage(tp1.getImage());
        this.setTitle("记事本");
        this.setSize(400,300);
        this.setLocation(300,280);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);      
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("open"))  //
        {
            JFileChooser wjxz=new JFileChooser();//文件选择框
            wjxz.setDialogTitle("文件打开"); //对话框标题
            wjxz.showOpenDialog(null);//显示打开对话框的模式,null默认
            wjxz.setVisible(true);
            
            String wjlj=wjxz.getSelectedFile().getAbsolutePath();//选择文件的全路径
            FileReader wjl=null;    BufferedReader hcl=null;
            try
            {
                wjl=new FileReader(wjlj);
                hcl=new BufferedReader(wjl);
                String s="",zfc="";
                while((s=hcl.readLine())!=null)
                {
                    zfc+=(s+"\n");
                }
                wby.setText(zfc);
            }
            catch(Exception a){}
            finally
            {
                try
                {
                    wjl.close();
                    hcl.close();
                }
                catch(Exception a1){}
            }
        }
        else if(e.getActionCommand().equals("save"))
        {
            JFileChooser wjxz1=new JFileChooser();
            wjxz1.setDialogTitle("另存为");
            wjxz1.showSaveDialog(null);
            wjxz1.setVisible(true);
            
            String wjlj1=wjxz1.getSelectedFile().getAbsolutePath();
            try
            {
                PrintStream pl=new PrintStream(wjlj1);
                System.setOut(pl);
                System.out.println(this.wby.getText());             
            }
            catch(Exception a2){}
            finally
            {
                try
                {
                }
                catch(Exception a2){}
            }
        }
    }
}

本人初学者,旨在记录学习java的过程,欢迎大家指正,如果有针对上述部分代码更优秀的代码请写在评论区,我一定会回复感谢并更正。

相关文章

  • 记事本-功能待添加

    IO流、图形化用户界面、事件监听 本人初学者,旨在记录学习java的过程,欢迎大家指正,如果有针对上述部分代码更优...

  • Extensive Notes

    多功能记事本,支持日记,待做事项提醒,图文记事,而且还有涂鸦记事哦,快来试试吧。 Multi function N...

  • Swift编码规范

    待更新 文件 首字母请用大写 使用MyClass+Additions的格式添加Extension功能 Additi...

  • 记事本App

    功能清单: 1.记事本: 列表,点开后进入页面明细,点编辑再进行页面编辑。列表可以添加提醒 2.记账本: 3.我的...

  • Python+Django 搭建YAN_SPACE项目-day1

    环境OK的前提- 目标:创建一个记事本项目,有登录功能和记事本基本功能 步骤一: 通过 django-admin ...

  • VIM编辑器

    VIM概述 VI - Visual Interface (可视化接口)类似windows下的记事本,功能比记事本强...

  • windows下steam客户端社区打不开解决

    1、记事本打开 hosts 文件 2、添加hosts(注意,不要加#): 3、Done!

  • CSS(待添加)

    CSS(待添加) css的相关类容比较多,准备分几个模块写 css简介 什么是csscss(cascading s...

  • UI进阶之记事本4

    创建model类 WordNote WordNote.h中 添加几个属性 //记事本ID @property (...

  • Spider&Lua&Splash

    待添加

网友评论

      本文标题:记事本-功能待添加

      本文链接:https://www.haomeiwen.com/subject/jiwmuttx.html