[App 개발] AWT java text 에디터입니다.
본문
import java.awt.event.*;
import java.io.*;
class EditorDemo extends Frame implements ActionListener{
TextArea ta_content;
Label lb_status;
MenuBar mbar;
Menu menu_file;
MenuItem mi_new,mi_open,mi_save,mi_close,mi_openDialog;
FileDialog fd;
Dialog d;
public void setupMbar(){
mbar = new MenuBar();
setMenuBar(mbar);
menu_file = new Menu("file");
mbar.add(menu_file);
mi_new = new MenuItem("new file");
mi_new.addActionListener(this);
menu_file.add(mi_new);
menu_file.addSeparator();
mi_open = new MenuItem("open");
mi_open.addActionListener(this);
menu_file.add(mi_open);
mi_save = new MenuItem("save");
mi_save.addActionListener(this);
menu_file.add(mi_save);
menu_file.addSeparator();
mi_openDialog = new MenuItem("open dial log");
mi_openDialog.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setSize(200,100);
d.setTitle("dial log");
d.add(new Label("dial log",Label.CENTER));
d.setLocation(getLocationOnScreen().x + getWidth()/2-d.getWidth()/2,
getLocationOnScreen().y + getHeight()/2-d.getHeight()/2);
d.show();
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
//System.exit(0);
d.hide();
}
});
}
});
menu_file.add(mi_openDialog);
mi_close = new MenuItem("close");
mi_close.addActionListener(this);
menu_file.add(mi_close);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == mi_new){
lb_status.setText("delete");
ta_content.setText("");
}else if(e.getSource() == mi_close){
System.exit(0);
}else if(e.getSource() == mi_open){
fd.setTitle("open");
fd.setMode(FileDialog.LOAD);
fd.show();
lb_status.setText("opening file... " + fd.getDirectory()+fd.getFile());
openFile(fd.getDirectory()+fd.getFile());
}else if(e.getSource() == mi_save){
fd.setTitle("save as");
fd.setMode(FileDialog.SAVE);
fd.show();
lb_status.setText("saving file.. " + fd.getDirectory()+fd.getFile());
saveFile(fd.getDirectory()+fd.getFile());
}
}
public void openFile(String path){
FileReader fr = null;
BufferedReader br = null;
String readLine ="";
String content = "";
try{
fr = new FileReader(path);
br = new BufferedReader(fr);
while ((readLine = br.readLine()) != null){
content += readLine + "n";
}
ta_content.setText(content);
}catch(IOException e){
}finally{
try{
if(br != null)
br.close();
}catch(IOException e){}
}
}
public void saveFile(String path){
FileWriter fr = null;
BufferedWriter br = null;
String content = "";
try{
fr = new FileWriter(path,false);
br = new BufferedWriter(fr);
content = ta_content.getText();
br.write(content,0,content.length());
}catch(IOException e){
}finally{
try{
if(br != null)
br.close();
}catch(IOException e){}
}
}
public void setupComp(){
ta_content = new TextArea();
add(ta_content,BorderLayout.CENTER);
lb_status = new Label();
lb_status.setBackground(Color.lightGray);
add("South",lb_status);
fd = new FileDialog(this);
d = new Dialog(this);
}
public EditorDemo(){
setTitle("Editor window");
setLayout(new BorderLayout());
setupComp();// 사용자 정의 메소드
setupMbar();
setSize(400,500);
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension scrSize = tool.getScreenSize();
setLocation(scrSize.width/2 - getWidth()/2,scrSize.height/2 - getHeight()/2);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args)
{
new EditorDemo();
}
}
코드입니다.
windows에서 작업을 해서 돌린거라..
아직 Xcode에서는 테스트 안해봤습니다.
~_~;;;;
최신글이 없습니다.
최신글이 없습니다.
댓글목록 1
다랑어님의 댓글
<a href=http://www.macdevcenter.com/lpt/a/2686 target=_blank>http://www.macdevcenter.com/lpt/a/2686 </a>