Frage

I'm trying to display a block of text in a frame when the user clicks the menu button titled "Rules". Somehow nothing displays when I click Rules.

So my question is, why is there nothing displaying when I click Rules. I thought adding ActionListener to rules would mean that when Rules is clicked it will pop up a new frame with the title "Rules" and inside the frame there will be a message saying "Write rules here...".

Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class CheckerProgram{
    public static int rows = 8;
    public static int columns = 8;
    public static Color color1 = Color.BLACK;
    public static Color color2 = Color.RED;

    public static void main( String[] args ){
        JFrame window = new JFrame("Checkers");
        window.setSize( 800, 800 );
        window.setTitle("Checkers!");
        Container pane = window.getContentPane();
        pane.setLayout(new GridLayout(rows, columns));
        window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        Color temp;
        for( int i=0; i<rows; i++ ){
            if( i%2 == 0 ){
                temp = color1;
            }
            else{
                temp = color2;
            }

            for( int j=0; j<columns; j++ ){
                JPanel panel = new JPanel();
                panel.setBackground(temp);

                if( temp.equals(color1)){
                    temp = color2;
                }
                else{
                    temp = color1;
                }

                pane.add(panel);
            }
        }
        window.setVisible(true);

        JMenuBar menuBar = new JMenuBar();
        window.setJMenuBar(menuBar);

        // Display options ex, new game, surrender, close game.
        JMenu options = new JMenu("Options");
        menuBar.add(options);
        JMenuItem newGame = new JMenuItem("New Game");
        JMenuItem surrender = new JMenuItem("Surrender");
        JMenuItem closeGame = new JMenuItem("Close Game");
        options.add(newGame);
        options.add(surrender);
        options.add(closeGame);

        // Action button for Close Game under Options menu
        class closeGameAction implements ActionListener{
            public void actionPerformed( ActionEvent e ){
                System.exit(0);
            }
        }
        closeGame.addActionListener( new closeGameAction() );

        // Display scores for both players
        JMenu scores = new JMenu("Scores");
        menuBar.add(scores);

        // Display rules for game
        JMenu rules = new JMenu("Rules");
        menuBar.add(rules);
        rules.addActionListener( new rulesAction() );


    } // End of main


    static class rulesAction implements ActionListener{
            public void actionPerformed( ActionEvent e ){
                JFrame displayRules = new JFrame("Rules");
                displayRules.setVisible(true);
                displayRules.setSize( 300, 300 );
                JLabel label = new JLabel("Write rules here...");
                JPanel panel = new JPanel();
                panel.add(label);
                displayRules.add(panel);

            }
     }
}

Here's a screenshot of the main frame. So up there there's a menu option called "Rules" when I click it nothing happens.

enter image description here

War es hilfreich?

Lösung 2

You need a JMenuItem to prompt the window.

JMenu rules = new JMenu("Rules");
JMenuItem jmiRules = new JMenuItem("Rules");
rules.add(jmiRules);
menuBar.add(rules);
jmiRules.addActionListener( new rulesAction() );

Also put the displayRules.setVisible(true); at the end of the actionPerformed so everything is added before displayed

Also put the window.setVisible(true); at the end of the main. It's causing a delay of the menuBar. You should add all you components before making screen visible.

Andere Tipps

rules is a JMenu:

JMenu rules = new JMenu("Rules");

I think you mean for it to be a JMenuItem.

You should also consider using a JDialog to display the rules. See "The Use of Multiple JFrames, Good/Bad Practice?"

In general you should also set things visible after you've created them. When I ran this on OSX the menu bar does not appear and it's (loosely) because you are setting the JFrame visible before adding the bar.

A JMenu doesn't support an ActionListener (even though the addActionListener() method is valid).

So you need to add a MouseListener to a JMenu to handle a mouse click, otherwise you need to display "Rules" as a menu item for another JMenu.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top