一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - 使用Java制作一個簡單的記事本

使用Java制作一個簡單的記事本

2019-12-10 13:54hebedich JAVA教程

本文給大家?guī)淼氖鞘褂肑ava制作一個簡單的記事本的代碼,有相同需要的朋友可以參考下

通過使用Java的Swing、IO來實現(xiàn)一個簡單記事本,實現(xiàn)打開指定的text文本文件,然后將text文件的內(nèi)容加載到Swing組件中,然后在Swing組件中編輯記事本內(nèi)容,然后同菜單的保存選項將編輯后的內(nèi)容保存到text文件中。代碼如下:

 

復(fù)制代碼代碼如下:

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
/**
*功能:簡單記事本
*2011-12-25
*/
public class SimpleNotepad implements ActionListener{
    private Frame frame;
    private FileDialog fd_load;
    private FileDialog fd_save;
    private TextArea ta;
    private String file = "";
    private MenuItem save;
    private RandomAccessFile raf;
    private FileChannel fci;
    private FileLock flock;
    private CharsetEncoder encoder;
    private CharsetDecoder decoder;
    public static void main( String args[]) {
        new SimpleNotepad().init();
    }
    public void init(){
        frame = new Frame("My Notepad");
        MenuBar mb = new MenuBar();
        Menu file = new Menu("文件");
        Menu help = new Menu("幫助");
        MenuItem open = new MenuItem("打開");
        save = new MenuItem("保存");
        save.setEnabled(false);
        file.add(open);
        file.add(save);
        mb.add(file);
        mb.add(help);      
        frame.setMenuBar(mb);
        ta = new TextArea();
        frame.add(ta,"Center");    
        open.addActionListener(this);
        save.addActionListener(this);
        frame.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }  
        });  
        frame.setSize(600,400);
        frame.setLocation(300,100);
        frame.setVisible( true);      
        fd_load = new FileDialog(frame,"打開文件",FileDialog.LOAD);
        fd_save = new FileDialog(frame,"保存文件",FileDialog.SAVE);
        Charset charset = Charset.forName(System.getProperty("file.encoding"));
        encoder = charset.newEncoder();
        decoder = charset.newDecoder();
    }
    public void actionPerformed(ActionEvent e){
        String s = e.getActionCommand();
        if(s.equals("打開")){
            fd_load.setVisible(true);
            String d = fd_load.getDirectory();
            String f = fd_load.getFile();
            if((d != null) && (f != null)){
                String destfile = d + f;
                if(destfile.equals(file)){
                    return;
                }else{
                    this.closeFile();
                    file = destfile;
                    this.loadFile();
                }  
            }
        }else if(s.equals("保存")){
            this.saveFile();
        }
    }  
    public void loadFile(){
        try{
            raf = new RandomAccessFile(file,"rw");
            fci = raf.getChannel();
            flock = fci.tryLock();
            if(flock == null){
                ta.setText("");
                JOptionPane.showMessageDialog(null, 
                    "文件正在使用中,無法以獨占的方式打開!",
                    "錯誤提示", JOptionPane.ERROR_MESSAGE);   
                file = "";
                raf.close();
                raf = null;
            }else{
                int length = (int)fci.size();
                ByteBuffer bb = ByteBuffer.allocate(length);
                fci.read(bb);
                bb.flip();
                CharBuffer cb = decoder.decode(bb);
                ta.setText(cb.toString());
                frame.setTitle("My Notepad - " + file);
                save.setEnabled(true);
            }
        }catch(IOException e){
            e.printStackTrace();   
        }
    }
    public void saveFile(){
        String content = ta.getText();
        try{
            CharBuffer cb = CharBuffer.wrap(content.toCharArray());
            ByteBuffer bb = encoder.encode(cb);
            raf.setLength(0);
            fci.write(bb);
            fci.force(true);
        }catch(IOException e){
            e.printStackTrace();   
        }
    }
    public void closeFile(){
        try{
            if(flock != null){
                flock.release();
            }
            if(raf != null){
                raf.close();   
            }
            file = "";
            frame.setTitle("My Notepad");
            save.setEnabled(false);
        }catch(IOException e){
            e.printStackTrace();   
        }  
    }  
}

 

效果圖:

使用Java制作一個簡單的記事本

以上就是本文的全部內(nèi)容了,希望大家能夠喜歡。

延伸 · 閱讀

主站蜘蛛池模板: 性色xxx | 逼里逼里香 | 爽好紧别夹宝贝叫大声点护士 | 成人丁香乱小说 | 欧美18一19性高清hd4k | 97porm自拍视频区原创 | 俄罗斯引擎首页进入 | 女女同性做爰xxoo亲吻 | 国产成人在线免费视频 | 免费国产影视观看网站入口 | videos变态极端| 国产91 最新 在线 | 精品国产美女福利在线 | 欧美日韩国产一区二区三区伦 | 欧美日韩一区二区综合 | 18美女光胸光屁屁洗澡 | 亚洲国产一区 | 亚洲国产精品一在线观看 | 国产亚洲精品第一综合另类 | 四虎com| 故意短裙公车被强好爽在线播放 | h玉足嫩脚嗯啊白丝 | 亚洲爆操| 国产成人精品免费 | 色交视频| 久热人人综合人人九九精品视频 | 被夫上司侵犯了中文字幕 | 欧美s级人做人爱c视频 | av魔镜收集号 | 色老板在线观看 | 国产精品久久国产精品99 | 国产欧美视频一区二区三区 | 日韩精品一区二区三区中文版 | 精品国产日韩一区三区 | 特黄特色大片免费视频播放 | 91大神第九部红酒气质女 | 亚瑟天堂久久一区二区影院 | 91最新入口 | 日本不卡免费新一二三区 | 亚洲欧美日韩天堂 | 日韩中文在线 |