亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Java制作一個(gè)簡單的記事本

 更新時(shí)間:2015年02月28日 15:27:09   投稿:hebedich  
本文給大家?guī)淼氖鞘褂肑ava制作一個(gè)簡單的記事本的代碼,有相同需要的朋友可以參考下

通過使用Java的Swing、IO來實(shí)現(xiàn)一個(gè)簡單記事本,實(shí)現(xiàn)打開指定的text文本文件,然后將text文件的內(nèi)容加載到Swing組件中,然后在Swing組件中編輯記事本內(nèi)容,然后同菜單的保存選項(xiàng)將編輯后的內(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,
                    "文件正在使用中,無法以獨(dú)占的方式打開!",
                    "錯(cuò)誤提示", 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();  
        } 
    } 
}

效果圖:

 

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

相關(guān)文章

  • SpringBoot+MyBatisPlus+Vue 前后端分離項(xiàng)目快速搭建過程(后端)

    SpringBoot+MyBatisPlus+Vue 前后端分離項(xiàng)目快速搭建過程(后端)

    這篇文章主要介紹了SpringBoot+MyBatisPlus+Vue 前后端分離項(xiàng)目快速搭建過程(后端),快速生成后端代碼、封裝結(jié)果集、增刪改查、模糊查找,畢設(shè)基礎(chǔ)框架,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • Java?六類運(yùn)算符詳解

    Java?六類運(yùn)算符詳解

    這篇文章主要介紹了Java?六類運(yùn)算符,在?Java?語言中,運(yùn)算符有算數(shù)運(yùn)算符、關(guān)系運(yùn)算符、邏輯運(yùn)算符、賦值運(yùn)算符、字符串連接運(yùn)算符、條件運(yùn)算符,感興趣的朋友可以閱讀一下
    2023-03-03
  • Java多線程下載文件實(shí)現(xiàn)案例詳解

    Java多線程下載文件實(shí)現(xiàn)案例詳解

    這篇文章主要介紹了Java多線程下載文件實(shí)現(xiàn)案例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • 最新評論