Results 1 to 3 of 3
Thread: Adding Action Listeners
- 08-07-2011, 06:15 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 19
- Rep Power
- 0
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);
}
}
- 08-07-2011, 06:24 PM #2
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.
-
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
-
Applets issue with Action Listeners
By aadem in forum New To JavaReplies: 2Last Post: 03-19-2011, 06:17 PM -
For some reason, my GUI is not recognizing multiple action listeners for buttons.
By JavaStudent1990 in forum New To JavaReplies: 8Last Post: 08-10-2010, 02:59 AM -
Creating a GUI events with action listeners
By sidd0123 in forum AWT / SwingReplies: 8Last Post: 04-02-2010, 11:32 PM -
JOptionPane, action listeners, and the Enter key.
By DigitalMan in forum AWT / SwingReplies: 5Last Post: 01-26-2010, 02:51 AM -
Action Event and Listeners
By lost1 in forum New To JavaReplies: 3Last Post: 11-14-2007, 04:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks