å¾ä¹
ä¹åèªå·±åè¿ç代ç ï¼ä½ å¯ä»¥åèä¸
package 个人ç»ä¹ ;
import java.awt.FileDialog;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.Savepoint;
import javax.swing.JFrame;
/**
* å建ä¸ä¸ªç±»ä¼¼è®°äºæ¬çç¨åº
* 该ç¨åºæ¥æèªå·±ç书åçªå£
* å¯ä»¥èªå®ä¹ä¿åå°å
* @author Administrator
*
*/
/*
* å建记äºæ¬ç±»
* å®ç°ActionListeneræ¥å£
* éåå®çactionPerformedæ¹æ³
*
*/
public class JiShiBen implements ActionListener {
//æ¥æèªå·±ççªå£
JFrame frame=new JFrame("è®°äºæ¬ç¨åº");
//èåæ
MenuBar menubar=new MenuBar();
//å建ä¸æèå
Menu wenjian=new Menu("æ件");
//å建ä¿åæé®
MenuItem dakai=new MenuItem("æå¼æ件");
MenuItem baochun=new MenuItem("ä¿å");
MenuItem xinjian=new MenuItem("æ°å»º");
//å建å¯ä»¥æ¾ç¤ºææ¬çå¤è¡åºå
TextArea wenben=new TextArea();
//æ¾ç¤ºä¸ä¸ªå¯¹è¯çªå£ï¼ç¨æ·éæ©ä¸ä¸ªæ件ï¼å¦æ模å¼æ¯SAVEï¼å寻æ¾ä¸ä¸ªå°æ¹å»åå
¥ä¸ä¸ªæ件
FileDialog fd;
File file=null;
/*
* å建jishibençæé å½æ°
*/
public JiShiBen(){
frame.setMenuBar(menubar);//å°èåæ å
³èå°çªå£
menubar.add(wenjian);//å°ä¸æèåæ·»å å°èåæ
wenjian.add(xinjian);
wenjian.add(dakai);//å°ä¿åæé®æ·»å å°ä¸æèå
wenjian.add(baochun);
dakai.addActionListener(this);//ç±baochuanæé®æ¥åå¤çæä½äºä»¶
baochun.addActionListener(this);
xinjian.addActionListener(this);
frame.add(wenben);//å°ææ¬åºåæ·»å å°çªå£
//设置çªå£çå±æ§
frame.setSize(600,480);
frame.setLocation(200, 200);
frame.setVisible(true);
/*
*
* ç¨äºæ¥æ¶çªå£äºä»¶ç侦å¬å¨æ¥å£ã
* æ¨å¨å¤ççªå£äºä»¶çç±»è¦ä¹å®ç°æ¤æ¥å£ï¼åå
¶å
å«çæææ¹æ³ï¼ï¼
* è¦ä¹æ©å±æ½è±¡ç±» WindowAdapterï¼ä»
éåæéçæ¹æ³ï¼ã
* ç¶å使ç¨çªå£ç addWindowListener æ¹æ³å°ä»è¯¥ç±»æå建
* ç侦å¬å¨å¯¹è±¡å该 Window 注åã
* å½éè¿æå¼ãå
³éãæ¿æ´»æåç¨ã
* å¾æ åæåæ¶å¾æ åèæ¹åäºçªå£ç¶ææ¶ï¼
* å°è°ç¨è¯¥ä¾¦å¬å¨å¯¹è±¡ä¸çç¸å
³æ¹æ³ï¼
* å¹¶å° WindowEvent ä¼ éç»è¯¥æ¹æ³ã
* windowClosing(WindowEvent e)
ç¨æ·è¯å¾ä»çªå£çç³»ç»èåä¸å
³éçªå£æ¶è°ç¨ã
*
*/
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);//ç»æ¢å½åè¿è¡çjavaèææº
}
});
}
/*
* å®ç°ActionListeneræ¥å£ä¸çæ¹æ³
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==baochun){
//public Object getSource()æååç Event ç对象ã è¿å,æååç Event ç对象ã
fd=new FileDialog(frame,"ä¿åææ¬æ件",FileDialog.SAVE);
/*
* FileDialog(Dialog parent, String title, int mode)
å建ä¸ä¸ªå
·ææå®æ é¢çæ件对è¯æ¡çªå£ï¼ç¨äºå è½½æä¿åæ件ã
static int LOAD
æ¤å¸¸éå¼æ示æ件对è¯æ¡çªå£çä½ç¨æ¯æ¥æ¾è¦è¯»åçæ件ã
static int SAVE
æ¤å¸¸éå¼æ示æ件对è¯æ¡çªå£çä½ç¨æ¯æ¥æ¾è¦åå
¥çæ件ã
*/
fd.setVisible(true);
//æ件çåæ¾ä½ç½®æ¯éè¿fdè·åçä½ç½®ï¼æ件åæ¯éè¿fdè·åçæ件å
file=new File(fd.getDirectory(), fd.getFile()+".txt");
save(file);
}
if(e.getSource()==dakai){
fd=new FileDialog(frame,"æå¼æ件",FileDialog.LOAD);
fd.setVisible(true);
/*
* String getFile()
è·åæ¤æ件对è¯æ¡çéå®æ件ã
String getDirectory()
è·åæ¤æ件对è¯æ¡çç®å½ã
*/
file=new File(fd.getDirectory()+fd.getFile());
System.out.println(fd.getFile());
System.out.println(fd.getDirectory());
open(file);
}
if(e.getSource()==xinjian){
wenben.setText("");
}
}
/*
* å建saveæ¹æ³
*/
public void save(File file){
try{
PrintWriter pw=new PrintWriter(file);
pw.write(wenben.getText());//wenben.getText()è¿åææ¬åºåçææ¬
pw.close();
}catch(Exception e){
}
}
//å建openæ¹æ³
public void open(File file){
try{
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
String line=null;
while((line=br.readLine())!=null){
wenben.append(line+"\n");
}
br.close();
}catch(Exception e){
}
}
public static void main(String[] args) {
new JiShiBen();
}
}