티스토리 뷰

 

//NotePad

package com.io;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.io.*;

public class NotePad extends JFrame implements ActionListener{

    JTextArea ta;
    JMenuItem newItem,openItem,saveItem,closeItem;
    public NotePad()
    {
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("파일");
        newItem = new JMenuItem("새로 만들기");
        openItem = new JMenuItem("열기");
        saveItem = new JMenuItem("저장");
        closeItem = new JMenuItem("종료");
        
        menu.add(newItem);
        menu.addSeparator();
        menu.add(openItem);
        menu.add(saveItem);
        menu.addSeparator();
        menu.add(closeItem);
        
        bar.add(menu);
        
        //bar 설정
        setJMenuBar(bar);
        
        ta = new JTextArea();
        JScrollPane js = new JScrollPane(ta);
        
        add("Center",js);
        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        //이벤트 등록
        newItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        closeItem.addActionListener(this);
        
        
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new NotePad();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==newItem)
        {
            ta.setText("");
        }
        else if(e.getSource()==openItem)
        {
            JFileChooser jc=new JFileChooser();
            if(jc.showOpenDialog(this)==jc.APPROVE_OPTION) // 열기 다이얼로그 출력
            {
                try
                {
                    File f=jc.getSelectedFile(); // 선택된 파일 불러옴
                    FileReader fis = new FileReader(f); // 2byte씩 읽어옴(한글 안깨짐)
                    int i=0;
                    /*
                     * read() : 문자 한개 리턴
                     * read(byte[],int,int) : byte 갯수
                     */

                    ta.setText("");
                    while((i=fis.read())!=-1)
                    {
                        ta.append(String.valueOf((char)i)); // 출력
                    }
                    
                }catch(Exception ex){}
            }
        }
        else if(e.getSource()==saveItem)
        {
            JFileChooser jc=new JFileChooser();
            if(jc.showSaveDialog(this)==jc.APPROVE_OPTION)
            {
                try
                {
                    String str = ta.getText().trim();
                    if(str.length()<1)
                        return;
                    File f=jc.getSelectedFile();
                    FileWriter fw=new FileWriter(f);
                    
                    fw.write(str);
                    fw.close();
                    
                }catch(Exception ex){}
            }
        }
        else if(e.getSource()==closeItem)
        {
            dispose();
            System.exit(0);
        }
    }

}

 

 

 

 

 

기본화면

 

 

위의 메뉴바에서 열기를 누르고 파일을 선택하면

 

 

요렇게 읽어옴(a.txt를 메모장으로 만들어서 저장한걸 불러옴)

 

 

수정후 다시 저장가능

 


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함