import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.*;
public class NotePadExam extends JFrame
{
JFrame jf,jf2;
JPanel jp1,jp2;
JMenuBar jmb;
JMenu jmFile;
JMenuItem jmi_CreateNew,jmi_Open,jmi_Save,jmi_OtherSave,jmi_Exit;
JLabel jlb1;
JTextArea jta;
JDialog jd;
public NotePadExam()
{
super("제목없음");
try
{
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//jf = new JFrame();
jf = this;
jf.getContentPane().setLayout(new FlowLayout());
//jf.getContentPane().setLayout(new BorderLayout());
//jf = new JFrame();
jp1 = new JPanel();
jp2 = new JPanel();
//jp1.setLayout(new FlowLayout());
jmb = new JMenuBar();
/*//////////////
파일메뉴추가
/*//////////////
jmFile = new JMenu("파일");
//jm=createMenuItem(jm,"열기");
//jm=createMenuItem(jm,"저장");
//jm=createMenuItem(jm,"다른이름으로 저장");
//jm=createMenuItem(jm,"끝내기");
jta = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(450,400));
scrollPane.setBorder(BorderFactory.createTitledBorder(""));
scrollPane.getViewport().add(jta, null);
//jpanel.add(scrollPane);
//jta.setSize(30,50);
//jd = new JDialog(this,"yes");
//jd.setLayout(new FlowLayout());
//jd.add(new Button("저장"));
//jd.setSize(100,100);
//jd.setVisible(true);
//새로 만들기 이벤트 생성
makeNewDoc(createMenuItem(jmFile,"새로만들기"),jta);
//문서열기
openDoc(createMenuItem(jmFile,"열기"),jta);
//메뉴바추가
jmb.add(jmFile);
/*//////////////
파일메뉴추가
/*//////////////
jp1.add(jmb);
//jmb.setSize(50,200);
//텍스트창추가
jp2.add(scrollPane);
jf.add(jp1);
jf.add(jp2);
//add(jf);
//jf.getContentPane().add(jp1,BorderLayout.CENTER);
//createDialogBOx(this);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void openDoc(JMenuItem jmim,final JTextArea jtaa)
{
//문서열기 작업하기
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("문서열기");
String jtaaStr = jtaa.getText().trim();
if(!jtaaStr.equals("") && jtaaStr != null)
{
System.out.println("널값 아님");
//doOpenDialogForSave(jtaa);
//기존파일이 있는 경우 저장창이 나온다.
}
else
{
//파일열기 대화상자를 열어야 한다.
NotePadJFileChooser dpjc = new NotePadJFileChooser("열기");
int returnVal = dpjc.showOpenDialog(NotePadExam.this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
File selectedFile = dpjc.getSelectedFile();
FileInputStream fis=null;
//FileOutputStream fos = null;
BufferedInputStream dos = null;
//StringBuffer strB = new StringBuffer();
try
{
fis = new FileInputStream(selectedFile);
//dos = new BufferedInputStream(fis);
byte[] buffer = new byte[512];
int readcount = 0;
//StringBuffer openedFileBuffer = new StringBuffer();
String str = new String();
while((readcount = fis.read(buffer)) != -1)
{
str+=new String(buffer,0,readcount,"EUC-KR");
//System.out.println(str);
// for(int i=readcount;i<buffer
// System.out.println(buffer.toString());
//System.out.write(buffer,0,readcount);
//openedFileBuffer.append(buffer,0,readcount);
}
// System.out.println(openedFileBuffer);
//jtaa.setText(openedFileBuffer);
//BufferedInputStream bis = new BufferedInputStream(dos);
System.out.println(str);
jtaa.setText(str);
jtaa.requestFocus();
//jtta.setFocusable(true);
//jtta.setCaretPosition(0);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
finally
{
try
{
fis.close();
}
catch (IOException e)
{
}
}
}
}
}
};
jmim.addActionListener(al);
}
public void makeNewDoc(JMenuItem jmim,final JTextArea jtaa)
{
//새파일 만들기 알고리즘////////////////////////////////////////////////////////////////////
//새로만들기 메뉴아이템 이벤트 생성
//1) 작업하던 문서가 없는 경우 바로 새문서 만듬///////////////////////////////////////////
//2) 작업하던 문서가 있는 경우(저장 안했을 경우) 저장,저장안함,취소를 물어본후 1번 작업//
//3) 작업하던 파일이 전과 비교했을 경우 변경된 게 없는 경우 이전 문서 닫고 새문서 만듬//
////////////////////////////////////////////////////////////////////////////////////////////
ActionListener al = doOpenDialogForSave(jtaa);
jmim.addActionListener(al);
}
public ActionListener doOpenDialogForSave(final JTextArea jtaa)
{
//return new ActionListener(){public void actionPerformed(ActionEvent ae){}};
ActionListener al = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String str = jtaa.getText().trim();
String msg[] = {"저장","저장안함","취소"};
//일단 작업하던 문서가 있는 지 없는 지 체크함
if(str.equals("") || str == null)
{
createNewDoc(jtaa);
}
else
{
//System.out.println("dddd");
int dialogResult =
JOptionPane.showOptionDialog(
NotePadExam.this,
"변경된 내용을 저장하시겠습니까?",
"새로만들기",
JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,msg,msg[0]
);
if(dialogResult == JOptionPane.YES_OPTION)
saveDoc(jtaa);//저장메소드
else if(dialogResult == JOptionPane.NO_OPTION)//저장취소메소드
createNewDoc(jtaa);//저장취소
else //취소
System.out.print("cancel");
//저장하던 문서가 있는경우 저장,저장안함,취소 물어봄
}
//System.out.println("새로만들기 클릭했음");
}
};
return al;
}
public void saveDoc(JTextArea jta)
{
NotePadJFileChooser jfChooser = new NotePadJFileChooser("저장");
int returnVal = jfChooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
//System.out.println("You were choosen file what named " +jfChooser.getSelectedFile().getName());
String savingFileName = jfChooser.getSelectedFile().getName();
FileWriter fw = null;
try
{
File choosenFileDirectory = new File(jfChooser.getCurrentDirectory().getAbsolutePath()+"\\"+savingFileName+".txt");
//System.out.println(choosenFileDirectory.getAbsolutePath());
fw = new FileWriter(choosenFileDirectory);
fw.write(jta.getText());
fw.close();
this.setTitle(savingFileName);
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
//this.setTitle("안녕");//쓰불 타이틀 바꾼다고 몇시간 뻘짓 했넹..==;
//StringBuffer strB = new StringBuffer(jta.getText());
}
/*
public void fileWrite(InputStream in)
{
byte[] buffer = new byte[512];
int readcount = 0;
try
{
while((readcount = in.read(buffer)) != -1)
{
System.out.write(buffer,0,readcount);
}
}
catch (IOException e)
{
System.out.println(e);
}
}
*/
public void createNewDoc(JTextArea jtaa)
{
// if(jf==null)
// setTitle("");
//super.setTitle("새로만들기");
//setTitle2("새로만들자");
this.setTitle("제목없음");
jtaa.setText("");
}
public JMenuItem createMenuItem(JMenu jmu,String name2)
{
//System.out.println("111111111");
JMenuItem jmui = new JMenuItem(name2);
jmu.add(jmui);
return jmui;
}
public static void main(String[] args)
{
NotePadExam npe = new NotePadExam();
npe.setSize(500,500);
npe.setVisible(true);
//System.out.println("Hello World!");
}
}
class NotePadJFileChooser extends JFileChooser
{
//JFileChooser jfChooser;
FileNameExtensionFilter filter;
public NotePadJFileChooser(String buttonName)
{
super();
filter = new FileNameExtensionFilter(
"텍스트 문서(*.txt)"
,"txt"
);
this.setFileFilter(filter);
this.setApproveButtonText(buttonName);
this.setApproveButtonToolTipText(buttonName);
}
/*
JFileChooser jfChooser = new JFileChooser();
FileNameExtensionFilter filter =
jfChooser.setFileFilter(filter);
jfChooser.setApproveButtonText("저장");
jfChooser.setApproveButtonToolTipText("저장하기");
int returnVal = jfChooser.showOpenDialog(this);
*/
}
'자바 > 메모장' 카테고리의 다른 글
java메모장만들기 2일차(2시간 작업) (0) | 2010.10.18 |
---|---|
메모장 제 1일차(2시간작업) (0) | 2010.10.14 |