当前位置: 移动技术网 > IT编程>开发语言>Java > java实现Windows记事本

java实现Windows记事本

2019年03月08日  | 移动技术网IT编程  | 我要评论
给大家分享下我之前的作品:

源文件下载:

  链接:https://pan.baidu.com/s/1zkenez5uj5vj8zuyi6zcda
  提取码:vpca

 

源码:

  jf_notpad.java实现主要面板,在二级菜单可以实现java模板建立,可以后期自己添加:

  

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;    //导入菜单
import javax.swing.filechooser.filenameextensionfilter;
import java.util.date;    //获取系统的时间
import java.text.simpledateformat;    //控制系统的时间格式

import java.awt.event.*;
import java.io.*;
import java.util.regex.*;

public class jf_notpad extends jframe implements actionlistener,windowlistener{
    //menubar
    private    jmenubar mb;
    private jmenu file,edit,format,view,help;
    
    //menutoolbar
    private jtoolbar tool;
    private    static int buttonwidth,buttonheight;
    private jbutton create,unfold,copy,cut,paste,pasteshortcut,delete;
    
    
    //file
    //jmenu下还可以有二级菜单
    private jmenu newnot;
    //jmenuitem不可以有其他菜单
    private jmenuitem notpad,model,open,save,saveas,pageset,print,exit;
    
    //edit
    private jmenuitem undoe,cute,copye,pastee,deletee,finde,findnexte,replacee,gotoe,selectalle,timedatee;
    
    //format
    private jmenuitem     wordwrap,font;
    
    //view
    private jmenuitem     statusbar;
    
    //help
    private jmenuitem     viewhelp,about;
    
    //leftspace
    private jpanel leftspace;
    
    //textarea
    private jtextarea content;
    private jscrollpane scroll;
    
    //openurl,saveurl
    private static string openurl="e:/notpad.txt";    //保存之前打开文件的路径
    private static string saveurl="e:/notpad.txt";    //保存之前保存文件的路径
    private boolean saved=false;    //判断文件是否保存过
    
    //contentfont 字体
    private font contentfont;
    
    
    //是否在打开保存面板后,还应该打开选择文件面板
    private boolean openfilebool=false;
    
    public static void main(string[] args) {
        jf_notpad not=new jf_notpad();
    }
    
    public static jbutton changeiconsize(jbutton button,string url,int width,int height,string tip){
        button.setbounds(0,0,width,height);
        imageicon buttonimg=new imageicon(url);
        //改变图片的大小
        image temp=buttonimg.getimage().getscaledinstance(button.getwidth(), button.getheight(), buttonimg.getimage().scale_default);
        button=new jbutton(new imageicon(temp));
        button.settooltiptext(tip);    //提示
        
        return button;
    }
    
    public static image changeimagesize(string url,int width,int height){
        imageicon img=new imageicon(url);
        //改变图片的大小
        image temp=img.getimage().getscaledinstance(width, height, img.getimage().scale_default);
        
        return temp;
    }
    
