java savedialog1

  1. package abc.jdm;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.awt.event.*;
  6. public class MyNotepad extends JFrame{
  7.     private JTextField filenameTF=new JTextField(),dirTF=new JTextField();
  8.     private JButton openbutton=new JButton("open"),savebutton=new JButton("save");
  9.     private JPanel optpane=new JPanel(),navigatepane=new JPanel();
  10.     private JTextArea contentta=new JTextArea(5,20);
  11.     public MyNotepad(String title){
  12.         super(title);
  13.         openbutton.addActionListener(new openhandeler());
  14.         savebutton.addActionListener(new savehandeler());
  15.         optpane.add(openbutton);
  16.         optpane.add(savebutton);
  17.         dirTF.setEditable(false);
  18.         filenameTF.setEditable(false);
  19.         navigatepane.setLayout(new GridLayout(2,1));
  20.         navigatepane.add(filenameTF);
  21.         navigatepane.add(dirTF);
  22.         Container contentpane=getContentPane();
  23.         contentpane.add(optpane,BorderLayout.SOUTH);
  24.         contentpane.add(navigatepane,BorderLayout.NORTH);
  25.         contentpane.add(new JScrollPane(contentta),BorderLayout.CENTER);
  26.         setSize(500,300);
  27.         setVisible(true);
  28.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.     }
  30.     //自定义定义打开事件类.
  31.     class openhandeler implements ActionListener{
  32.         public void actionPerformed(ActionEvent e){
  33.             JFileChooser jfilechooser = new JFileChooser();
  34.             int result=jfilechooser.showOpenDialog(MyNotepad.this);
  35.             if(result==JFileChooser.APPROVE_OPTION){
  36.                 File dir=jfilechooser.getCurrentDirectory();
  37.                 File file=jfilechooser.getSelectedFile();
  38.                 filenameTF.setText(file.getName());
  39.                 dirTF.setText(dir.toString());
  40.                 contentta.setText(read(new File(dir,file.getName())));
  41.                 
  42.             }
  43.             if(result==JFileChooser.CANCEL_OPTION)
  44.                 dirTF.setText("");
  45.             
  46.         }
  47.     }
  48.     //自定义保存文件的事件.
  49.     class savehandeler implements ActionListener{
  50.         public void actionPerformed(ActionEvent e){
  51.             JFileChooser jfilechooser=new JFileChooser();
  52.             int result=jfilechooser.showSaveDialog(MyNotepad.this);
  53.             if(result==JFileChooser.APPROVE_OPTION){
  54.                 File dir=jfilechooser.getCurrentDirectory();
  55.                 File file=jfilechooser.getSelectedFile();
  56.                 filenameTF.setText(file.getName());
  57.                 dirTF.setText(dir.toString());
  58.                 write(new File(dir,file.getName()),contentta.getText());
  59.             }
  60.             if(result==JFileChooser.CANCEL_OPTION)
  61.                 dirTF.setText("");
  62.             
  63.         }
  64.     }
  65.     /**
  66.      * @param args
  67.      */
  68.     //读文件。
  69.     private String read(File file){
  70.         try{
  71.             BufferedReader read=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
  72.             String data=null;
  73.             StringBuffer buffer=new StringBuffer();
  74.             while((data=read.readLine())!=null)
  75.                 buffer.append(data+"/n");
  76.                 read.close();
  77.                 return buffer.toString();
  78.         }
  79.         catch(IOException e){
  80.             throw new RuntimeException(e);
  81.             }
  82.         }
  83.     //保存文件.
  84.     public void write(File file,String str){
  85.         try{
  86.         PrintWriter write=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"GBK"));
  87.         write.println(str);
  88.         write.close();
  89.         }
  90.         catch(IOException e){
  91.         throw new RuntimeException(e);  
  92.         }
  93.     }
  94.     public static void main(String[] args) {
  95.         // TODO Auto-generated method stub
  96.         new MyNotepad("记事本");
  97.     }
  98. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值