Hi, everybody.
I'm just starting and I think this may be too simple of a thing to get response on , but I guess I wanted to get feedback from the ground up. I made a simple little class called JaVonNuemann, whose purpose is to basically duplicate itself. It takes a text file with all its own code, and copies it to a new file.
I also had it pop up as a GUI. Right now all the GUI does is allow you to click a button to make the copying happen.
If anybody has any advice as to a better way to do any of this, I'd appreciate hearing it. It's probably too simple a thing to ask about, but I thought I'd see just in case I did some small thing wrong.
Code:
public class JaVonNuemann implements ActionListener {
public JaVonNuemann () {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("JaVonNuemann");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Make a nice JPanel to hold our components.
JPanel main = new JPanel ();
frame.setContentPane(main);
//Add the label.
JLabel label = new JLabel("Make that thing!");
main.add(label);
//Make a Button
JButton button = new JButton ("Make it good!");
button.setActionCommand("Make It!");
button.addActionListener(this);
main.add(button);
//Display the window.
frame.pack();
frame.setVisible(true);
}
private static void createAndShowGUI() {
new JaVonNuemann ();
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void copy() throws IOException {
File inputFile = new File("JaVonNuemann.txt");
File outputFile = new File("JaVonNuemann1.java");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
public void actionPerformed(ActionEvent e) {
try {
copy();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}