I was wondering whether someone would possibly be able to help me with a problem that I am having? So I am creating a game of checkers and I wish to add a menu bar to the top of my window, the code of which can be seen below, however within one of the menu bar tabs, I want to add the rules for the game, in order to display the rules in a clear format. I have decided to use a JDialog box, which I have created and the code of which can be seen code this section of box. My Question is how do I add it into my code within the first section, which I have labelled 1), as they are different classes within java, I wish to add the JDialog box into the section of code which states: "Where my Dialog Box should go!!", which is the 9th line down, If some could possibly supply me with code to solve this I would be extremely grateful, I have tried to look out help online but couldn't find anything directly from this problem, Thank you.
1) public static void main(String[] args) {
JFrame window = new JFrame("Checkers"); // Sets the title at the top of the window as 'Checkers'
JMenuBar bar = new JMenuBar(); // Adds the Menu Bar
JMenu fileMenu = new JMenu("File"); // Adds a File Tab to the Menu Bar
JMenu HelpMenu = new JMenu("Help"); // Adds a Help Tab to the Menu Bar
JMenuItem Exit = new JMenuItem("Exit"); // Adds the Exit sub-tab as an Item of the JMenu
JMenuItem MainMenu = new JMenuItem("Main Menu"); // Adds the Main Menu sub-tab as an Item of the JMenu
JMenu Rules = new JMenu("Rules of Checkers"); // Adds the Rules of Checkers sub-tab as an Item of the JMenu
JMenuItem RulesText = new JMenuItem("Where my Dialog Box should go!");
Rules.add(RulesText); // Adds the Rules Text Item into the Rules of Checkers tab.
HelpMenu.add(Rules); // Adds the Rules of Checkers tab into the Help tab
bar.add(HelpMenu); // Adds the Help tab to the Menu Bar
fileMenu.add(MainMenu);// Adds the Main Menu sub-tab into the File tab
fileMenu.addSeparator(); // Adds a line in between the Main Menu sub-tab and the Exit sub-tab
fileMenu.add(Exit); // Adds the Exit sub-tab into the Menu tab
bar.add(fileMenu); // Adds the Menu tab to the Menu bar
bar.add(HelpMenu); // Adds the Help tab to the Menu Bar
window.setJMenuBar(bar); // Adds the Menu Bar to the application window
Exit.addActionListener(new ActionListener() // Adds an ActionListener to the Exit Sub-tab
{
public void actionPerformed(ActionEvent ev)
{
System.exit(0); // This means that when the Exit sub-tab is clicked, it will exit the application
}
});
The code for my JDialog box if it helps anything, Thank you.
2) static JFrame frame;
public static void main(String args[])
{
JOptionPane.showMessageDialog(frame,
"- Pieces must always move diagonally\n" +
"- Single pieces are limited to forward moves\n" +
"- Kings may move both forward and backward\n" +
"- When a piece is captured, it is removed from the board\n" +
"- If a player is able to make a capture, there is no option, the jump must be made\n" +
"- When a piece reaches the opponents end of the board, it is crowned and becomes a King",
"Rules for Checkers",
JOptionPane.PLAIN_MESSAGE);
You do exactly what you did for the "Exit" menu item. That is you create an ActionListener and add the ActionListener to the "Rules" menu item.
Then in the ActionListener code you create and display the option pane.
The reason you have a problem is that the whole design of your application is wrong. You should never code your application in the main(...) method. The main method is just used to create an instance of your application. I suggest you look at the Swing tutorial on How to Use Menus. The MenuLookDemo
will give you an idea on how to better structure your code.
Also, be consistent with variable names. Variable names should NOT start with an upper case character.