    jf_notpad(){
        //menubar
        mb=new jmenubar();
        file=new jmenu("file(f)");
        file.setmnemonic('f');
        edit=new jmenu("edit(e)");
        edit.setmnemonic('e');
        format=new jmenu("format(o)");
        format.setmnemonic('o');
        view =new jmenu("view(v)");
        view.setmnemonic('v');
        help=new jmenu("help(h)");
        help.setmnemonic('h');
        
        //menutoolbar
        tool=new jtoolbar();
        buttonwidth=22;
        buttonheight=22;
        
        create=new jbutton();
        create=jf_notpad.changeiconsize(create, "img/notpad/news.png", buttonwidth, buttonwidth, "new");
        unfold=new jbutton();
        unfold=jf_notpad.changeiconsize(unfold, "img/notpad/open.png", buttonwidth, buttonwidth, "open");
        copy=new jbutton();
        copy=jf_notpad.changeiconsize(copy, "img/notpad/copy.png", buttonwidth, buttonwidth, "copy");
        cut=new jbutton();
        cut=jf_notpad.changeiconsize(cut, "img/notpad/cut.png", buttonwidth, buttonwidth, "cut");
        paste=new jbutton();
        paste=jf_notpad.changeiconsize(unfold, "img/notpad/paste.png", buttonwidth, buttonwidth, "paste");
        pasteshortcut=new jbutton();
        pasteshortcut=jf_notpad.changeiconsize(pasteshortcut, "img/notpad/pasteshortcut.png", buttonwidth, buttonwidth, "pasteshortcut");
        delete=new jbutton();
        delete=jf_notpad.changeiconsize(delete, "img/notpad/delete.png", buttonwidth, buttonwidth, "delete");
        
        //menutoolbarlistener
        create.setactioncommand("notpad");
        create.addactionlistener(this);
        unfold.setactioncommand("open");
        unfold.addactionlistener(this);
        copy.setactioncommand("copye");
        copy.addactionlistener(this);
        cut.setactioncommand("cute");
        cut.addactionlistener(this);
        paste.setactioncommand("pastee");
        paste.addactionlistener(this);
        pasteshortcut.setactioncommand("pasteshortcut");
        pasteshortcut.addactionlistener(this);
        delete.setactioncommand("deletee");
        delete.addactionlistener(this);
        
        //file
        newnot=new jmenu("new(n)");
        newnot.setmnemonic('n');
        imageicon icon=new imageicon(this.changeimagesize("img/notpad/logo.png", buttonwidth-5,buttonheight-5 ));
        notpad=new jmenuitem("notpad",icon);
        model=new jmenuitem("model");
        icon=new imageicon(this.changeimagesize("img/notpad/open.png", buttonwidth-5, buttonheight-5));
        open=new jmenuitem("open",icon);
        icon=new imageicon(this.changeimagesize("img/notpad/save.png", buttonwidth-5, buttonheight-5));
        save=new jmenuitem("save",icon);
        icon=new imageicon(this.changeimagesize("img/notpad/saveas.png", buttonwidth-5, buttonheight-5));
        saveas=new jmenuitem("save as...",icon);
        pageset=new jmenuitem("pageset");
        print=new jmenuitem("print");
        icon=new imageicon(this.changeimagesize("img/notpad/exit.png", buttonwidth-5, buttonheight-5));
        exit=new jmenuitem("exit",icon);
        
        //filelistener
        notpad.setactioncommand("notpad");
        notpad.addactionlistener(this);
        model.setactioncommand("model");
        model.addactionlistener(this);
        open.setactioncommand("open");
        open.addactionlistener(this);
        save.setactioncommand("save");
        save.addactionlistener(this);
        saveas.setactioncommand("saveas");
        saveas.addactionlistener(this);
        pageset.setactioncommand("pageset");
        pageset.addactionlistener(this);
        print.setactioncommand("print");
        print.addactionlistener(this);
        exit.setactioncommand("exit");
        exit.addactionlistener(this);
        
        //edit    
        undoe=new jmenuitem("undo");
        icon=new imageicon(this.changeimagesize("img/notpad/cut.png", buttonwidth-5, buttonheight-5));
        cute=new jmenuitem("cut",icon);
        icon=new imageicon(this.changeimagesize("img/notpad/copy.png", buttonwidth-5, buttonheight-5));
        copye=new jmenuitem("copy",icon);
        icon=new imageicon(this.changeimagesize("img/notpad/paste.png", buttonwidth-5, buttonheight-5));
        pastee=new jmenuitem("paste",icon);
        icon=new imageicon(this.changeimagesize("img/notpad/delete.png", buttonwidth-5, buttonheight-5));
        deletee=new jmenuitem("delete",icon);
        finde=new jmenuitem("find...");
        findnexte=new jmenuitem("find next");
        replacee=new jmenuitem("replace...");
        gotoe=new jmenuitem("go to..");
        selectalle=new jmenuitem("select all");
        timedatee=new jmenuitem("time/data");
        
        //editlistener
        undoe.setactioncommand("undoe");
        undoe.addactionlistener(this);
        cute.setactioncommand("cute");
        cute.addactionlistener(this);
        copye.setactioncommand("copye");
        copye.addactionlistener(this);
        pastee.setactioncommand("pastee");
        pastee.addactionlistener(this);
        deletee.setactioncommand("deletee");
        deletee.addactionlistener(this);
        finde.setactioncommand("finde");
        finde.addactionlistener(this);
        findnexte.setactioncommand("findnexte");
        findnexte.addactionlistener(this);
        replacee.setactioncommand("replacee");
        replacee.addactionlistener(this);
        gotoe.setactioncommand("gotoe");
        gotoe.addactionlistener(this);
        selectalle.setactioncommand("selectalle");
        selectalle.addactionlistener(this);
        timedatee.setactioncommand("timedatee");
        timedatee.addactionlistener(this);
        
        //format
        icon=new imageicon(this.changeimagesize("img/notpad/wordwrap.png", buttonwidth-5, buttonheight-5));
        wordwrap=new jmenuitem("word wrap",icon);
        icon=new imageicon(this.changeimagesize("img/notpad/font.png", buttonwidth-8, buttonheight-8));
        font=new jmenuitem("font...",icon);
        
        //formatlistener
        wordwrap.setactioncommand("wordwrap");
        wordwrap.addactionlistener(this);
        font.setactioncommand("font");
        font.addactionlistener(this);
        
        //view
        statusbar=new jmenuitem("status bar");
        
        //viewlistener
        statusbar.setactioncommand("statusbar");
        statusbar.addactionlistener(this);
        
        //help
        viewhelp=new jmenuitem("view help");
        icon=new imageicon(this.changeimagesize("img/notpad/about.png", buttonwidth-5, buttonheight-5));
        about=new jmenuitem("about notepad",icon);
        
        //helplistener
        viewhelp.setactioncommand("viewhelp");
        viewhelp.addactionlistener(this);
        about.setactioncommand("about");
        about.addactionlistener(this);
        
        //leftspace
        leftspace=new jpanel();
        
        //contentfont 
        contentfont=new font("kaiti",font.plain,20);
        
        //textarea
        content=new jtextarea();
        content.setfont(contentfont);
        scroll=new jscrollpane(content);
        //scroll.sethorizontalscrollbar(content);
        
        //openurl,saveurl
//        openurl="e:/notpad.txt";
//        saveurl="e:/notpad.txt";
//        saved=false;
        
        //savefilepanel
//        save savef=new save("save",this);
        
        //添加组件,从下一级开始装
        newnot.add(notpad);    newnot.add(model);
        file.add(newnot);    file.add(open);    file.add(save);    file.add(saveas);    file.addseparator();    file.add(pageset);    file.add(print);    file.addseparator();    file.add(exit);
        
        edit.add(undoe);
        edit.addseparator();
        edit.add(cute);
        edit.add(copye);
        edit.add(pastee);
        edit.add(deletee);
        edit.addseparator();
        edit.add(finde);
        edit.add(findnexte);
        edit.add(replacee);
        edit.add(gotoe);
        edit.addseparator();
        edit.add(selectalle);
        edit.add(timedatee);
        
        format.add(wordwrap);
        format.add(font);
        
        view.add(statusbar);
        
        help.add(viewhelp);
        help.addseparator();
        help.add(about);
        

        mb.add(file);    mb.add(edit); mb.add(format);    mb.add(view);    mb.add(help);
        
        tool.add(create);    
        tool.add(unfold); 
        tool.add(copy);
        tool.add(cut);
        tool.add(paste);
        tool.add(pasteshortcut);
        tool.add(delete);
        
        this.setjmenubar(mb);
        this.add(tool,borderlayout.north);
        this.add(leftspace,borderlayout.west);
        this.add(scroll);
        
        
        this.settitle("notpad");
        this.seticonimage(new imageicon("img/notpad/logo.png").getimage());
        this.setlocation(300,300);
        this.setsize(400,400);
        this.setdefaultcloseoperation(this.exit_on_close);
        this.setvisible(true);
        
    }
    
    private string origcontent=null;    //保存一开始的文件或者保存过的文件
    private string copycontent=null;
    @override
    public void actionperformed(actionevent e) {
        switch(e.getactioncommand()){
        case "notpad":{
            system.out.println("notpad");
            
            this.ischangefile("txt");
            
        }break;

        case "model":{
            system.out.println("model");
            
            this.ischangefile("java");
        }break;

        case "open":{
            system.out.println("open");
            //防止第一次打开空文件时,还没保存
            if(this.origcontent==null){
                this.saved=true;
            }
            
            if(saved){
                this.openfilepanel();
            }else{
                save savef=new save("save",this);
                this.openfilepanel();
            }
            
            
            
        }break;

        case "save":{
            system.out.println("save");
            this.savefile();
            //this.setsaved(true);
            
        }break;

        case "saveas":{
            system.out.println("saveas");
            
            string str=content.gettext();
            //选择保存文件的路径
            jfilechooser filechoose=null;
            if(saveurl==null){
                filechoose=new jfilechooser();
            }else{
                filechoose=new jfilechooser(saveurl);
            }
            filenameextensionfilter filter=new filenameextensionfilter("txt & java","java","txt");    //设置可以识别的文件
            filechoose.setfilefilter(filter);
            filechoose.setdialogtitle("save file");
            filechoose.showsavedialog(save);
            filechoose.setvisible(true);
            
            file outfile =filechoose.getselectedfile();
            saveurl=filechoose.getselectedfile().getpath();
            printstream p=null;
            try{
                p=new printstream(outfile);
                system.setout(p);
                system.out.print(str);
            }catch(exception a){
                system.out.println("saveas file error!!");
            }finally{
                p.close();
            }    
        }break;

        case "pageset":{
            system.out.println("pageset");
            
        }break;

        case "print":{
            system.out.println("print");
            
        }break;

        case "exit":{
            system.out.println("exit");
            
            system.exit(-1);
        }break;

        case "undoe":{
            system.out.println("undoe");
            
        }break;

        case "cute":{
            system.out.println("cute");
            if(this.content.getselectedtext()!=null){
                this.copycontent=this.content.getselectedtext();
            }
            this.content.replaceselection("");
        }break;

        case "copye":{
            system.out.println("copye");
            if(this.content.getselectedtext()!=null){
                this.copycontent=this.content.getselectedtext();
            }
            
        }break;

        case "pastee":{
            system.out.println("pastee");
            if(copycontent!=null){
                this.content.replacerange("", content.getselectionstart(), content.getselectionend());
            }
            this.content.insert(copycontent, this.content.getselectionstart());
        }break;

        case "deletee":{
            system.out.println("deletee");
            
            this.content.replaceselection("");
        }break;

        case "finde":{
            system.out.println("finde");
            
            
        }break;

        case "findnexte":{
            system.out.println("findnexte");
            
        }break;

        case "replacee":{
            system.out.println("replacee");
            
        }break;

        case "gotoe":{
            system.out.println("gotoe");
            
        }break;

        case "selectalle":{
            system.out.println("selectalle");
            
            this.content.selectall();
        }break;

        case "timedatee":{
            system.out.println("timedatee");
            
            simpledateformat df=new simpledateformat("yyyy-mm-dd hh:mm:ss");
            //system.out.println(df.format(new date()));
            this.content.insert(df.format(new date()), content.getselectionstart());
        }break;

        case "wordwrap":{
            system.out.println("wordwrap");
            
            imageicon icon;
            if(this.content.getlinewrap()){
                this.content.setlinewrap(false);
                icon=new imageicon(this.changeimagesize("img/notpad/wordwrap.png", buttonwidth-5, buttonheight-5));
                this.wordwrap.seticon(icon);
            }else{
                this.content.setlinewrap(true);
                icon=new imageicon(this.changeimagesize("img/notpad/wordwrapno.png", buttonwidth-5, buttonheight-5));
                this.wordwrap.seticon(icon);
            }
            
        }break;

        case "font":{
            system.out.println("font");
            
            fontpanel f=new fontpanel("font",this);
            
//            this.content.setfont(new font(f.getfontn(),f.getfontsytlen(),f.getsizen()));
            
        }break;

        case "statusbar":{
            system.out.println("statusbar");
            
        }break;

        case "viewhelp":{
            system.out.println("viewhelp");
            
        }break;

        case "about":{
            system.out.println("about");
            
            about a=new about("about","1.0","feiquan","228332xxxxx@qq.com","1843522xxxx","https://www.baidu.com");
        }break;
        
        case "pasteshortcut":{
            system.out.println("pasteshortcut");
            
        }break;
        
        }
        
    }

