JMenuBar
i am having problems in adding actions to my JMenuBar.
My Current code is
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(this);
...........
.........
public void actionPerformed(ActionEvent event) {
if (event.getSource() == easyMenuItem) {
easyGame();
}
However, it is not working. ny idea?
quitMenuItem != easyMenuItem
oops.....transcribed it wrong......i have numerous JMenuItems, and transcribed the wrong one. The original post should have read:
am having problems in adding actions to my JMenuBar.
My Current code is
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(this);
...........
.........
public void actionPerformed(ActionEvent event) {
if (event.getSource() == quitMenuItem) {
quitGame();
}
It's impossible to say, give what you've posted. The best thing you can do is post a small (<1 page) example program demonstrating your problem.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Vector;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.*;
import javax.swing.text.*;
import java.io.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import java.util.*;
import java.awt.event.KeyEvent;
class Window extends JFrame implements ActionListener {
public Window() {
super("window");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(this);
menuBar.add(fileMenu);
fileMenu.add(quitMenuItem);
this.setJMenuBar(menuBar);
setContentPane(mainPanel);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == quitMenuItem) {
quitGame();
}
}
public void quitGame() {
}
}
class MenuDemo {
public static void main(String [] args) {
Window newWindow = new Window();
}
}
That is a sample program which comes up with the same error
I can't run your code -- there are syntax errors.
Here is an example of creating a menuitem:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class QuitGame implements ActionListener {
public void actionPerformed(ActionEvent event) {
//quit game code
System.out.println("QuitGame");
}
}
class MenuExample implements Runnable {
public void run() {
JFrame f = new JFrame("MenuExample");
f.setJMenuBar(createMenuBar());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(new QuitGame());
fileMenu.add(quitMenuItem);
menuBar.add(fileMenu);
return menuBar;
}
public static void main(String[] args) {
EventQueue.invokeLater(new MenuExample());
}
}
1) Swing related questions should be posted in the Swing forum.
2) Read the API before posting a question. The JMenuItem API has a link to the Swing tutorial on "How to Use Menus" which contains a working example
3) Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.