자바 메모장을 만들었는데..
-
게시물 수정 , 삭제는 로그인 필요
저장을 하는법을 몰라서 다른이름으로 저장이랑 같게 만들었습니다...
실제 메모장처럼 저장을 맨처음만 이름을 정해서 저장하고 그다음부터 저장을 누르면 알아서 아무창이 안뜨게 저장할려면 어떻게 해야될까요? ㅠㅠ
자바 고수님들 도와주세요
package kr.ac.snut.io;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notepad extends JFrame implements ActionListener{
JTextArea output;
JScrollPane scrollPane;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem1, menuItem2,fnew,fopen,fsave,fsaveas,fexit;
JRadioButtonMenuItem rbMenuItem1, rbMenuItem2;
JCheckBoxMenuItem cbMenuItem1, cbMenuItem2;
JFileChooser fc;
public Notepad() {
super("메모장");
fc = new JFileChooser();
menuBar = new JMenuBar();
submenu = new JMenu("파일");
fnew = new JMenuItem("새 글");
fopen = new JMenuItem("불러오기");
fsave = new JMenuItem("저장하기");
fsaveas = new JMenuItem("다른 이름으로 저장");
fnew.addActionListener(this);
fopen.addActionListener(this);
fsave.addActionListener(this);
fsaveas.addActionListener(this);
submenu.add(fnew);
submenu.add(fopen);
submenu.addSeparator();
submenu.add(fsave);
submenu.add(fsaveas);
submenu.addSeparator();
menuBar.add(submenu);
output = new JTextArea(5, 30);
scrollPane = new JScrollPane(output);
add(scrollPane, "Center");
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(550, 550);
setVisible(true);
}
public static void main(String[] args) {
Notepad demo = new Notepad();
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = (JMenuItem)e.getSource();
if(o == fnew){
output.setText("");
setTitle("");
}
else if(o == fopen){
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.setTitle(file.getName());
output.setText("");
try{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
for(String str; (str = br.readLine()) != null; ){
output.append(str);
output.append("\n");
}
br.close();
}catch(Exception e1)
{
e1.printStackTrace();
}
}
}
else if(o == fsave){
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.setTitle(file.getName());
try{
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
String s = output.getText();
bw.write(s);
bw.newLine();
bw.close();
}catch(Exception e3){
e3.printStackTrace();
}
}
}
else if(o == fsaveas){
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.setTitle(file.getName());
try{
FileWriter fw = new FileWriter(file.getName());
BufferedWriter bw = new BufferedWriter(fw);
String s = output.getText();
bw.write(s);
bw.newLine();
bw.close();
}catch(Exception e3){
}
}
else{
System.exit(0);
}
}
}
}
저장을 하는법을 몰라서 다른이름으로 저장이랑 같게 만들었습니다...
실제 메모장처럼 저장을 맨처음만 이름을 정해서 저장하고 그다음부터 저장을 누르면 알아서 아무창이 안뜨게 저장할려면 어떻게 해야될까요? ㅠㅠ
자바 고수님들 도와주세요
package kr.ac.snut.io;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notepad extends JFrame implements ActionListener{
JTextArea output;
JScrollPane scrollPane;
JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem1, menuItem2,fnew,fopen,fsave,fsaveas,fexit;
JRadioButtonMenuItem rbMenuItem1, rbMenuItem2;
JCheckBoxMenuItem cbMenuItem1, cbMenuItem2;
JFileChooser fc;
public Notepad() {
super("메모장");
fc = new JFileChooser();
menuBar = new JMenuBar();
submenu = new JMenu("파일");
fnew = new JMenuItem("새 글");
fopen = new JMenuItem("불러오기");
fsave = new JMenuItem("저장하기");
fsaveas = new JMenuItem("다른 이름으로 저장");
fnew.addActionListener(this);
fopen.addActionListener(this);
fsave.addActionListener(this);
fsaveas.addActionListener(this);
submenu.add(fnew);
submenu.add(fopen);
submenu.addSeparator();
submenu.add(fsave);
submenu.add(fsaveas);
submenu.addSeparator();
menuBar.add(submenu);
output = new JTextArea(5, 30);
scrollPane = new JScrollPane(output);
add(scrollPane, "Center");
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(550, 550);
setVisible(true);
}
public static void main(String[] args) {
Notepad demo = new Notepad();
}
@Override
public void actionPerformed(ActionEvent e) {
Object o = (JMenuItem)e.getSource();
if(o == fnew){
output.setText("");
setTitle("");
}
else if(o == fopen){
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.setTitle(file.getName());
output.setText("");
try{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
for(String str; (str = br.readLine()) != null; ){
output.append(str);
output.append("\n");
}
br.close();
}catch(Exception e1)
{
e1.printStackTrace();
}
}
}
else if(o == fsave){
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.setTitle(file.getName());
try{
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
String s = output.getText();
bw.write(s);
bw.newLine();
bw.close();
}catch(Exception e3){
e3.printStackTrace();
}
}
}
else if(o == fsaveas){
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
this.setTitle(file.getName());
try{
FileWriter fw = new FileWriter(file.getName());
BufferedWriter bw = new BufferedWriter(fw);
String s = output.getText();
bw.write(s);
bw.newLine();
bw.close();
}catch(Exception e3){
}
}
else{
System.exit(0);
}
}
}
}