-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeProject.java
More file actions
111 lines (108 loc) · 2.83 KB
/
HomeProject.java
File metadata and controls
111 lines (108 loc) · 2.83 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Home extends JFrame implements ActionListener{
JTextArea ta;
Home(){
setSize(500,400);
ta = new JTextArea();
ta.setFont(new Font("Lucida Sans Typewriter",Font.BOLD,20));
JScrollPane text_pane = new JScrollPane(ta);
add(text_pane);
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
JMenuItem newitem = new JMenuItem("New");
newitem.addActionListener(this);
JMenuItem open = new JMenuItem("Open");
open.addActionListener(this);
JMenuItem save = new JMenuItem("Save");
save.addActionListener(this);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(this);
file.add(newitem);
file.addSeparator();
file.add(open);
file.addSeparator();
file.add(save);
file.addSeparator();
file.add(exit);
JMenu edit = new JMenu("Edit");
JMenu format = new JMenu("Format");
JMenuItem font = new JMenuItem("Font");
font.addActionListener(this);
JMenuItem color = new JMenuItem("Color");
color.addActionListener(this);
format.add(font);
format.addSeparator();
format.add(color);
JMenu help = new JMenu("Help");
mb.add(file);
mb.add(edit);
mb.add(format);
mb.add(help);
setJMenuBar(mb);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
String val = ae.getActionCommand();
System.out.println(val);
if(val.equals("New")){
ta.setText("");
setTitle("Untitled");
}
if(val.equals("Open")){
JFileChooser chooser = new JFileChooser();
int k = chooser.showOpenDialog(this);
if(k==JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
String path = file.getPath();
setTitle(path);
try{
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
while(true){
String data = br.readLine();
if(data==null){
break;
}
ta.append(data+'\n');
}
}
catch(Exception e){
}
}
}
if(val.equals("Save")){
JFileChooser chooser = new JFileChooser();
int k = chooser.showSaveDialog(this);
if(k==JFileChooser.APPROVE_OPTION){
File file = chooser.getSelectedFile();
String path = file.getPath();
setTitle(path);
try{
FileWriter fw = new FileWriter(path,true);
PrintWriter pw = new PrintWriter(fw);
String data = ta.getText();
pw.println(data);
fw.close();
}
catch(Exception e){
}
}
}
if(val.equals("Exit")){
System.exit(0);
}
if(val.equals("Color")){
Color col = JColorChooser.showDialog(this,"Choose Color",Color.GREEN);
ta.setForeground(col);
}
}
}
class HomeProject{
public static void main(String args[]){
Home h = new Home();
}
}