Alright, so I'm trying to learn how to make GUI's. I've managed to construct a frame, panel, menubars, items etc. Now I'm trying to get it so something happens when I click in the menu. I've followed a few different tutorials online, and also read through a textbook I bought. However, no matter what I try, I keep getting this error.
Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type RiskMain is accessible. Must qualify the allocation with an enclosing instance of type RiskMain (e.g. x.new A() where x is an instance of RiskMain). at RiskMain.createWindow(RiskMain.java:43) at RiskMain.main(RiskMain.java:34)
the code is
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; public class RiskMain { private static JFrame mainFrame = new JFrame("Risk Processor"); private static JMenuBar menuBar = new JMenuBar(); private static JMenu fileMenu = new JMenu("File"); private static JMenu gameMenu = new JMenu("Game"); private static JMenu playerMenu = new JMenu("Player"); private static JMenuItem newGame = new JMenuItem("New"); private static JMenuItem openGame = new JMenuItem("Open"); private static JMenuItem saveGame = new JMenuItem("Save"); private static JMenuItem quitGame = new JMenuItem("Quit"); private static JMenuItem newDay = new JMenuItem("New Day"); private static JMenuItem genText = new JMenuItem("Generate Text"); private static JMenuItem addAttack = new JMenuItem("Add Attack"); private static JMenuItem purchItem = new JMenuItem("Purchase Item"); public static void main(String []args) { createWindow(); } public static void createWindow() { menuBar.add(fileMenu); menuBar.add(gameMenu); menuBar.add(playerMenu); fileMenu.add(newGame); newGame.addActionListener(new MenuItemListener()); fileMenu.add(openGame); fileMenu.add(saveGame); fileMenu.add(quitGame); gameMenu.add(newDay); gameMenu.add(genText); playerMenu.add(addAttack); playerMenu.add(purchItem); mainFrame.setJMenuBar(menuBar); mainFrame.setVisible(true); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(1200,800); } private class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "click"); } } }
I appreciate any help and incite that can be offered.