Hi, Ive been trying to add action listeners to my menu items. I've tried various methods but can't get anything to work! I'm not too familiar with extends and implements but if I was shown to add one action listener it would a massive help. thanks in advance
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.event.*;
public class wordDictionary
{
public static void main(String [] args)
{
wordDictionary aFrame = new wordDictionary();
}
public wordDictionary()
{
JFrame aFrame = new JFrame();
Container contentPane;
aFrame.setTitle("Word Dictionary");
aFrame.setResizable(false);
aFrame.setSize(400, 300);
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
aFrame.setVisible(true);
JMenuBar menuBar = new JMenuBar();
//Menu Items
JMenu analyseText = new JMenu("Analyse Text");
JMenu dictionary = new JMenu("Dictionary");
JMenu statistics = new JMenu("Statistics");
JMenu exit = new JMenu("Exit");
// Dictionary Menu Items
JMenuItem addWord = new JMenuItem("Add Word");
JMenuItem deleteWord = new JMenuItem("Delete Word");
JMenuItem searchWord = new JMenuItem("Search");
JMenuItem listEntries = new JMenuItem("List Entries");
//Statistic menu items
JMenuItem wordCount = new JMenuItem("Word Count");
JMenuItem palindromes = new JMenuItem("Palindromes");
JMenuItem longestWord = new JMenuItem("Longest Word");
JMenuItem shortestWord = new JMenuItem("Shortest Word");
JMenuItem alphabeticFreq = new JMenuItem("Alphabetic Frequency");
//add listeners
//MenuListener listener = new MenuListener();
//deleteWord.addMenuListener(this);
menuBar.add(analyseText);
menuBar.add(dictionary);
menuBar.add(statistics);
menuBar.add(exit);
//Add items to dictionary menu
dictionary.add(addWord);
dictionary.add(deleteWord);
dictionary.add(searchWord);
dictionary.add(listEntries);
//Add items to statistics menu
statistics.add(wordCount);
statistics.add(palindromes);
statistics.add(longestWord);
statistics.add(shortestWord);
statistics.add(alphabeticFreq);
//Set menu bar for JFrame
aFrame.setJMenuBar(menuBar);
}
private class addWord extends JMenuItem implements ActionListener
{
public addWord(String text)
{
addWord.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("Item clicked: "+e.getActionCommand());
}
}
}