Java Code for creating a Notepad-like Application
//Notepad in Java using JFrame and ActionListener
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class NotepadProject extends JFrame implements ActionListener {
JMenuBar bar;
JMenu file, edit, format, help;
JMenuItem new1, open, save, close, exit;
JMenuItem cut, copy, paste, find;
JCheckBoxMenuItem word_wrap;
JMenuItem color;
JMenuItem about;
JTextArea area;
public NotepadProject() {
area = new JTextArea();
add("Center", new JScrollPane(area));
bar = new JMenuBar();
setJMenuBar(bar);
file = new JMenu("File");
edit = new JMenu("Edit");
format = new JMenu("Format");
help = new JMenu("Help");
bar.add(file);
bar.add(edit);
bar.add(format);
bar.add(help);
file.setMnemonic('F');
edit.setMnemonic('E');
format.setMnemonic('o');
help.setMnemonic('H');
new1 = new JMenuItem("New");
open = new JMenuItem("Open");
save = new JMenuItem("Save");
close = new JMenuItem("Close");
exit = new JMenuItem("Exit");
file.add(new1);
file.add(open);
file.add(save);
file.add(close);
file.add(exit);
cut = new JMenuItem("Cut");
copy = new JMenuItem("Copy");
paste = new JMenuItem("Paste");
find = new JMenuItem("Find");
edit.add(cut);
edit.add(copy);
edit.add(paste);
edit.add(find);
word_wrap = new JCheckBoxMenuItem("Word wrap");
color = new JMenuItem("Color");
format.add(word_wrap);
format.add(color);
about = new JMenuItem("About");
help.add(about);
new1.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
close.addActionListener(this);
exit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
find.addActionListener(this);
word_wrap.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
area.setLineWrap(!area.getLineWrap());
}
});
color.addActionListener(this);
about.addActionListener(this);
setSize(500, 500);
setVisible(true);
setResizable(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if(obj == new1) {
new NotepadProject();
}
if(obj == open) {
JFileChooser fc = new JFileChooser();
fc.showDialog(null, "Open");
File f = fc.getSelectedFile();
try {
FileReader ff = new FileReader(f);
int c;
String s="";
while((c = ff.read())!= -1)
s = s + (char)c;
ff.close();
area.setText(s);
}
catch(Exception ex){
}
}
if(obj == save) {
JFileChooser fc = new JFileChooser();
fc.showDialog(null, "Save");
File f = fc.getSelectedFile();
try {
FileWriter ff = new FileWriter(f);
String s = area.getText();
char buffer[] = s.toCharArray();
ff.write(buffer);
ff.close();
}
catch(Exception ex) {
}
}
if(obj == close) {
JFrame f = this;
f.dispose();
}
if(obj == exit) {
System.exit(0);
}
if(obj == cut) {
area.cut();
}
if(obj == copy) {
area.copy();
}
if(obj == paste) {
area.paste();
}
if(obj == find) {
String s = JOptionPane.showInputDialog(null, "Enter text to find:");
int startpos;
if(area.getSelectedText() == null) {
startpos = 0;
}
else
startpos = area.getSelectionStart()+1;
int index = area.getText().indexOf(s, startpos);
if(index!=-1) {
area.setSelectionStart(index);
area.setSelectionEnd(index+s.length());
area.requestFocus();
}
}
if(obj == color) {
JColorChooser cc = new JColorChooser();
Color c1 = cc.showDialog(this, "Color Chooser", area.getForeground());
if(c1!=null)
area.setForeground(c1);
}
if(obj == about) {
String st = "<html><body>Mero Notepad v 1.0<br/>©Lok</body></html>";
JOptionPane.showMessageDialog(null, st);
}
}
public static void main(String []args) {
new NotepadProject();
}
}
No comments:
Post a Comment