C:\PROGRA~1\Java\JDK15~1.0\bin>javac Editor.java
Note: Editor.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Below is my code with no errors but its not running.........
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class Editor extends Frame implements ActionListener
- {
- TextArea textArea = new TextArea();
- //Set up the menu on the default CTOR.
- Editor()
- {
- super("Text AWT Editor");
- setLayout(new BorderLayout());
- add("Center", textArea);
- Menu menu = new Menu("File");
- menu.add(makeMenuItem("Open"));
- menu.add(makeMenuItem("Save"));
- menu.add(makeMenuItem("Quit"));
- MenuBar menuBar = new MenuBar();
- menuBar.add(menu);
- setMenuBar(menuBar);
- pack();
- }
- private MenuItem makeMenuItem(String name)
- {
- MenuItem m = new MenuItem(name);
- m.addActionListener(this);
- return m;
- }
- public static void main(String[] s)
- {
- new Editor().show();
- }
- public void actionPerformed(ActionEvent e)
- {
- String command = e.getActionCommand();
- if (command.equals("Quit"))
- dispose();
- else if (command.equals("Open"))
- openFile();
- else if (command.equals("Save"))
- saveFile();
- }
- private void openFile()
- {
- //Show the Open File dialog box to the user.
- FileDialog fd = new FileDialog(this, "Open File", FileDialog.LOAD);
- fd.show();
- //Get the file path.
- StringBuffer sbPath = new StringBuffer(fd.getDirectory());
- sbPath.append("\\");
- String fileName = fd.getFile();
- //Cancel if null:
- if (fileName == null)
- {
- return;
- }
- else sbPath.append(fileName);
- // Open and fill the input stream; paint the TextArea.
- try
- {
- FileInputStream fs = new FileInputStream(sbPath.ToString());
- byte[] data = new byte [ sbPath.length() ];
- fs.read(data);
- textArea.setText(new String(data));
- }
- catch (IOException e)
- {
- textArea.setText(e.toString());
- }
- }
- private void saveFile()
- {
- try
- {
- // Show the dialog box to the user.
- FileDialog fd = new FileDialog(this, "Save File", FileDialog.SAVE);
- fd.show();
- //Get the path and file name.
- StringBuffer sbPath = new StringBuffer(fd.getDirectory());
- sbPath.append("\\");
- // Get the file name to create:
- sbPath.append(fd.getFile());
- FileOutputStream fo = new FileOutputStream(sbPath.ToString());
- // Parse the data:
- String strdata = textArea.getText();
- byte[] data = new byte [strdata.length()];
- for (int idx = 0; idx < strdata.length(); idx++)
- {
- data[idx] = (byte)strdata.charAt(idx);
- }
- // Write the array to the file.
- fo.write(data);
- }
- catch (IOException ex)
- {
- textArea.setText(ex.toString());
- }
- }
- }