How to implement ActionListener

E

elingtse

Hi all,

I have problem to call the ActionListener but have no idea what is
going wrong, Please help. Thankyou.

When compile, the error message show :
-----------------------------------------------------------
C:\>javac TestEditorFrame.java

..\EditorFrame.java:7: cannot resolve symbol
symbol : class ActionListener
location: class EditorFrame
public class EditorFrame extends JFrame implements ActionListener{

^
..\EditorFrame.java:45: cannot resolve symbol
symbol : class ActionEvent
location: class EditorFrame
public void actionPerformed(ActionEvent ae){
^
..\EditorFrame.java:40: addActionListener(java.awt.event.ActionListener)
in javax
..swing.AbstractButton cannot be applied to (javax.swing.JMenuItem)
itemSave.addActionListener(itemSave);
^
3 errors

Class EditorFrame
---------------------------
import java.awt.*;
import javax.swing.*;

public class EditorFrame extends JFrame implements ActionListener{
//Attribute
private JTextArea textArea;
private JMenuBar menubar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem itemSave = new JMenuItem("Save");

//constructor
public EditorFrame(){
Container contentPane = getContentPane();
String content = ("This is a text.\nSecond Line.");
textArea = new JTextArea(content, 3, 20);
setJMenuBar(menubar);
menubar.add(fileMenu);
fileMenu.add(itemSave);
contentPane.add(textArea);
itemSave.addActionListener(this);
}

//Action Listener Method
public void actionPerformed(ActionEvent ae){
System.out.println("ok");
}
}

Class TestEditorFrame
---------------------------------
import java.awt.*;
import javax.swing.*;

//Definition of class TestEditorFrame
public class TestEditorFrame extends JFrame {

//Main executive method
public static void main(String args[]){

//create an EditorFrame object
EditorFrame anEditor = new EditorFrame();

anEditor.setVisible(true);

//set when the close button is clicked, the application exits
anEditor.setDefaultCloseOperation(EXIT_ON_CLOSE);

//Set the GUI to its optimal size
anEditor.pack();

}
}
 
Z

zero

(e-mail address removed) wrote in @g47g2000cwa.googlegroups.com:
Hi all,

I have problem to call the ActionListener but have no idea what is
going wrong, Please help. Thankyou.

When compile, the error message show :
-----------------------------------------------------------
C:\>javac TestEditorFrame.java

.\EditorFrame.java:7: cannot resolve symbol
symbol : class ActionListener
location: class EditorFrame
public class EditorFrame extends JFrame implements ActionListener{

^
.\EditorFrame.java:45: cannot resolve symbol
symbol : class ActionEvent
location: class EditorFrame
public void actionPerformed(ActionEvent ae){
^
.\EditorFrame.java:40: addActionListener(java.awt.event.ActionListener)
in javax
.swing.AbstractButton cannot be applied to (javax.swing.JMenuItem)
itemSave.addActionListener(itemSave);
^
3 errors

<code snipped>

Rather than just hand you the solution, I'll tell you where you can find
it.

Look at:

http://java.sun.com/j2se/1.5.0/docs/api/

Bookmark this page, you'll need it a lot.

In the left bottom frame, look for "ActionListener" and "ActionEvent",
the two classes that are mentioned in your error messages. Check both of
their pages. At the top of the page (directly above the title), you'll
see the packages that those classes belong to.

Make sure you import all the classes you use in the program!

Hope this helps,
Zero
 
R

ricky.clarkson

I would strongly recommend importing each class you use, individually,
rather than using the .* syntax.

import javax.swing.JButton;
import javax.swing.JFrame;

etc.

Now look for the import that imports ActionListener in your code. Oh,
it's not there?!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top