    @override
    public void windowopened(windowevent e) {
        // todo auto-generated method stub
        
    }

    @override
    public void windowclosing(windowevent e) {
        // todo auto-generated method stub
        system.out .println("弹出是否选择保存的对话框");
        if(saved){
            save savef=new save("save",this);
        }
    }

    @override
    public void windowclosed(windowevent e) {
        // todo auto-generated method stub
        
    }

    @override
    public void windowiconified(windowevent e) {
        // todo auto-generated method stub
        
    }

    @override
    public void windowdeiconified(windowevent e) {
        // todo auto-generated method stub
        
    }

    @override
    public void windowactivated(windowevent e) {
        // todo auto-generated method stub
        
    }

    @override
    public void windowdeactivated(windowevent e) {
        // todo auto-generated method stub
        
    }

    public static string getopenurl() {
        return openurl;
    }

    public static void setopenurl(string openurl) {
        jf_notpad.openurl = openurl;
    }

    public static string getsaveurl() {
        return saveurl;
    }

    public static void setsaveurl(string saveurl) {
        jf_notpad.saveurl = saveurl;
    }
    
    //保存文件其中包含了,判断文件是否存在
    public void savefile(){
        string str=this.content.gettext();
        system.out.println("正在保存:\n"+str);
        
        //判断文件是否存在,不存在则创建文件
        file file=new file(openurl);
        if(!file.exists()){
            try{
                file.createnewfile();
            }catch(exception a){
                system.out.println("创建文件 "+file.getabsolutepath()+" 失败!!");
            }
        }
        
        printstream p=null;
        try{
            p=new printstream(new file(openurl));
            system.setout(p);
            system.out.print(str);
            saved=true;
        }catch(exception a){
            system.out.println("save file error!!");
            saved=false;
        }finally{
            p.close();
        }
        
    }
    
