import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Editor extends Frame implements ActionListener {
TextArea textArea = new TextArea();
Editor() {
super("Editor");
setLayout( new BorderLayout() );
add("Center", textArea);
Menu menu = new Menu ("File");
menu.add ( makeMenuItem ("Load") );
menu.add ( makeMenuItem ("Save") );
menu.add ( makeMenuItem ("Quit") );
MenuBar menuBar = new MenuBar();
menuBar.add ( menu );
setMenuBar( menuBar );
pack();
}
public void actionPerformed( ActionEvent e ) {
String command = e.getActionCommand();
if ( command.equals("Quit") )
dispose();
else if ( command.equals("Load") )
loadFile();
else if ( command.equals("Save") )
saveFile();
}
private void loadFile () {
FileDialog fd = new FileDialog( this, "Load File", FileDialog.LOAD );
fd.show();
String file = fd.getFile();
if ( file == null ) // Cancel
return;
try {
FileInputStream fis = new FileInputStream ( fd.getFile() );
byte [] data = new byte [ fis.available() ];
fis.read( data );
textArea.setText( new String( data ) );
} catch ( IOException e ) {
textArea.setText( "Could not load file..." );
}
}
private void saveFile() {
FileDialog fd = new FileDialog( this, "Save File", FileDialog.SAVE );
fd.show();
// Save file data...
}
private MenuItem makeMenuItem( String name ) {
MenuItem m = new MenuItem( name );
m.addActionListener( this );
return m;
}
public static void main(String[] s) {
new Editor().show();
}
}
|