-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileGUITest.java
More file actions
83 lines (64 loc) · 1.9 KB
/
Copy pathFileGUITest.java
File metadata and controls
83 lines (64 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class FileGUITest {
public static void main(String[] args) {
MyFrame mf = new MyFrame("My Notepad",100,100,400,300);
}
}
class MyFrame extends JFrame implements ActionListener //isA
{
JLabel jl1 = new JLabel("Enter filename : ");
JTextField jt = new JTextField(20);
JLabel jl2 = new JLabel("Enter data : ");
JTextArea data = new JTextArea(5,25);
JButton b1 = new JButton("SAVE"); //hasA
JButton b2 = new JButton("Clear"); //hasA
MyFrame(String title, int row, int col, int x, int y) {
setTitle(title);
setSize(x, y);
setLocation(row, col);
setVisible(true);
FlowLayout fl = new FlowLayout();
setLayout(fl);
add(jl1); add(jt);
add(jl2); add(data);
add(b1);
add(b2);
b1.addActionListener(this); //where the actionPerformed() is ???
b2.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
//System.out.println("Button is clicked...");
if(e.getSource()==b1) {
System.out.println("b1 is clicked...save");
String filename = jt.getText();
FileWriter fw;
try {
fw = new FileWriter(filename);
System.out.println("File is ready...");
String filesData = data.getText();
fw.write(filesData);
System.out.println("Data is written....");
fw.close();
System.out.println("File is closed.....");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(e.getSource()==b2) {
System.out.println("b2 is clicked...clear");
jt.setText("");
data.setText("");
}
}
}