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.