Results 1 to 3 of 3
  1. #1
    mamoonrizwan is offline Member
    Join Date
    Apr 2011
    Posts
    19
    Rep Power
    0

    Default Adding Action Listeners

    // i want to add Aciton Listenrs to the following code . please help
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.JFrame;

    class JavaMenu extends JFrame {
    JMenuBar menubar;
    JMenu file,
    edit,help
    ;

    JMenuItem exit,
    open,
    save,
    saveas,
    print,
    cut,
    copy,
    paste,
    undo,
    about
    ;
    JTextArea textarea;
    JLabel label;

    public JavaMenu (){
    setLayout(new FlowLayout());

    menubar = new JMenuBar();
    setJMenuBar(menubar);


    file = new JMenu("File");
    menubar.add(file);
    open = new JMenuItem("Open");
    file.add(open);
    save = new JMenuItem("Save");
    file.add(save);
    saveas = new JMenuItem("SaveAs");
    file.add(saveas);
    print = new JMenuItem("Print");
    file.add(print);
    exit = new JMenuItem("Exit");
    file.add(exit);

    edit = new JMenu("Edit");
    menubar.add(edit);
    cut = new JMenuItem("Cut");
    edit.add(cut);
    copy = new JMenuItem("Copy");
    edit.add(copy);
    paste = new JMenuItem("Paste");
    edit.add(paste);
    undo = new JMenuItem("Undo");
    edit.add(undo);

    help = new JMenu("help");
    menubar.add(help);
    about = new JMenuItem("about");
    help.add(about);


    label = new JLabel("type text here");
    add(label);
    textarea = new JTextArea(200,200);
    add(textarea);

    Event e = new Event();
    exit.addActionListener(e);



    }

    public class Event implements ActionListener{
    public void actionPerformed(ActionEvent e){
    System.exit(0);
    }
    }



    public static void main (String args[]){
    JavaMenu menu1 = new JavaMenu();
    menu1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    menu1.setSize(500,500);
    menu1.setVisible(true);



    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    What components do you want to add the action listener to?
    I see that you are adding one to exit.
    If you can add that one, can you copy the logic and code and use that for another?

    BTW Event is the name of an existing java SE class. You should use your own, unique names for your classes.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mamoonrizwan View Post
    // i want to add Aciton Listenrs to the following code . please help
    Please have a look at these tutorials which should help you:
    How to Write an Action Listener
    How to Use Buttons, Check Boxes, and Radio Buttons
    How to Use Menus

    Then by all means, give it a go. If you get stuck or your code shows errors, please come on back and show us your code, any errors you may have and any specific questions you may have.

    Also, if you post code, please use code tags so that the code will retain its formatting and be more readable. The link on this in my signature below can help show you how to do this. If it is in any way confusing, please don't hesitate to ask.

    Best of luck!

Similar Threads

  1. Applets issue with Action Listeners
    By aadem in forum New To Java
    Replies: 2
    Last Post: 03-19-2011, 06:17 PM
  2. Replies: 8
    Last Post: 08-10-2010, 02:59 AM
  3. Creating a GUI events with action listeners
    By sidd0123 in forum AWT / Swing
    Replies: 8
    Last Post: 04-02-2010, 11:32 PM
  4. JOptionPane, action listeners, and the Enter key.
    By DigitalMan in forum AWT / Swing
    Replies: 5
    Last Post: 01-26-2010, 02:51 AM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •