문자저장기


선생님이한 등록 저장기

메뉴바를 달고, 메뉴를 달고, 아이템을 달아야한다!!!!

 


flush







문자저장기
public class MAIN {

       public static void main(String[] args) {
             // TODO Auto-generated method stub

             new GUI();
            
      }

}

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;


public class MANAGER {

      FileOutputStream fos =null ;
      FileWriter fw = null;
      
       // File(): 숫자를 파일을 만들거나 연결시켜준다. 파일을 생성한다.  //FileWriter(); 문자열을 파일로 저장한다.
      
       public void writeMessage(String dir, String contents){    // 디렉토리랑 내용을 가져오면, 해당 디렉토리에 해당 파일명에 내용을 넣을것이다.

                   try {
                         contents=contents .replaceAll("\n", "\r\n");  // .replaceAll: 특정 문자열을 찾기&바꾸는 메소드
                         fw= new FileWriter(dir ); 
                         fw.write( contents);
                         fw.flush();
                  } catch (IOException e1 ) {
                         // TODO Auto-generated catch block
                         e1.printStackTrace();
                  } 
                   try {
                         fw.write( contents);
                  } catch (IOException e ) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                  }

      }
      
}



import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class GUI extends JFrame {
       private JTextField textField ;
       private JTextArea textArea ;
       private JMenuItem mntmNewMenuItem ;
       private MANAGER tm ;
      
      
       public GUI() {
             super("텍스트저장기" );
             tm= new MANAGER();  // MANAGER 객체화
            
            getContentPane().setLayout( null);
            
            JLabel label = new JLabel("\uC81C\uBAA9");
             label.setBounds(17, 56, 60, 36);
            getContentPane().add( label);
            
             textField = new JTextField();
             textField.setBounds(74, 61, 570, 27);
            getContentPane().add( textField);
             textField.setColumns(10);
            
            JSeparator separator = new JSeparator();
             separator.setBounds(27, 107, 642, 2);
            getContentPane().add( separator);
            
            JLabel label_1 = new JLabel("\uB0B4\uC6A9");
             label_1.setBounds(17, 124, 60, 36);
            getContentPane().add( label_1);
            
             textArea = new JTextArea();
             textArea.setBounds(74, 133, 570, 332);
            getContentPane().add( textArea);
            
            JMenuBar menuBar = new JMenuBar();
            setJMenuBar( menuBar);
            
            JMenu mnNewMenu = new JMenu("\uBA54\uB274");
             menuBar.add(mnNewMenu );
            
             mntmNewMenuItem = new JMenuItem("\uC800\uC7A5");
             mnNewMenu.add(mntmNewMenuItem );
            
             mntmNewMenuItem.addActionListener(new aListener());
            
            setVisible( true);
            setSize(742,590);
            
      }
      
       public class   aListener implements ActionListener{
            
             @Override
             public void actionPerformed(ActionEvent e){
                  
                   if(e .getActionCommand().equals("저장")){
                        
                        FileDialog fd= new FileDialog(GUI.this ,"파일저장" ,FileDialog.SAVE);   // ()안에 첫번째:아버지가 될 frame,두번째:파일 탐색기 제목, 세번째:모드
                        
                        String content=textArea .getText();
                        String Filename=textField .getText();
                        
//1
                         fd.setFile( Filename+".txt" );   // 이렇게 하면 파일이름을 따로 하지 않아도 된다.
//2
                         fd.setVisible( true);  // 보여줘야하므로, setVisible(true); 를 해줘야함.
//3                     
                        String dir=fd .getDirectory();
                        
                         tm.writeMessage( dir+Filename +".txt" ,content );
                        

                        
                  }
                  
            }
            
      }

}







등록저장기(보기만 한것)
package test;

public class Main {

    public static void main(String[] args) {
        new GUI();
    }
}




package test;

public class VO {

    private String name;
    private String age;

    public VO(String name, String age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "이름=" + name + "  나이=" + age;
    }

}



package test;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Manager {
    private FileWriter fw;
    private ArrayList<VO> alist = new ArrayList<>();

    public ArrayList<VO> getAlist() {
        return alist;
    }

    public void setAlist(ArrayList<VO> alist) {
        this.alist = alist;
    }

    public void addList(VO vo) {
        alist.add(vo);
    }

    public void save(String dir) {
        try {
            fw = new FileWriter(dir);
            fw.write(alist.toString());
            fw.flush();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}




package test;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.JLabel;

public class GUI extends JFrame {
    private JTextField textField;
    private JTextField textField_1;
    private JList list;
    private JMenuItem menuItem;
    private JMenuItem menuItem_1;
    private Manager mg;

    public GUI() {
        super("간단등록부");
        mg = new Manager();
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("\uBA54\uB274");
        menuBar.add(mnNewMenu);

        menuItem = new JMenuItem("\uC800\uC7A5");
        mnNewMenu.add(menuItem);

        menuItem_1 = new JMenuItem("\uB4F1\uB85D");
        mnNewMenu.add(menuItem_1);
        getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setBounds(72, 29, 116, 21);
        getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(255, 29, 116, 21);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);

        list = new JList();
        list.setBounds(27, 92, 376, 124);
        getContentPane().add(list);

        JSeparator separator = new JSeparator();
        separator.setBounds(12, 71, 391, 11);
        getContentPane().add(separator);

        JLabel lblNewLabel = new JLabel("\uC774\uB984");
        lblNewLabel.setBounds(12, 32, 57, 15);
        getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("\uB098\uC774");
        lblNewLabel_1.setBounds(200, 32, 57, 15);
        getContentPane().add(lblNewLabel_1);

        menuItem.addActionListener(new alistener());
        menuItem_1.addActionListener(new alistener());
        setSize(446, 301);
        setVisible(true);

    }

    private class alistener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getActionCommand().equals("등록")) {
                String name = textField.getText();
                String age = textField_1.getText();

                VO vo = new VO(name, age);
                mg.addList(vo);
                list.setListData(mg.getAlist().toArray());
                textField.setText("");
                textField_1.setText("");

            } else if (e.getActionCommand().equals("저장")) {

                FileDialog fd = new FileDialog(GUI.this, "저장", FileDialog.SAVE);
                fd.setVisible(true);
                String dir = fd.getDirectory();
                String fileName = fd.getFile();
                mg.save(dir + fileName);
            }
        }

    }
}







+ Recent posts