import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
public class Application extends JFrame implements ActionListener {
JTextField text = new JTextField("Input",6);
JButton save = new JButton("Save");
JButton load = new JButton("Load");
JTextField output = new JTextField("0",6);
public Application() {
super("Sample Program");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add Listeners
save.addActionListener(this);
load.addActionListener(this);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
setLayout(flow);
//add components
add(text);
add(save);
add(load);
add(output);
setVisible(true);
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == save) {
Writer();
} else if (source == load) {
Reader();
}
}
void Writer() {
Message mess = new Message();
String writetext = text.getText();
mess.writeMessage(writetext);
try {
FileOutputStream fo = new FileOutputStream("Message1.obj");
ObjectOutputStream oo = new ObjectOutputStream(fo);
oo.writeObject(mess);
oo.close();
} catch (IOException e) {
System.out.println("Error - " + e.toString());
}
}
void Reader() {
try {
FileInputStream fi = new FileInputStream("Message1.obj");
ObjectInputStream oi = new ObjectInputStream(fi);
Message mess = (Message) oi.readObject();
output.setText(mess.text);
oi.close();
} catch (Exception e) {
System.out.println("Error - " + e.toString());
}
}
public static void main(String[] arguments) {
new Application();
}
}
class Message implements Serializable {
String text;
void writeMessage(String inText) {
text = inText;
}
}