0

I've created a code that whenever you click the jmenuitem New Game int x will have a value of 1 but sadly it doesn't return any value

    package sprite;

    import java.awt.event.*;

    import javax.swing.*;
    import  sprite.Intro;
    import sprite.Fishes;

    public class Ken{

    static int x;

    public static void main(String args[])
    {   
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame f = new JFrame("Save the FISH.");
        Fishes fs = new Fishes();
        Intro in = new Intro();
        Arrow a = new Arrow();          
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(800,600);
        if(x == 0){
            f.add(a);
        }           
        if(x == 1){
            f.add(fs);
        }           
        JMenuBar mb = new JMenuBar();
        JMenu opt = new JMenu("Game");
        JMenu sd = new JMenu("Select Difficulty");
        JMenuItem ng = new JMenuItem("New Game");
        JMenuItem ex = new JMenuItem("Exit");
        JMenuItem l1 = new JMenuItem("Easy");
        JMenuItem l2 = new JMenuItem("Average");
        JMenuItem l3 = new JMenuItem("Hard");           
        f.setJMenuBar(mb);    
        mb.add(opt);
        mb.add(sd);         
        opt.add(ng);
        opt.add(ex);            
        sd.add(l1);
        sd.add(l2);
        sd.add(l3);

        class exit implements ActionListener{

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }

        class newGame implements ActionListener{

            public void actionPerformed(ActionEvent e) {
                x = 1;
            }
        }           
        ng.addActionListener(new newGame());
        ex.addActionListener(new exit());
    }       
}

what I'm planning to do is that x will have a value of 1 so i can use it in another class to proceed and whenever x gains a value of 1 a new panel will appear.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Africa Ken
  • 25
  • 6

1 Answers1

2

Get out of the static world and create a true OOP class, one with "state" -- with instance fields. You can set the instance fields from within the actionPerformed, solving your problem.

Your public static main method should exist solely to create your GUI objects and set them running, and that's it.


Edit
You stated in comment,

what exactly do you mean? im just a newbie i haven't gain full understanding of every single element in java.

Sorry, my last answer was a bit rushed as my lunch time was finishing and I had to get back to work. Currently your program is nothing more than a single static main method with everything trying to be shoehorned into this method. This would be fine if you were creating the most basic console program, such as one that asks the user for 2 numbers, then adds the numbers and returns the answer, but you are no longer trying to do this. Instead you're trying to create a Swing GUI program, one whose state you wish to change if the user interacts with it in an event-driven way, in other words you want it to change state if the user presses a button or selects a menu item.

Since your needs and requirements are becoming more complex, your program structure will need to change to reflect this. Is this an absolute requirement that you do this? No -- something called Turing Equivalence tells that it is possible to write most complex program imaginable inside of a single static main method, but due to the increased complexity, the program would become very difficult to understand and almost impossible to debug.

What I recommend specifically is that you create one or more well behaved object-orient classes, classes with non-static variables and non-static methods, and use these to build your GUI and its model (the non-GUI nucleus that GUI programs should have). Again the main method should be short, very short, and should only involve itself in creating the above classes, and setting the GUI visible, and that's about it.

What you want to do is to study the basic concepts of Java, and in particular that on how to create Java classes. The Java tutorials can help you with this.


Edit 2
There is another major problem with your program in that you're suffering from a newbie falacy, that by changing the state of a variable, this will magically change something that used the variable previously.

For instance, in your program you use a static variable, x, to hold a value of 0 or 1, and then you try to add a component to a JFrame based on the state of that variable:

// Code Block (A)
if(x == 0){
   f.add(a);
}           
if(x == 1){
   f.add(fs);
} 

Note that when this code is called, the program is just starting its run, and so x has the default value of 0, and so a will be added to your JFrame.

Later in your ActionListener you change x's value to 1:

// Code Block (B)
class newGame implements ActionListener {
   public void actionPerformed(ActionEvent e) {
      x = 1;
    }
}  

But you will need to understand that changing x's value here in code block (B) will have no effect on what the JFrame is displaying because code block (A) has run and completed and is never to run again in this program.

The solution is to either re-run code block (A) from within (B), something I don't recommend, or better yet, use a CardLayout to swap JPanels displayed by the JFrame from within Card Block (B).

You can learn more about CardLayout at the CardLayout Tutorial. Also, please check out my previous answers with code on this very site that you can find here:

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • what exactly do you mean? im just a newbie i haven't gain full understanding of every single element in java. – Africa Ken Oct 17 '13 at 17:10