I just finished this project, thought I had it done, but on line 1, it's telling me, "incorrect Package". What am I missing here?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class JavaNotepad extends JFrame implements ActionListener, KeyListener
{
boolean txtChanged = false;
String fname = "";
JMenuBar mbar;
JMenu mnuFile, mnuEdit, mnuHelp;
JMenuItem fileNew, fileOpen, fileSave, fileExit;
JMenuItem editCut, editCopy, editPaste, editSelectAll, editDel;
ImageIcon iconNew, iconOpen, iconSave;
ImageIcon iconCut, iconCopy, iconPaste;
JButton bttnNew, bttnOpen, bttnSave;
JButton bttnCut, bttnCopy, bttnPaste;
JTextArea txtPad;
Container c;
JavaNotepad()
{
initComponents();
setTitle("Java Notepad");
setSize(500,550);
setVisible(true);
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WinHandler());
}
//reference content pane
void initComponents()
{
c = getContentPane();
//set layout manager
c.setLayout(new BorderLayout());
initMenu();
//create textarea
txtPad = new JTextArea();
Font f = new Font("Serif", Font.PLAIN, 12);
txtPad.setFont(f);
txtPad.addKeyListener(this);
//add txtPad in scrollpane
JScrollPane jscroll = new JScrollPane(txtPad);
//add scrollpane in window
c.add(jscroll, BorderLayout.CENTER);
}
void initMenu()
{
//create menubar
mbar = new JMenuBar();
//create menus
mnuFile = new JMenu("File");
mnuEdit = new JMenu("Edit");
//create menuitems
fileNew = new JMenuItem("New");
fileOpen= new JMenuItem("Open");
fileSave= new JMenuItem("Save");
fileExit = new JMenuItem("Exit");
editCut = new JMenuItem("Cut");
editCopy= new JMenuItem("Copy");
editPaste = new JMenuItem("Paste");
editSelectAll = new JMenuItem("Select All");
//add file menuitems
mnuFile.add(fileNew);
mnuFile.add(fileOpen);
mnuFile.add(fileSave);
mnuFile.add(fileExit);
//add edit menuitems
mnuEdit.add(editCut);
mnuEdit.add(editCopy);
mnuEdit.add(editPaste);
mnuEdit.addSeparator();
mnuEdit.add(editSelectAll);
//attach menus to menubar
mbar.add(mnuFile);
mbar.add(mnuEdit);
mbar.add(mnuHelp);
//attach menubar to window
setJMenuBar(mbar);
//attach actionlistener to file menuitems
fileNew.addActionListener(this);
fileOpen.addActionListener(this);
fileSave.addActionListener(this);
fileExit.addActionListener(this);
//attach actionlistener to edit menuitems
editCut.addActionListener(this);
editCopy.addActionListener(this);
editPaste.addActionListener(this);
editSelectAll.addActionListener(this);
}
//establish menu actions
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if(src.equals(fileNew))
{
newFile();
}
else if(src.equals(fileOpen))
{
openFile();
}
else if(src.equals(fileSave))
{
saveFile();
}
else if(src.equals(fileExit))
{
exitFile();
}
else if(src.equals(editCut))
{
txtPad.cut();
}
else if(src.equals(editCopy))
{
txtPad.copy();
}
else if(src.equals(editPaste))
{
txtPad.paste();
}
else if(src.equals(editSelectAll))
{
txtPad.selectAll();
}
}
//new file process
void newFile()
{
if(txtChanged == true)
{
int res;
res = JOptionPane.showConfirmDialog
(
this, //parent
"Do you wish to save changes?",
"File New",
JOptionPane.YES_NO_CANCEL_OPTION
);
if(res == JOptionPane.YES_OPTION)
{
saveFile();
}
else if(res == JOptionPane.CANCEL_OPTION)
{
return;
}
}
fname = "";
txtChanged = false;
txtPad.setText("");
}
//save process
void saveFile()
{
if(fname.equals(""))
{
JFileChooser jfc = new JFileChooser();
int res;
res = jfc.showSaveDialog(this);
if(res == jfc.APPROVE_OPTION)
{
fname = jfc.getSelectedFile().getAbsolutePath();
}
else
{
return;
}
}
try
{
FileWriter fw = new FileWriter(fname);
fw.write(txtPad.getText());
fw.flush();
fw.close();
txtChanged = false;
}
catch(Exception ex)
{
JOptionPane.showMessageDialog
(
this,
ex.getMessage(),
"Error Saving",
JOptionPane.ERROR_MESSAGE
);
}
}
//open process
void openFile()
{
int res;
JFileChooser jfc = new JFileChooser("c:/");
res = jfc.showOpenDialog(this);//parent window
if(res == jfc.APPROVE_OPTION)
{
fname = jfc.getSelectedFile().getAbsolutePath();
try
{
FileReader fr = new FileReader(fname);
BufferedReader br = new BufferedReader(fr);
//clear existing content of text area
txtPad.setText("");
String s;
while( (s = br.readLine()) != null )
{
txtPad.append(s);
txtPad.append("\n");
}
br.close();
fr.close();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog
(
this,
ex.getMessage(),
"Error Opening",
JOptionPane.ERROR_MESSAGE //icon
);
}
}
}
//exit process
void exitFile()
{
if(txtChanged == true)
{
int res;
res = JOptionPane.showConfirmDialog
(
this,
"Do you wish to save changes?",
"File Exit",
JOptionPane.YES_NO_CANCEL_OPTION
);
if(res == JOptionPane.YES_OPTION)
{
saveFile();
}
else if(res == JOptionPane.CANCEL_OPTION)
{
return;
}
}
dispose();
}
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
txtChanged = true;
}
class WinHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
exitFile();
}
}
public static void main(String args[])
{
JavaNotepad mp = new JavaNotepad();
}
}