answersLogoWhite

0

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package simpletexteditor;

/**

*

* @author Ibrahim Alwazir

*/

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

class MyPad extends JFrame implements ActionListener, KeyListener

{

boolean txtChanged = false;

String fname = "";

JMenuBar mbar;

JMenu mnuFile, mnuEdit;

JMenuItem fileNew, fileOpen, fileSave, fileExit;

JMenuItem editCut, editCopy, editPaste, editSelectAll, editDel;

JToolBar tlbr;

ImageIcon iconNew, iconOpen, iconSave;

ImageIcon iconCut, iconCopy, iconPaste;

JButton bttnNew, bttnOpen, bttnSave;

JButton bttnCut, bttnCopy, bttnPaste;

JCheckBoxMenuItem wordWrap;

JTextArea txtPad;

Container c;

MyPad()

{

initComponents();

setTitle("Simple Text Editor");

setSize(400,300);

setVisible(true);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

addWindowListener(new WinHandler());

}

void initComponents()

{

//get a handle (reference) to the

//content pane of window

c = getContentPane();

//set BorderLayout Manager to arrange components in content pane

c.setLayout(new BorderLayout());

initMenu();

//create textarea

txtPad = new JTextArea();

Font f = new Font("Arial", Font.PLAIN, 20);

txtPad.setFont(f);

txtPad.addKeyListener(this);

txtPad.setLineWrap(false);

/* Sets word wrap style to false. */

txtPad.setWrapStyleWord(false);

//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");

wordWrap = new JCheckBoxMenuItem("word wrap");

editCut = new JMenuItem("Cut");

editCopy= new JMenuItem("Copy");

editPaste = new JMenuItem("Paste");

editSelectAll = new JMenuItem("Select All");

editDel= new JMenuItem("Delete");

//add menuitems in menus

mnuFile.add(fileNew);

mnuFile.add(fileOpen);

mnuFile.add(fileSave);

mnuFile.add(fileExit);

mnuEdit.add(editCut);

mnuEdit.add(editCopy);

mnuEdit.add(editPaste);

mnuEdit.addSeparator();

mnuEdit.add(editSelectAll);

mnuEdit.add(editDel);

mnuEdit.add(wordWrap);

//attach menus to menubar

mbar.add(mnuFile);

mbar.add(mnuEdit);

//attach menubar to window

setJMenuBar(mbar);

//attach actionlister to menuitems

fileNew.addActionListener(this);

fileOpen.addActionListener(this);

fileSave.addActionListener(this);

fileExit.addActionListener(this);

editCut.addActionListener(this);

editCopy.addActionListener(this);

editPaste.addActionListener(this);

editSelectAll.addActionListener(this);

editDel.addActionListener(this);

wordWrap.addActionListener(this);

}

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(wordWrap))

{

/* Calls the warpFile() method. */

wrapFile();

}

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();

}

else if(src.equals(editDel))

{

//replace selected content with a blank

txtPad.replaceSelection("");

}

}//end of actionPerformed

void newFile()

{

if(txtChanged true)

{

/* Sets the line and word wrap style to TRUE. */

txtPad.setLineWrap(true);

txtPad.setWrapStyleWord(true);

}

else

{

/* Sets the line and word wrap style to FALSE. */

txtPad.setLineWrap(false);

txtPad.setWrapStyleWord(false);

}

}

public void keyPressed(KeyEvent e)

{

//System.out.println("KP");

}

public void keyReleased(KeyEvent e)

{

//System.out.println("KR");

}

public void keyTyped(KeyEvent e)

{

txtChanged = true;

}

class WinHandler extends WindowAdapter

{

public void windowClosing(WindowEvent e)

{

exitFile();

}

}

public static void main(String args[])

{

MyPad mp = new MyPad();

}

}

class HelpDlg extends JDialog

{

public HelpDlg(MyPad m)

{

//MyPad object is the parent window of this dialog

super(m,true); //register it

//m is the MYPad ref that will be the owner

//true means current dialog will be a modal window

setSize(200, 150);

setVisible(true);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

}

}

User Avatar

Wiki User

14y ago

What else can I help you with?