    //判断文件是否修改过,并根据传入的属性判断新建那个文件
    public void ischangefile(string properties){
        system.out.println("是否保存文件:"+saved);
        //判断文件是否修改过
        if(!(this.content.gettext().equals(origcontent))){
            saved=false;
        }
        
        //如果内容为空,也视为已经保存
        if(this.content.gettext()==null||this.content.gettext().equals(""))saved=true;
        
        system.out.println("判断后是否保存文件:"+saved);
        if(saved){
            switch(properties){
            case "txt":{
                this.content.settext("");
                this.settitle("notpad");
                origcontent="";
            }break;
            case "java":{
                this.settitle("notpad");
                string str="public class notpad {\n\tpublic static void main (string[] ages) {\n\t\t\n\t}\n}";
                this.content.settext(str);
                
                origcontent=str;
            }break; 
            }
            
        }else{
            system.out .println("弹出是否选择保存的对话框");
            save savef=new save("save",this);
            
        }
        system.out.println("经过保存面板后是否保存文件:"+saved);
    }
    
    //打开选择打开文件面板,并选择文件
    public void openfilepanel(){
        origcontent=this.content.gettext();
        //显示选择文件面板
        jfilechooser filechoose=null;
        if(openurl==null){
            filechoose=new jfilechooser();
        }else{
            filechoose=new jfilechooser(openurl);
        }
        filenameextensionfilter filter=new filenameextensionfilter("txt & java","java","txt");
        filechoose.setfilefilter(filter);
        filechoose.setdialogtitle("open file");
        filechoose.showopendialog(null);
        filechoose.setvisible(true);
        
        //得到文件
        string url=filechoose.getselectedfile().getabsolutepath();    //得到选择文件的路径
        
        
        //设置保存面板显示的路径
        jf_notpad.setopenurl(url);
        save savef=new save("save",this);
        savef.setvisible(false);
        savef.seturl(url);
        jlabel temp=savef.geturll();
        temp.settext(url+" ?");
        savef.seturll(temp);
        
        string name=filechoose.getselectedfile().getname();
        this.settitle(name+" - notpad");    //设置主窗口标题
        
        //文件流
        filereader fread=null;     bufferedreader buffread=null;
        
        try{
            fread=new filereader(openurl);
            buffread=new bufferedreader(fread);
            
            string str=""; string readline="";
            readline=buffread.readline();
            while(readline!=null){
                str=str+readline+"\r\n";
                readline=buffread.readline();
            }
            
            content.settext(str);
            origcontent=str;
        }catch(exception a){
            system.out.println("open file error!!");
        }finally{
            try{
                fread.close();
                buffread.close();
            }catch(exception b){
                system.out.println("open file stream error!!");
            }
        }
    }

