Created
February 18, 2025 00:32
-
-
Save akishore15/64781768d222247411cc24ae3b2ac9c2 to your computer and use it in GitHub Desktop.
Code editor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.*; | |
public class JavaCodeEditor extends JFrame implements ActionListener { | |
private JTextArea textArea; | |
private JButton compileButton; | |
public JavaCodeEditor() { | |
setTitle("Java Code Editor"); | |
setSize(800, 600); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
textArea = new JTextArea(); | |
compileButton = new JButton("Compile"); | |
compileButton.addActionListener(this); | |
setLayout(new BorderLayout()); | |
add(new JScrollPane(textArea), BorderLayout.CENTER); | |
add(compileButton, BorderLayout.SOUTH); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
if (e.getSource() == compileButton) { | |
compileCode(); | |
} | |
} | |
private void compileCode() { | |
try { | |
FileWriter writer = new FileWriter("MyCode.java"); | |
writer.write(textArea.getText()); | |
writer.close(); | |
ProcessBuilder pb = new ProcessBuilder("javac", "MyCode.java"); | |
pb.redirectErrorStream(true); | |
Process process = pb.start(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | |
String line; | |
StringBuilder output = new StringBuilder(); | |
while ((line = reader.readLine()) != null) { | |
output.append(line).append("\n"); | |
} | |
JOptionPane.showMessageDialog(this, output.toString(), "Compilation Output", JOptionPane.INFORMATION_MESSAGE); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
SwingUtilities.invokeLater(() -> { | |
JavaCodeEditor editor = new JavaCodeEditor(); | |
editor.setVisible(true); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment