1

I have a JMenuBar with a help button. When user clicks on "help" I need to open a different window with a .jpg showing the game instructions. That same window can be closed when the user clicks outside of it. I think I'm missing code because it does not work:

Window.java

public class Window extends JWindow
{
  //java.net.URL imgIntro = getClass().getResource("/images/intro.jpg");
  ImageIcon imIntro = new ImageIcon(getClass().getResource("/images/intro.jpg"));

  //java.net.URL imgRegles = getClass().getResource("/images/rules.jpg");
  ImageIcon imRules = new ImageIcon(getClass().getResource("/images/rules.jpg"));

  static Thread t = new Thread();
  static int thread = 0;
  static JButton bouton;
  static int X;
  static int Y;

  public Window( int X, int Y, int type) {

    super();
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(X,Y);
    setLocation( (dim.width - X)/2 , (dim.height - Y)/2);
    setVisible(true);
    Container fen = getContentPane();

    if (type == 1 ) bouton = new JButton(imIntro);
    else            bouton = new JButton(imRules);

    bouton.setPreferredSize(new Dimension(X, Y) );
    fen.add( bouton);
    bouton.setVisible( true );

    show();


   /* if window introduction,
      just display for 5 secondes */

    if( type == 1 ) {
        try {
            Thread.sleep(5000);
            thread = 1;
        }
        catch( java.lang.InterruptedException ex ) {
            JOptionPane.showMessageDialog(null, "erreur");
            }
        dispose();

    }


    /* if window of rules
       only close it when user clicks */

    else if ( type == 2 ) {
        bouton.addActionListener( new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                     dispose();
                }
        });
    }

 }
}

Menu.java

    public class Menu extends JMenuBar implements ActionListener{

     Interface map;

            JMenu m5;

    public Menu(Interface map){
            super();
            this.map=map;
           m5 = new JMenu ("Help");//dislay instructions / rules

            this.add(m5);

    public void actionPerformed(ActionEvent evt){

//.../
                      else if(evt.getSource () == m5){
                          //new JWindow
                          Window rules = new Window( 700, 457, 2);

                      }
    }
    }

EDIT WITH MCVE

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 *
 * @author Mark
 */
public class Test {



    JFrame mainFrame = new JFrame ();
    JFrame instructions = new JFrame();


    public Test (){
     gui ();
}


    public void gui (){
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(600, 400);
        mainFrame.setVisible(true);

        instructions.setSize(200, 200);

        JMenuBar mb = new JMenuBar ();
        JMenu help = new JMenu("Help");
        mb.add(help);
        JMenuItem instructionsMenu = new JMenuItem ("Instructions");
        help.add(instructions);


        mainFrame.setJMenuBar(mb);

        instructions.addActionListener(new ActionListener(){
            @Override

            public void actionPerformed(ActionEvent e){

                instructions.setVisible(true);
                mainFrame.dispose();
            }


        });


    }




    public static void main(String[] args) throws IOException, URISyntaxException
    {
        new Test ();
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
Mark Doe
  • 125
  • 2
  • 2
  • 9
  • Post an [MCVE](http://stackoverflow.com/help/mcve). Be sure to copy-paste your code to a *new project* and make sure it compiles and runs before posting it here. Please indent the code properly. – user1803551 Jan 22 '16 at 21:56
  • @user1803551: Well it's going to be difficult to post an MCVE... I'm only trying to display a new JWindow with an image of the instructions of the game when user click "Help". – Mark Doe Jan 25 '16 at 00:04
  • It's not going to be difficult at all. All those menus have nothing to do with the question. Create a class with a `main` that shows a window with a button and when the button is pressed the new window with the image is displayed. Should be no more than 30 lines. Then we can see what's not working. – user1803551 Jan 25 '16 at 00:42
  • @user1803551: I edited my post... And I'm making the class.. Meanwhile, for your information, the problem is just that, the new window with the image is not getting displayed when the user click on the "help" button. Nothing happens really as if there is no `actionEvent`. It's just a flat JButton that does nothing. I'm guessing I'm missing codes. – Mark Doe Jan 25 '16 at 00:58
  • Still can't run it. I don't see a `main` method. Be sure to copy-paste your code to a *new project* and make sure it compiles and runs before posting it here. Also, please indent your code properly. The IDE can help you with that. – user1803551 Jan 25 '16 at 01:44
  • @user1803551: Sorry it took so long I was trying codes around. I came up with an MCVE that does not compile. The IDE returns this : cannot find symbol `` – Mark Doe Jan 25 '16 at 04:08
  • 1
    You want to set the `ActionListener` for the menu button, not for the window. You also need to prepare the frame *before* setting it to visible. – user1803551 Jan 25 '16 at 04:10
  • @user1803551: Silly Me! Dude Thanks! I've been coding for almost 10h need a break. – Mark Doe Jan 25 '16 at 04:19
  • So you got it all to work? – user1803551 Jan 25 '16 at 12:08
  • @user1803551: Yes I did. I give you reps if you want them. Else I will post my own answer. – Mark Doe Jan 25 '16 at 19:23

2 Answers2

1

I suggest you use a JDialog for the instructions window and not a JFrame. See here for example.

public class Test {

    JFrame mainFrame = new JFrame();
    JDialog instructions = new JDialog(mainFrame);

    public Test() {

        gui();
    }

    public void gui() {

        instructions.setSize(200, 200);

        JMenuBar mb = new JMenuBar();
        JMenu help = new JMenu("Help");
        mb.add(help);
        JMenuItem instructionsMenu = new JMenuItem("Instructions");
        help.add(instructionsMenu);
        instructionsMenu.addActionListener(e -> instructions.setVisible(true));

        mainFrame.setJMenuBar(mb);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(600, 400);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(() -> new Test());
    }
}

Notes:

  • Call setVisible as the last thing you do for a window.
  • In your real program, where you have components in the windows, call their pack() instead of setting their size.
  • Start Swing from the EDT.
Community
  • 1
  • 1
user1803551
  • 12,965
  • 5
  • 47
  • 74
-1

Why do you have a else-if condition without the if condition? This would create a syntax error.

Also, your code to show your window should be something like this:

dispose();
Window rules = new Window( 700, 457, 2);
rules.show();