码迷,mamicode.com
首页 > 其他好文 > 详细

界面编程 文件IO 监听

时间:2015-06-14 19:45:53      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

记事本的打开保存功能    用到了JFileChoser

 1 package MyNote;
 2 
 3 import java.awt.BorderLayout;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 import java.io.*;
 7 
 8 import javax.swing.*;
 9 
10 public class MyNote extends JFrame implements ActionListener{
11 
12     /**
13      * @param args
14      */
15     JTextArea jta;
16     JScrollPane jsp;
17     
18     JMenuBar jmb;
19     JMenu jm;
20     JMenuItem jmi1,jmi2;
21     public static void main(String[] args) {
22         // TODO Auto-generated method stub
23         MyNote mn = new MyNote();
24     }
25 
26     public MyNote(){
27         jta = new JTextArea();
28         jsp = new JScrollPane(jta);
29         jmb = new JMenuBar();
30         jm = new JMenu("File");
31         jmi1 = new JMenuItem("open");
32         jmi2 = new JMenuItem("save");
33         jmi1.addActionListener(this);
34         jmi1.setActionCommand("open");
35         jmi2.addActionListener(this);
36         jmi2.setActionCommand("save");
37         
38         this.setJMenuBar(jmb);
39         jm.add(jmi1);
40         jm.add(jmi2);
41         jmb.add(jm);
42         
43         this.add(jsp);
44         this.setSize(400,300);
45         this.setVisible(true);
46         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
47     }
48 
49     @Override
50     public void actionPerformed(ActionEvent e) {
51         // TODO Auto-generated method stub
52         if(e.getActionCommand().equals("open")){
53             System.out.println("open");
54             JFileChooser fc = new JFileChooser("dd");
55             fc.setDialogTitle("请选择");
56             fc.showOpenDialog(null);
57             fc.setVisible(true);
58             
59             String s = fc.getSelectedFile().getAbsolutePath();
60             System.out.print(s);
61             
62             FileReader fr =null;
63             BufferedReader br=null;
64             String str = "";
65             String str1 = "";
66             
67             try {
68                 fr = new FileReader(s);
69                 br = new BufferedReader(fr);
70                 
71                 int n=0;
72                 while((str=br.readLine())!=null){
73                     str1 = str1+str+"\r\n";
74                 }
75             } catch (Exception e1) {
76                 // TODO Auto-generated catch block
77                 e1.printStackTrace();
78             }finally{
79                 try {
80                     br.close();
81                 } catch (IOException e1) {
82                     // TODO Auto-generated catch block
83                     e1.printStackTrace();
84                 }
85             }
86             
87             jta.setText(str1);
88             
89         }
90     }
91 }

 

界面编程 文件IO 监听

标签:

原文地址:http://www.cnblogs.com/hzzhero/p/4575432.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!