    public boolean issaved() {
        return saved;
    }

    public void setsaved(boolean saved) {
        this.saved = saved;
    }

    public boolean getopenfilebool() {
        return openfilebool;
    }

    public void setopenfilebool(boolean openfilebool) {
        this.openfilebool = openfilebool;
    }

    public string getorigcontent() {
        return origcontent;
    }

    public void setorigcontent(string origcontent) {
        this.origcontent = origcontent;
    }

    public jtextarea getcontent() {
        return content;
    }

    public void setcontent(jtextarea content) {
        this.content = content;
    }
}
jf_notpad

 

  fontpanel.java实现,字体面板:

  

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class fontpanel extends jframe implements actionlistener{
    private jlist list1,list2,list3;
    private jscrollpane scroll1,scroll2,scroll3;
    private jlabel l1,l2,l3,l4,l5,l6,simplel;
    private jbutton ok,cancel,use;
    private jpanel p,scrollp,p2,space,space2,simple,mainp,bottomp;
    private  static string fontn="kaiti";
    private static int fontsytlen=0,sizen=20;
    private  jf_notpad notpad ;
    
//    public static void main (string[] ages) {
//        fontpanel f=new fontpanel("font");
//        
//    }
    
    fontpanel(string title,jf_notpad notpad){
        
        this.notpad=notpad;
        
        string[] s={"font","fontstyle","size"};
        
        //获取系统字体
        string[] font=graphicsenvironment.getlocalgraphicsenvironment().getavailablefontfamilynames();
        string style=" plain,bold, italic ";
        string[] fontstyle=style.split(",");
        string sizestr="";
        for(int i=8;i<73;i++){
            sizestr=sizestr+i+",";
        }
        string[] size=sizestr.split(",");
        int count=5;
        
        list1=new jlist(font);
        list1.setvisiblerowcount(count);
        list2=new jlist(fontstyle);
        list2.setvisiblerowcount(count);
        list3=new jlist(size);
        list3.setvisiblerowcount(count);
        
        list1.setselectedindex(153);
        list2.setselectedindex(0);
        list3.setselectedindex(12);
        
        scroll1=new jscrollpane(list1);
        scroll2=new jscrollpane(list2);
        scroll3=new jscrollpane(list3);
        
        l1=new jlabel(s[0],jlabel.center);
        l2=new jlabel(s[1],jlabel.center);
        l3=new jlabel(s[2],jlabel.center);
        l4=new jlabel(this.fontn,jlabel.center);
        l5=new jlabel(fontstyle[this.fontsytlen],jlabel.center);
        l6=new jlabel(string.valueof(this.sizen),jlabel.center);
        
        
        ok=new jbutton("ok");     cancel=new jbutton("cancel");
        ok.setactioncommand("ok");     ok.addactionlistener(this);
        cancel.setactioncommand("cancel");    cancel.addactionlistener(this);
        
        p=new jpanel();
        p.setlayout(new gridlayout(2,3,10,10));
        
        p.add(l1);    p.add(l2);    p.add(l3);
        p.add(l4);    p.add(l5);    p.add(l6);
        
        scrollp=new jpanel(new gridlayout(1,3,10,10));
        
        scrollp.add(scroll1);    scrollp.add(scroll2);    scrollp.add(scroll3);
        
        simple=new jpanel();
        simple.setlayout(new flowlayout(flowlayout.left));
        simplel=new jlabel("aabbyyzz");
        simplel.setfont(new font(fontpanel.fontn,fontpanel.fontsytlen,fontpanel.sizen));
        
        use=new jbutton("simple");
        use.setactioncommand("simple");
        use.addactionlistener(this);
        
        simple.add(use);    simple.add(new jlabel("   "));    simple.add(simplel);
        
        p2=new jpanel();
        p2.setlayout(new flowlayout(flowlayout.right));
        
        p2.add(ok);    p2.add(cancel);
        
        space=new jpanel();
        space2=new jpanel();
        
        bottomp=new jpanel(new gridlayout(2,1,10,10));
        bottomp.add(simple);    bottomp.add(p2);
        
        mainp =new jpanel(new gridlayout(3,1,10,10));
        
        mainp.add(p);    mainp.add(scrollp);     mainp.add(bottomp);
        
        this.add(mainp);
        this.add(p2,borderlayout.south);    
        this.add(space,borderlayout.west);
        this.add(space2,borderlayout.east);
        
        this.settitle(title);
        this.seticonimage(new imageicon("img/notpad/font.png").getimage());
        this.setlocation(300,300);
        this.setsize(500,640);
        this.setvisible(true);
        this.setresizable(false);
//        this.setdefaultcloseoperation(this.exit_on_close);
        
    }

    @override
    public void actionperformed(actionevent e) {
        switch(e.getactioncommand()){
        case "ok":{
            this.setfontn(this.list1.getselectedvalue().tostring());
            this.setfontsytlen(this.list2.getselectedindex());
            this.setsizen(this.list3.getselectedindex()+8);
//            system.out.println(this.getfontn()+"\t"+this.getfontsytlen()+"\t"+this.getsizen());
            this.setvisible(false);
            
            font f=new font(fontpanel.fontn,fontpanel.fontsytlen,fontpanel.sizen);
            jtextarea t=notpad.getcontent();
            t.setfont(f);
            notpad.setcontent(t);
        }break;
        case "cancel":{
            this.setvisible(false);
        }break;
        case "simple":{
            this.simplel.setfont(new font(this.list1.getselectedvalue().tostring(),this.list2.getselectedindex(),this.list3.getselectedindex()+8));
        }break;
        }
    }

    public static string getfontn() {
        return fontn;
    }

    public static void setfontn(string fontn) {
        fontpanel.fontn = fontn;
    }

    public static int getfontsytlen() {
        return fontsytlen;
    }

    public static void setfontsytlen(int fontsytlen) {
        fontpanel.fontsytlen = fontsytlen;
    }

    public static int getsizen() {
        return sizen;
    }

    public static void setsizen(int sizen) {
        fontpanel.sizen = sizen;
    }
}
fontpanel.java

 

 

   about.java实现关于面板:

  

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public  class about extends jframe implements actionlistener {
    private jlabel [] l={null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null};
    private jpanel p,p2;
    private jbutton b;
    
    about(string title,string version,string copyrigth,string qqemil,string relative,string href){
        string [] s={"","","version:",version," "," ","@copyright:",copyrigth,"qq邮箱:",qqemil,"联系方式:",relative,"","<html><a href='"+href+"'>更多</a></html>>","",""};
        jpanel p=new jpanel();
        p.setlayout(new gridlayout((int)l.length/2,2));
        for(int i=0;i<l.length;i++){
            if(i%2==0){
                l[i]=new jlabel(s[i],jlabel.center);
            }else{
                l[i]=new jlabel(s[i],jlabel.left) ;
            }
            //设置手型
            if(i==13){
                l[i].setcursor(cursor.getdefaultcursor().getpredefinedcursor(cursor.hand_cursor));
            }
            p.add(l[i]);
        }
        system.out.println(l.length);
        b=new jbutton("ok");
        b.setactioncommand("ok");
        b.addactionlistener(this);
        
        p2=new jpanel();
        p2.setlayout(new flowlayout(flowlayout.right));
        p2.add(b);
        p2.add(new jlabel("          "));
        
        this.add(p);
        this.add(p2,borderlayout.south);
        this.settitle(title);
        this.seticonimage(new imageicon("img/notpad/about.png").getimage());
        this.setlocation(300,300);
        this.setsize(350,300);
        this.setvisible(true);
        this.setresizable(false);
        //this.setdefaultcloseoperation(this.exit_on_close);
    }
    
    public void actionperformed(actionevent e)
    {
        system.out.println(e);
    }
}
about.java

 

    save.java实现文件的保存读取:

  

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class save extends jframe implements actionlistener {
    private jlabel l,urll;
    private jbutton save,nosave,cancel;
    private string url;
    private jpanel bottomp,contentp;
    private font font;
    private jf_notpad notpad;
    
//    public static void main(string[] ages){
//        save s=new save("save");
//    }
    
    save(string title,jf_notpad notpad){
        this.notpad=notpad;
        
        font=new font("",font.bold,14);
        
        url=notpad.getopenurl()+" ?";
        urll=new jlabel(url,jlabel.center);
        urll.setfont(font);
        l=new jlabel("do you want to save changes to ",jlabel.center);
        l.setfont(font);
        
        contentp=new jpanel(new gridlayout(2,1));
        contentp.add(l);    contentp.add(urll);
        
        save=new jbutton("save");
        nosave=new jbutton("don't save");
        cancel=new jbutton("cancel");
        
        save.setactioncommand("save");
        nosave.setactioncommand("nosave");
        cancel.setactioncommand("cancel");
        
        save.addactionlistener(this);
        nosave.addactionlistener(this);
        cancel.addactionlistener(this);
        
        bottomp=new jpanel(new flowlayout(flowlayout.right));
        
        bottomp.add(save);    bottomp.add(nosave);    bottomp.add(cancel);
        
        
        this.add(contentp);
        this.add(bottomp,borderlayout.south);
        
        this.settitle(title);
        this.setlocation(300,300);
        this.setsize(400,170);
        this.setresizable(false);
//        this.setdefaultcloseoperation(this.exit_on_close);
        this.setvisible(true);
        
    }

    @override
    public void actionperformed(actionevent e) {
        switch(e.getactioncommand()){
        case "save":{
            notpad.savefile();
            notpad.setsaved(true);
            notpad.setorigcontent(notpad.getcontent().gettext());
            this.setvisible(false);

        }break;
        case "nosave":{
            notpad.setorigcontent(notpad.getcontent().gettext());
            notpad.setsaved(true);
            this.setvisible(false);
        }break;
        case "cancel":{
            notpad.setsaved(false);
            this.setvisible(false);
        }break;
        }
    }

    public string geturl() {
        return url;
    }

    public void seturl(string url) {
        this.url = url;
    }

    public jlabel geturll() {
        return urll;
    }

    public void seturll(jlabel urll) {
        this.urll = urll;
    }
}
save.java

 

 

 

版权

作者:feiquan

出处:http://www.cnblogs.com/feiquan/

版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

大家写文都不容易,请尊重劳动成果~ 这里谢谢大家啦(*/ω\*)

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网