-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyEditor.java
More file actions
166 lines (165 loc) · 3.4 KB
/
MyEditor.java
File metadata and controls
166 lines (165 loc) · 3.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class MyEditor implements ActionListener
{
JFrame jf;
JLabel jl;
JTextField jtf;
JTextArea jta,jta1;
JButton jbcompile,jbrun;
JScrollPane jsp,jsp1;
Runtime r;
String str="";
String fname="";
String result="";
String result1="";
MyEditor()
{
jf=new JFrame("My Editor");
jf.setLayout(null);
jl=new JLabel("Enter java class name");
jl.setBounds(20,20,130,25);
jtf=new JTextField();
jtf.setBounds(180,20,230,25);
jta=new JTextArea(50,50);
jta1=new JTextArea(50,50);
jta.addFocusListener(new MyFocusListener(this));
jta.setFont(new Font("arinda",Font.PLAIN,15));
jta1.setFont(new Font("arinda",Font.PLAIN,15));
jsp=new JScrollPane(jta);
jsp1=new JScrollPane(jta1);
jsp.setBounds(50,60,320,150);
jsp1.setBounds(50,270,320,150);
jf.add(jsp);
jf.add(jsp1);
jbcompile=new JButton("compile");
jbrun=new JButton("run");
jbcompile.setBounds(100,230,80,25);
jbrun.setBounds(280,230,80,25);
jf.add(jl);
jf.add(jtf);
r=Runtime.getRuntime();
jf.add(jbcompile);
jf.add(jbrun);
jbcompile.addActionListener(this);
jbrun.addActionListener(this);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(550,550);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbcompile)
{
str="";
if(!jtf.getText().equals(""))
{
try
{
fname=jtf.getText().trim()+".java";
FileWriter fw=new FileWriter(fname);
String s1=jta.getText();
PrintWriter pw=new PrintWriter(fw);
pw.println(s1);
pw.flush();
Process error=r.exec("C:\\Program Files\\Java\\jdk1.8.0_171\\bin\\javac.exe "+fname);
BufferedReader err=new BufferedReader(new InputStreamReader(error.getErrorStream()));
while(true)
{
String temp=err.readLine();
if(temp!=null)
{
result+=temp;
result+="\n";
}
else
{
break;
}
}
if(result.equals(""))
{
jta1.setText("compilation successfull"+fname);
err.close();
}
else
{
jta1.setText(result);
}
}
catch(Exception e1)
{
System.out.println(e1);
}
}
else
{
jta1.setText("please enter the java program name");
}
}
else if(e.getSource()==jbrun)
{
int start=0;
try
{
String fn=jtf.getText().trim();
Process p=r.exec("C:\\Program Files\\Java\\jdk1.8.0_171\\bin\\java.exe "+fn);
BufferedReader output=new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error=new BufferedReader(new InputStreamReader(p.getErrorStream()));
while(true)
{
String temp=output.readLine();
if(temp!=null)
{
result+=temp;
result+="\n";
}
else
{
break;
}
}
while(true)
{
String temp=error.readLine();
if(temp!=null)
{
result1+=temp;
result1+="\n";
}
else
{
break;
}
}
output.close();
error.close();
jta1.setText(result+"\n"+result1);
}
catch(Exception e2)
{
System.out.println(e2);
}
}
}
public static void main(String... s)
{
new MyEditor();
}
}
class MyFocusListener extends FocusAdapter
{
MyEditor e;
MyFocusListener(MyEditor e)
{
this.e=e;
}
public void focusGained(FocusEvent fe)
{
String str=e.jtf.getText().trim();
e.jta.setText("public class "+str+"\n"+"{"+"\n"+"public static void main(String... s)"+"\n"+"{"+"\n"+" "+"\n"+"}"+"\n"+"}");
}
}