일기장프로그램인데여.. 저장버튼을 누르면 저장이 완료되었습니다라고
나오긴 하는데여... 파일이 어디에 저장이 되는지 잘 모르겠어여 ㅜ.ㅜ
제발 부탁드려요.. 급한거라서여.. ㅜ.ㅜ 그리고 주석 좀 자세히 달아 주시면 안될까여? 부탁 드립니다
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.io.*;
public class SaveFile extends JFrame implements ActionListener{
JTextField tf_date, tf_weather, tf_title; //날짜, 날씨, 제목
JTextArea ta_content; //내용
JButton b_save, b_cancel; //저장, 취소
GridBagLayout gbl;
GridBagConstraints gbc;
GregorianCalendar gc; //날짜관련
SimpleDateFormat sdf; //날짜의 형식을 지정
public SaveFile(){ super( "일기장");
JPanel p = new JPanel(new BorderLayout());
//전체 돗자리
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
JPanel p1 = new JPanel(gbl); gbc.fill = gbc.BOTH;
//컴포넌트가 격자보가 작을때 처리..
gbc.insets=new Insets(5,5,5,5);
//격자와 격자사이의 여백
tf_date = new JTextField("",10);
tf_weather = new JTextField("",10);
tf_title = new JTextField("",10);
ta_content = new JTextArea("",8,10);
b_save = new JButton("저 장");
b_cancel = new JButton("종 료");
layout(new JLabel("제 목 : "),0,0,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(tf_title,1,0,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(new JLabel("내 용 : "),0,1,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(new JScrollPane(ta_content),0,2,2,3,1.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(new JLabel("날 짜 : "),0,5,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(tf_date,1,5,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(new JLabel("날 씨 : "),0,6,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1);
layout(tf_weather,1,6,1,1,0.0,0.0,gbc.NONE,gbc.CENTER,p1); p.add(p1,"Center");
gc = new GregorianCalendar();
sdf = new SimpleDateFormat("yy.MM.dd HH:mm E요일");
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DAY_OF_MONTH),gc.get(Calendar.HOUR_OF_DAY),gc.get(Calendar.MINUTE));
tf_date.setText(sdf.format(gc.getTime()));
tf_date.setEnabled(false);
JPanel p2 = new JPanel();
p2.add(b_save); p2.add(b_cancel);
p.add(p2,"South");
b_save.addActionListener(this);
b_cancel.addActionListener(this);
getContentPane().add(p);
}
public void layout(JComponent obj, int x, int y, int width, int height, double weight_x, double weight_y, int fill, int anchor, JPanel p1)
{
gbc.gridx = x; //컴포넌트가 표시될 격자 위치, 왼쪽 상단에서 오른쪽 하단 방향
gbc.gridy = y; gbc.gridwidth = width; //컴포넌트가 차지할 격자의 폭과 높이지정
gbc.gridheight = height;
gbc.weightx = weight_x; //컴포넌트의 크기를 비율로 지정
gbc.weighty = weight_y;
gbl.setConstraints(obj,gbc);
p1.add(obj); } //layout()
public boolean Save(){ gc = new GregorianCalendar();
sdf = new SimpleDateFormat("yyyyMMdd");
gc.set(gc.get(Calendar.YEAR),gc.get(Calendar.MONTH),gc.get(Calendar.DAY_OF_MONTH));
String filename = sdf.format(gc.getTime());
String savefile = "제목:"+tf_title.getText()+"\n내용:"+ta_content.getText()+"\n날씨:"+tf_weather.getText();
try{ FileWriter fw = new FileWriter(filename+".dat");
fw.write(savefile,0,savefile.length());
fw.close();
JOptionPane.showMessageDialog(null,"저장이 완료 되었습니다.","Success!!",JOptionPane.INFORMATION_MESSAGE);
return true;
}catch(Exception e){ System.out.println(e);
return false; }
}
public void actionPerformed(ActionEvent ae){ Object obj = ae.getSource();
if(obj == b_save){ //저장버튼 이벤트
Save();
} if(obj == b_cancel){ //취소버튼 이벤트 JOptionPane.showMessageDialog(null,"안녕히 주무세요~~^^","Good Night!!",JOptionPane.INFORMATION_MESSAGE);
System.exit(0); }
}
public static void main(String[] args) {
SaveFile sf = new SaveFile();
sf.setSize(300,370);
sf.setResizable(false);
sf.setVisible(true);
}
}
첫댓글 String savefile = "제목:"+tf_title.getText()+"\n내용:"+ta_content.getText()+"\n날씨:"+tf_weather.getText(); 이걸로 봐서는 실행시킨 디렉토리나 JDK디렉토리를 찾아보시면 될듯..
아니면.. "C:\" + "제목:"... 같이 앞에 디렉토리를 명시적으로 넣으시는것도 괜찮을것 같구요.