import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class VisualKompilasi extends JFrame {
private JLabel lblsource=new JLabel("Source Code");
private JTextField txfsource=new JTextField();
private TextArea txahasil=new TextArea();
private JButton btnopen=new JButton("...");
private JButton btncompile=new JButton("Compile");
private JButton btnedit=new JButton("Edit");
private JButton btnclear=new JButton("Clear");
JFileChooser fc=new JFileChooser();
File file;
public VisualKompilasi() {
setTitle("Simple Java Compiler");
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w=400;
int h=250;
int l = (dim.width - w)/2;
int t = (dim.height - h)/2;
setLocation(l,t);
setSize(w,h);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void komponenVisual() {
getContentPane().setLayout(null);
getContentPane().add(lblsource);
getContentPane().add(txfsource);
getContentPane().add(txahasil);
getContentPane().add(btnopen);
getContentPane().add(btncompile);
getContentPane().add(btnedit);
getContentPane().add(btnclear);
lblsource.setBounds(10,10,80,20);
txfsource.setBounds(90,10,250,20);
btnopen.setBounds(350,10,30,20);
btncompile.setBounds(10,40,100,20);
btnedit.setBounds(130,40,100,20);
btnclear.setBounds(250,40,100,20);
txahasil.setBounds(10,70,380,150);
txahasil.setEditable(false);
setVisible(true);
}
public void aksiReaksi() {
btnopen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int returnVal=fc.showOpenDialog(VisualKompilasi.this);
if (returnVal==JFileChooser.APPROVE_OPTION) {
file=fc.getSelectedFile();
txfsource.setText(file.getAbsolutePath());
}
}
});
btnedit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("notepad "+file.getAbsolutePath());
} catch (Exception ex) {
try {
Runtime.getRuntime().exec("gedit "+file.getAbsolutePath());
} catch (Exception ext) {
}
}
}
});
btncompile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Process pid = Runtime.getRuntime().exec("javac \""+file.getAbsolutePath()+"\"");
BufferedReader in = new BufferedReader(new InputStreamReader(pid.getErrorStream()));
String hasil;
txahasil.setText("Hasil :\n");
while((hasil=in.readLine())!=null) {
txahasil.append(hasil);
txahasil.append(System.getProperty("line.separator"));
}
if ((hasil=in.readLine())==null) {
txahasil.append("Source "+file.getName()+" berhasil dikompilasi");
}
in.close();
} catch (Exception ep) {
JOptionPane.showMessageDialog(null,ep);
}
}
});
btnclear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txfsource.setText("");
txahasil.setText("");
txfsource.requestFocus(true);
}
});
}
public static void main(String args[]) {
VisualKompilasi vk=new VisualKompilasi();
vk.komponenVisual();
vk.aksiReaksi();
}
}
My next challenge is to change the program into a simple compiler like NetBeans or Ge, the way is to make it through Jtextarea, if anyone can help??

New Topic/Question
Reply





MultiQuote








|