플밍 하나도 모르는데 이틀 후에 메모장 만드는 거 코딩봐야하는 친구놈꺼 시험이라


일단은 내가 어느 정도 해주고 있긴한데


JTextArea 위주로 메모장 만드는 것도 은근 힘드네.......




암튼, 저장이 이상하게 된다고 내가 적었는데


이게 어떻게 저장이 되냐면




『안녕하세요


반갑습니다』




『』사이의 부분에 엔터키로 공백 넣었지?

그 공백이 저장 안 된 채로 그냥 붙여서 저장된다;;;


그니까 저장된 파일을 윈도우 메모장으로 실행해보면



『안녕하세요반갑습니다』



이렇게 저장됨........

이리저리 검색중인데 도대체 어떤 메소드를 넣어야 제대로 저장될까?





참고로 지금 적용시킨 메소드는



else if (e.getSource() == this.sf) {

JFileChooser sa = new JFileChooser();

int opt = sa.showSaveDialog(this);

if (opt == JFileChooser.APPROVE_OPTION) {

try {

BufferedWriter wt = new BufferedWriter(new FileWriter(sa.getSelectedFile().getPath()));

wt.write(this.jta.getText());

wt.close();

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

}

}




이거인데, 도대체 어디서 막히는지 모르겠다.

(일단 엔터키 구분 관련 구문이 없는 건 ㄹㅇㅍㅌ)














참고로 전체 소스는 다음과 같음(씹스압 주의)


import java.util.*;

import java.io.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;


import javax.swing.*;


class Note extends JFrame implements ActionListener {

JTextArea jta = new JTextArea(this.getSize().width, this.getSize().height);

JScrollPane js=new JScrollPane(jta);

Rectangle rct=new Rectangle();

Menu File = new Menu();

MenuBar menu = new MenuBar();

MenuItem of = new MenuItem();

MenuItem sf = new MenuItem();

MenuItem cls = new MenuItem();

JPanel p1=new JPanel();

JPanel p2=new JPanel();


public Note() {

js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

this.setTitle("메모장");

this.setSize(320, 240);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setLayout(new BorderLayout());

this.add(js);

/*this.getContentPane().setLayout(new BorderLayout());

this.getContentPane().add(jta);*/


this.setMenuBar(this.menu);

this.menu.add(this.File);


this.File.setLabel("파일");


this.of.setLabel("열기");

this.of.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));

this.of.addActionListener(this);

this.File.add(this.of);


this.sf.setLabel("저장");

this.sf.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));

this.sf.addActionListener(this);

this.File.add(this.sf);


this.cls.setLabel("종료");

this.cls.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));

this.cls.addActionListener(this);

this.File.add(this.cls);

jta.setEditable(true);

jta.setLineWrap(true);

jta.setWrapStyleWord(true);

this.setVisible(true);


}


@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == this.of) {

JFileChooser op = new JFileChooser();

int opt = op.showOpenDialog(this);

if (opt == JFileChooser.APPROVE_OPTION) {

this.jta.setText("");

try {

Scanner sc = new Scanner(new FileReader(op.getSelectedFile().getPath()));

while (sc.hasNext())

this.jta.append(sc.nextLine() + "\n");

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

}

} else if (e.getSource() == this.sf) {

JFileChooser sa = new JFileChooser();

int opt = sa.showSaveDialog(this);

if (opt == JFileChooser.APPROVE_OPTION) {

try {

BufferedWriter wt = new BufferedWriter(new FileWriter(sa.getSelectedFile().getPath()));

wt.write(this.jta.getText());

wt.close();

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

}

}

}


public static void main(String args[]) {

Note nt = new Note();

}

}


public class Main {

public static void main(String args[]) {

Note nt = new Note();

}


}