import java.util.*;
import java.text.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Notepad extends Frame
{
private Panel toolbar = new Panel();
private Panel statusbar = new Panel();
private TextArea text = new TextArea();
private Button openButton = new Button("Open");
private Button saveasButton = new Button("SaveAs");
private TextField statusFeild = new TextField();
private TextField clockFeild = new TextField();
// private TextField helpFeild = new TextField();
private static SimpleDateFormat clockformat = new SimpleDateFormat("dd-MMM-yyyy,HH:mm:ss");
private FileDialog fd = new FileDialog(this);
private File currentFile;
public Notepad()
{
super("Untitled");
add(toolbar,"North");
add(statusbar,"South");
add(text,"Center");
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
toolbar.add(openButton);
toolbar.add(saveasButton);
statusbar.setLayout(new GridLayout());
statusbar.add(statusFeild);
// statusbar.add(helpFeild);
statusbar.add(clockFeild);
// statusFeild.setEnabled(false);
//helpFeild.setEnabled(False);
//clockFeild.setEnabled(False);
Thread th = new Thread(new Runnable()
{
public void run()
{
Date d = new Date();
while(true)
{
clockFeild.setText(clockformat.format(d));
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
d.setTime(System.currentTimeMillis());
}
}
});
th.setDaemon(true);
th.start();
openButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Select File to open");
fd.setMode(FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileReader fr = new FileReader(currentFile);
char ch[] = new char[(int)currentFile.length()];
fr.read(ch);
text.setText(new String(ch));
fr.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
saveasButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Saveas");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileWriter fw = new FileWriter(currentFile);
fw.write(text.getText());
fw.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
}
public static void main(String args [])
{
Notepad np = new Notepad();
np.setBounds(0,0,500,500);
np.setVisible(true);
}
}
import java.text.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Notepad extends Frame
{
private Panel toolbar = new Panel();
private Panel statusbar = new Panel();
private TextArea text = new TextArea();
private Button openButton = new Button("Open");
private Button saveasButton = new Button("SaveAs");
private TextField statusFeild = new TextField();
private TextField clockFeild = new TextField();
// private TextField helpFeild = new TextField();
private static SimpleDateFormat clockformat = new SimpleDateFormat("dd-MMM-yyyy,HH:mm:ss");
private FileDialog fd = new FileDialog(this);
private File currentFile;
public Notepad()
{
super("Untitled");
add(toolbar,"North");
add(statusbar,"South");
add(text,"Center");
toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
toolbar.add(openButton);
toolbar.add(saveasButton);
statusbar.setLayout(new GridLayout());
statusbar.add(statusFeild);
// statusbar.add(helpFeild);
statusbar.add(clockFeild);
// statusFeild.setEnabled(false);
//helpFeild.setEnabled(False);
//clockFeild.setEnabled(False);
Thread th = new Thread(new Runnable()
{
public void run()
{
Date d = new Date();
while(true)
{
clockFeild.setText(clockformat.format(d));
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
d.setTime(System.currentTimeMillis());
}
}
});
th.setDaemon(true);
th.start();
openButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Select File to open");
fd.setMode(FileDialog.LOAD);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileReader fr = new FileReader(currentFile);
char ch[] = new char[(int)currentFile.length()];
fr.read(ch);
text.setText(new String(ch));
fr.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
saveasButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
fd.setTitle("Saveas");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
if(fd.getFile() == null)
return;
currentFile = new File(fd.getDirectory(),fd.getFile());
try
{
FileWriter fw = new FileWriter(currentFile);
fw.write(text.getText());
fw.close();
setTitle(currentFile.getName());
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
});
}
public static void main(String args [])
{
Notepad np = new Notepad();
np.setBounds(0,0,500,500);
np.setVisible(true);
}
}
No comments:
Post a Comment