Desktop Application
//this is a Desktop Application Notepad using
Frame
import
java.awt.*;
import
java.awt.event.*;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
class
notepad extends Frame implements ActionListener
{
MenuBar mbar;
Menu file,color,send,font,edit;
TextArea ta;
Font f1,f2;
String cp;
notepad()
{
setTitle("Notepad");
setSize(500,500);
setVisible(true);
f1=new
Font("courier",Font.BOLD,50);
f2=new Font("courier",Font.BOLD,25);
mbar=new MenuBar();
send=new Menu("Sent to");
send.add(email=new
MenuItem("Email"));
send.add(excel=new
MenuItem("Excel"));
file=new Menu("File");
file.add(nw=new MenuItem("New"));
file.add(op=new MenuItem("Open"));
file.add(sv=new MenuItem("Save"));
file.add(ds=new MenuItem("-"));
file.add(ex=new MenuItem("Exit"));
mbar.add(file);
edit=new Menu("Edit");
edit.add(cpy=new MenuItem("Copy"));
edit.add(pst=new
MenuItem("Paste"));
mbar.add(edit);
font=new Menu("Font");
font.add(fn1=new MenuItem("Title
font"));
font.add(fn2=new MenuItem("Text
font"));
mbar.add(font);
color=new Menu("Color");
color.add(pk=new MenuItem("Pink"));
color.add(yl=new
MenuItem("Yellow"));
mbar.add(color);
setMenuBar(mbar);
ta=new TextArea();
add(ta);
nw.addActionListener(this);
op.addActionListener(this);
sv.addActionListener(this);
ex.addActionListener(this);
pk.addActionListener(this);
yl.addActionListener(this);
fn1.addActionListener(this);
fn2.addActionListener(this);
cpy.addActionListener(this);
pst.addActionListener(this);
addWindowListener(new MywindowAdapater() );
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==nw)
{
ta.setText(" ");
}
if(ae.getSource()==ex)
{
System.exit(0);
}
if(ae.getSource()==pk)
{
ta.setBackground(Color.PINK);
ta.setForeground(Color.GREEN);
}
if(ae.getSource()==yl)
{
ta.setBackground(Color.yellow);
ta.setForeground(Color.red);
}
if(ae.getSource()==fn1)
{
ta.setFont(f1);
}
else if(ae.getSource()==fn2)
{
ta.setFont(f2);
}
if(ae.getSource()==sv)
{
FileDialog fd=new
FileDialog(this,"save",FileDialog.SAVE);
fd.setVisible(true);
String Path=fd.getDirectory()+fd.getFile();
System.out.println(Path);
try
{
FileOutputStream fout=new
FileOutputStream(Path);
String str=ta.getText();
byte buf[]=str.getBytes();
for(int i=0;i<buf.length;i++)
{
fout.write(buf[i]);
}
fout.close();
}
catch(Exception e){}
}
if(ae.getSource()==op)
{
FileDialog fd=new
FileDialog(this,"open",FileDialog.LOAD);
fd.setVisible(true);
String Path=fd.getDirectory()+fd.getFile();
System.out.println(Path);
try
{
FileInputStream fis=new
FileInputStream(Path);
int i;
while((i=fis.read())!=-1)
{
ta.append(""+(char)i);
}
}
catch(Exception e){}
}
if(ae.getSource()==cpy)
{
cp=ta.getSelectedText();
System.out.println(cp);
}
if(ae.getSource()==pst)
{
int pos=ta.getCaretPosition();
ta.insert(cp, pos);
}
}
public static void main(String[] args) throws
IOException
{
new notepad();
}
}
class
MywindowAdapater extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}