Hi folks!
I'm trying to wire simple code and so far I'm stuck on ActionListener.
My program should contain (JMenu) Menu called SELECT and two options InnerFrame 1 and InnerFrame 2. To debug it I've started using System.EXIT , unfortunately I cant compile my program, I'm receiving an error: "java:48: non-static method addActionListener(java.awt.event.ActionListener) cannot be referenced from a static context
JMenuItem.addActionListener(this);"
code goes here
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import java.awt.event.*;
import java.awt.*;
/*
* InternalFrameDemo.java
*
*/
public class InternalFrameDemo1 extends JFrame implements ActionListener{
public InternalFrameDemo1() {
super("My First Internal Frame");
int inset = 10;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset,
screenSize.width - inset*2,
screenSize.height - inset*2);
//Set up the GUI.
JDesktopPane desktop = new JDesktopPane(); //a specialized layered pane
JInternalFrame innerframe = new JInternalFrame("Internal Window" ,
false, //resizable
false, //closable
true, //maximizable
false);//iconifiable
JMenuBar mb = new JMenuBar();
JMenu helpMenu = new JMenu("Select", true);
helpMenu.add(new JMenuItem ("Exit"));
JMenuItem.addActionListener(this);
mb.add(helpMenu);
setJMenuBar(mb);
innerframe.setSize(640,480);
innerframe.setVisible(true); //necessary as of 1.3
desktop.add(innerframe);
setContentPane(desktop); //important as it allows us to add items to the desktop
setSize(640,480);
setVisible(true);
//setResizable(false);
}
public static void main(String[] args) {
InternalFrameDemo1 frame = new InternalFrameDemo1();
}
public void actionPerformed(ActionEvent e)
{
String message = "";
if (e.getSource() instanceof JMenuItem){
if (e.getActionCommand().equals("Exit")) //message = "The New Item was selected";
System.exit(0);
//JOptionPane.showMessageDialog(null,message, "The Menu was clicked", JOptionPane.PLAIN_MESSAGE);
}
}
}
Could you advise, please?
Best regards
Thom