Friday, 31 August 2012

menu


   import java.io.*;
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

    public class Menu extends JFrame implements ActionListener
   {
         Container c;
         JTextArea ta;
         JMenuBar mbar1;
         JMenu file,edit;
         JMenuItem fnew,fopen,fsave,fexit,ecut,ecopy,epaste,eclear;
         JLabel l1;
         JToolBar sbar,tbar;
         JButton b1,b2;
         JProgressBar pbar;
       
         public Menu()
        {
             super("WINDOW");
             setSize(500,400);
             setDefaultCloseOperation(0);
             setLocation(50,50);
             c=getContentPane();
            ta=new JTextArea(10,40);
            pbar=new JProgressBar();
            Font f1=new Font("Times New Roman",1,25);
            pbar.setFont(f1);
            pbar.setMinimum(1);
            pbar.setMaximum(200);
            pbar.setStringPainted(true);
            pbar.setValue(50);
            c.add(pbar);
            ta.setFont(f1);
           ta.setLineWrap(true);
           ta.setWrapStyleWord(true);
           c.add(new JScrollPane(ta));
           mbar1=new JMenuBar();
           file=new JMenu("File");
           file.setMnemonic('f');
           fnew=new JMenuItem("New");
           fopen=new JMenuItem("Open");
           ImageIcon ic1=new ImageIcon("sunlogo62x30.gif");
           fsave=new JMenuItem("Save",ic1);
            fexit=new JMenuItem("Exit");
           fexit.setMnemonic('X');
           file.add(fnew);          file.add(fopen);
           file.add(fsave);     file.addSeparator();
          file.add(fexit);      mbar1.add(file);
          edit=new JMenu("Edit");
          ecut=new JMenuItem("Cut");
          ecopy=new JMenuItem("Copy");
          epaste=new JMenuItem("Paste");
          eclear=new JMenuItem("Clear");
          edit.add(ecut);    edit.add(ecopy);
         edit.add(epaste);  edit.add(eclear);
         mbar1.add(edit);
         setJMenuBar(mbar1);
         tbar=new JToolBar();
         sbar=new JToolBar();
         sbar.setFloatable(false);
         l1=new JLabel("READY");
         b1=new JButton(new ImageIcon("sunlogo62x30.gif"));
         b2=new JButton(new ImageIcon("save.gif"));
          tbar.add(b1);         tbar.add(b2);
         sbar.add(l1);
         c.add(tbar,BorderLayout.NORTH);
         c.add(sbar,BorderLayout.SOUTH);
          fexit.addActionListener(this);
          fopen.addActionListener(this);
          fsave.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
      }
           

           
            public void actionPerformed(ActionEvent ae)
     {
       if(ae.getSource()==fnew)
      { 
          ta.setText(" ");   
      }
    
             if(ae.getSource()==fopen||ae.getSource()==b1)
                  {
           JFileChooser jfc=new JFileChooser();
           File d=new File("D:/jdk1.4/bin");
           jfc.setCurrentDirectory(d);
           jfc.showOpenDialog(this);
          File f=jfc.getSelectedFile();
          setTitle(f.toString());
          try
          {
             FileReader fr=new FileReader(f);
             ta.read(fr,ta);
          }
         catch(Exception e){   }
     }

     if(ae.getSource()==fsave||ae.getSource()==b2)
     {
         JFileChooser jfc1=new JFileChooser();
          File d=new File("D:/jdk1.4/bin");
          jfc1.setCurrentDirectory(d);
          jfc1.showSaveDialog(this);
         File f=jfc1.getSelectedFile();
         setTitle(f.toString());
         try
         {
             FileWriter fw=new FileWriter(f);
             ta.write(fw);
        }
        catch(Exception e){   }
    }
                  if(ae.getSource()==fexit)
    {
        int x=JOptionPane.showConfirmDialog(this,"DO U WANT TO QUIT?", "Exit Dialog" ,0);
        if(x==0) 
          System.exit(0);
      }
   }
     public static void main(String a[])
     {
      Menu m=new Menu();
      m.setVisible(true);
     }

 } 
output:


No comments:

Post a Comment