Biblioteca Java - Blame information for rev 32
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
32 | mihai | 1 | /* |
2 | * To change this license header, choose License Headers in Project Properties. | ||
3 | * To change this template file, choose Tools | Templates | ||
4 | * and open the template in the editor. | ||
5 | */ | ||
6 | package guidemo; | ||
7 | |||
8 | import java.awt.*; | ||
9 | import java.awt.event.*; | ||
10 | import java.io.*; | ||
11 | |||
12 | import javax.swing.*; | ||
13 | |||
14 | |||
15 | public class Editor extends JFrame | ||
16 | { | ||
17 | JMenuBar mb; | ||
18 | JMenu f; | ||
19 | JMenu t; | ||
20 | JMenuItem s,o,e; | ||
21 | JTextArea ta; | ||
22 | |||
23 | Editor() | ||
24 | { | ||
25 | mb = new JMenuBar(); | ||
26 | f = new JMenu("File"); | ||
27 | |||
28 | s = new JMenuItem("Save"); | ||
29 | o = new JMenuItem("Open"); | ||
30 | e = new JMenuItem("Exit"); | ||
31 | |||
32 | s.addActionListener(new A()); | ||
33 | o.addActionListener(new A()); | ||
34 | e.addActionListener(new A()); | ||
35 | |||
36 | mb.add(f); | ||
37 | f.add(s); | ||
38 | f.add(o); | ||
39 | f.add(e); | ||
40 | |||
41 | setJMenuBar(mb); | ||
42 | |||
43 | ta = new JTextArea(); | ||
44 | add(ta); | ||
45 | |||
46 | setSize(500,400); | ||
47 | setVisible(true); | ||
48 | } | ||
49 | |||
50 | void save(String f) | ||
51 | { | ||
52 | try | ||
53 | { | ||
54 | PrintWriter pw = new PrintWriter( | ||
55 | new FileWriter(new File(f))); | ||
56 | pw.println(ta.getText()); | ||
57 | pw.close(); | ||
58 | }catch(Exception e) | ||
59 | { | ||
60 | |||
61 | } | ||
62 | |||
63 | } | ||
64 | |||
65 | void open(String f) | ||
66 | { | ||
67 | try | ||
68 | { | ||
69 | ta.setText(""); | ||
70 | BufferedReader bf = | ||
71 | new BufferedReader( | ||
72 | new FileReader(new File(f))); | ||
73 | String l = ""; | ||
74 | l = bf.readLine(); | ||
75 | while (l!=null) | ||
76 | { | ||
77 | ta.append(l+"\n"); | ||
78 | l = bf.readLine(); | ||
79 | } | ||
80 | }catch(Exception e){} | ||
81 | } | ||
82 | |||
83 | class A implements ActionListener | ||
84 | { | ||
85 | public void actionPerformed(ActionEvent e) | ||
86 | { | ||
87 | String t = ((JMenuItem)e.getSource()).getLabel(); | ||
88 | if(t.equals("Save")){ | ||
89 | FileDialog d = new FileDialog(Editor.this,"Incarca f",FileDialog.SAVE); | ||
90 | d.setVisible(true); | ||
91 | save(d.getDirectory()+d.getFile()); | ||
92 | } | ||
93 | |||
94 | if (t.equals("Open")) | ||
95 | { | ||
96 | FileDialog d = new FileDialog(Editor.this,"Incarca f",FileDialog.LOAD); | ||
97 | d.setVisible(true); | ||
98 | open(d.getDirectory()+d.getFile()); | ||
99 | } | ||
100 | |||
101 | } | ||
102 | } | ||
103 | |||
104 | public static void main(String args[]) | ||
105 | { | ||
106 | new Editor(); | ||
107 | } | ||
108 | } |