import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class MyNotepad extends JFrame
{
JMenuBar topBar ;
JMenu menuFile , menuEdit , menuHelp ;
JMenuItem itmNew , itmOpen , itmSave , itmClose , itmCopy , itmCut ,
itmPaste , itmAbout ;
JTextArea tAreaEditor ;
JFileChooser myFileChooser;
Container c ;
JTabbedPane myTabbedPane = new JTabbedPane();
//ImageIcon icon = createImageIcon("images/middle.gif");
MyNotepad ()
{
super("My Notepad ");
c = getContentPane();
c.setLayout(new FlowLayout());
//creates the new file chooser
myFileChooser = new JFileChooser();
topBar = new JMenuBar ();
// adding contents for the menu --> file
menuFile = new JMenu ("File");
menuFile .setMnemonic(KeyEvent.VK_F);
itmNew = new JMenuItem ("New");
itmOpen = new JMenuItem ("Open");
itmSave = new JMenuItem ("Save");
itmClose = new JMenuItem ("Close");
menuFile .add(itmNew );
menuFile .add(itmOpen );
menuFile .add( itmSave );
menuFile .add(itmClose );
topBar .add(menuFile );
//*********************************************
// adding contents for the menu --> Edit
menuEdit = new JMenu ("Edit");
menuEdit .setMnemonic(KeyEvent.VK_E);
itmCut = new JMenuItem ("Cut");
itmCopy = new JMenuItem ("Copy");
itmPaste = new JMenuItem ("Paste");
menuEdit .add(itmCut );
menuEdit .add(itmCopy );
menuEdit .add(itmPaste );
topBar .add(menuEdit );
//*********************************************
menuHelp = new JMenu ("Help");
itmAbout = new JMenuItem ("About");
menuHelp .add(itmAbout );
topBar .add(menuHelp );
//*********************************************
//ADDING ITEM LISTENERS
//----------------------------------------
MyMenuListener mm1 =new MyMenuListener ();
itmNew .addActionListener(mm1);
itmOpen .addActionListener(mm1);
// edit listeners
itmCut.addActionListener(mm1);
itmPaste.addActionListener(mm1);
itmCopy.addActionListener(mm1);
// Help listeners
itmAbout.addActionListener(mm1);
c.add(topBar);
//_________________________________________________
//add the first tabbed pane
//_________________________________________________
//JScrollPane myScrollPane = new JScrollPane(tAreaEditor );
//c.add( myScrollPane );
JComponent panel1 = mm1.makeTextPanel();
myTabbedPane.addTab(" Document 1", panel1);
c.add(myTabbedPane);
}
private class MyMenuListener implements ActionListener
{
int i=1;
public void actionPerformed(ActionEvent e)
{
if( e.getSource() == itmNew )
{
i++;
JComponent panel1 = makeTextPanel();
myTabbedPane.addTab(" Document "+i, panel1);
}
if( e.getSource() == itmOpen )
myFileChooser.showOpenDialog(c) ;
if( e.getSource() == itmCut )
tAreaEditor.cut();
if( e.getSource() == itmCopy )
tAreaEditor.copy();
if( e.getSource() == itmPaste )
tAreaEditor.paste();
if( e.getSource() == itmAbout )
JOptionPane.showMessageDialog(null, "name: Justin"+
"\n\nLicense:GNU GPL"+
"\n\ne-mail: just...@gmail.com");
}
protected JComponent makeTextPanel()
{
JPanel panel = new JPanel();
JTextArea tAreaEditor = new JTextArea (20,40) ;
tAreaEditor .setLineWrap(true) ;
JScrollPane myScrollPane = new JScrollPane(tAreaEditor );
panel.add( myScrollPane );
return panel;
}
}
}
class MyNotepadTest
{
public static void main(String args[])
{
MyNotepad m1 = new MyNotepad ();
m1.setSize(480,400);
m1.setVisible(true);
m1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}