Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package notepad;
- /**
- * @author toffe boy Aamir
- */
- //@URL aamir-4u.blogspot.com
- import java.util.Date;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.FlowLayout;
- import java.awt.Toolkit;
- import java.awt.datatransfer.Clipboard;
- import java.awt.datatransfer.StringSelection;
- import java.awt.datatransfer.Transferable;
- import static java.awt.event.InputEvent.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import javax.swing.ImageIcon;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.KeyStroke;
- import javax.swing.JScrollPane;
- import javax.swing.UIManager;
- /**
- *
- * @author toffe boy Aamir
- */
- public final class Notepad extends JFrame implements ActionListener{
- ImageIcon iconclose =new ImageIcon("src/img/cancel.png");
- ImageIcon iconopen =new ImageIcon("src/img/briefcase.png");
- ImageIcon iconsave =new ImageIcon("src/img/door_out.png");
- ImageIcon helpicon = new ImageIcon("src/img/Help.png");
- JLabel watch;
- JMenuBar mb;
- JScrollPane scp = new JScrollPane();
- JMenu file,edit,view,about;
- JMenuItem op,sa,cl,cop,pas,cut,help;
- JTextArea out;
- JPanel p = new JPanel();
- public static void main(String[] args) throws InterruptedException {
- Notepad note= new Notepad();
- note.setSize(640,480);
- note.setVisible(true);
- note.setTitle("Pad Pad");
- note.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setDefaultLookAndFeelDecorated(true);
- }
- Notepad() throws InterruptedException {
- try{
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
- }
- catch(Exception e){
- System.out.println(e);
- }
- Container c = getContentPane();
- c.add(p);
- p.setLayout(new BorderLayout());
- //Show Date
- //SimpleDateFormat sdf = new SimpleDateFormat("E dd-MM-yyyy 'at' hh:mm:ss: a ");
- //Date date = new Date();
- watch = new JLabel(time());
- watch.setLayout(new FlowLayout());
- watch.setBorder(new javax.swing.border.LineBorder(Color.WHITE, 5));
- p.add("South",watch);
- //Scroll bar
- p.add(scp);
- scp.setBackground(Color.BLACK);
- //Area of out put/input
- out = new JTextArea();
- scp.getViewport().add(out);
- //MenuBar
- mb = new JMenuBar();
- p.add("North",mb);
- //Menu
- file = new JMenu("File");
- file.setMnemonic('F');
- edit = new JMenu("Edit");
- view = new JMenu("View");
- about= new JMenu("About");
- mb.add(file);
- mb.add(edit);
- mb.add(view);
- mb.add(about);
- //File SubMenu Items
- op = new JMenuItem("Open");
- op.setAccelerator(KeyStroke.getKeyStroke('O',CTRL_DOWN_MASK));
- op.setMnemonic('o');
- sa = new JMenuItem("Save");
- sa.setAccelerator(KeyStroke.getKeyStroke('S',CTRL_DOWN_MASK));
- sa.setMnemonic('s');
- cl = new JMenuItem("Exit");
- cl.setAccelerator(KeyStroke.getKeyStroke('X',ALT_DOWN_MASK));
- cl.setMnemonic('x');
- file.add(op);
- file.add(sa);
- file.addSeparator();
- file.add(cl);
- //Actions on Buttons
- op.addActionListener(this);
- op.setIcon(iconopen);
- sa.addActionListener(this);
- sa.setIcon(iconsave);
- cl.addActionListener(this);
- cl.setIcon(iconclose);
- //Edit Sub Menu
- cop = new JMenuItem("Copy");
- cut = new JMenuItem("Cut");
- pas = new JMenuItem("Paste");
- edit.add(cut);
- edit.add(cop);
- edit.add(pas);
- //Action on Buttons
- cut.addActionListener(this);
- cop.addActionListener(this);
- pas.addActionListener(this);
- // About Menu
- help = new JMenuItem("Help");
- help.setIcon(helpicon);
- about.add(help);
- help.addActionListener(this);
- }
- //Method for open File
- public void open(){
- //file Chooser
- JFileChooser choose = new JFileChooser();
- // display box
- int i = choose.showOpenDialog(this);
- if (i==JFileChooser.APPROVE_OPTION){
- File f =choose.getSelectedFile();
- String filename = f.getPath();
- String str = "";
- String str1="";
- //Reading File
- try{
- BufferedReader br = new BufferedReader(new FileReader(filename));
- //Read File
- while((str=br.readLine())!=null)
- str1 +=str+"\n";
- out.setText(str1);
- br.close();
- }
- catch(Exception e){}
- }
- }
- //Method for Save File
- public void save(){
- JFileChooser choose = new JFileChooser();
- int i = choose.showSaveDialog(this);
- if (i==choose.APPROVE_OPTION){
- File f = choose.getSelectedFile();
- String saved = f.getPath()+".txt";
- try{
- BufferedWriter savefile = new BufferedWriter(new FileWriter(saved));
- savefile.write(this.out.getText());
- //out is the object of text Area where user writting text
- savefile.flush();
- savefile.close();
- }
- catch(Exception e){System.out.println(e);}
- }
- }
- public void copyme(String writeMe) {
- // get the system clipboard
- Clipboard systemClipboard =
- Toolkit
- .getDefaultToolkit()
- .getSystemClipboard();
- // set the textual content on the clipboard to our
- // transferable object
- // we use the
- Transferable transferableText =
- new StringSelection(writeMe);
- systemClipboard.setContents(
- transferableText,
- null);
- }
- public void pasteme(){
- }
- //Action Performe
- public void actionPerformed(ActionEvent e){
- if (op.isArmed()){
- this.open();
- }
- else if(cl.isArmed())
- {
- System.exit(0);
- }
- else if (sa.isArmed()){
- this.save();
- }
- else if (cop.isArmed()){
- this.copyme(out.getSelectedText());
- }
- else if (pas.isArmed()){
- this.pasteme();
- }
- else if (help.isArmed()){
- //create a new class to show something about you
- About.AboutI();
- }
- }
- public String time() throws InterruptedException{
- Thread.sleep(1000);
- return new Date().toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement