Quick question about my alignment ...

Why is my JMenuBar aligned in the centre?

As well as my buttons in the buttom panel …

I would like to have the top menubar aligned to the left of the panel!

Any ideeas?

/*

  • Invaders.java
  • Created on 27 May 2005, 15:06
    */

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

/**

  • This class creates the JFrame, adds a quit button
  • to it and contains the main method to start the
  • program

*/
public class Invaders
implements ActionListener
{
//The game window
private JFrame frame = null;

//The buttons
private JButton quit = null;
private JButton restart = null;    
private JLabel name = null;
private JLabel score = null;

private JMenuBar menubar;
private JMenu file, options;    
private JMenuItem restart_game, quit_game, change_speed, change_rows, change_name;



//The JPanel which runs and draws the game
private Game game = null;

/** Creates a new instance of Invaders */
public Invaders()
{
    //Create the window and buttons
    createGUI();        
    
    //Make our window get the focus
    frame.getContentPane().requestFocusInWindow();

    //Don't allow the focus to move around in our window
    frame.getContentPane().setFocusTraversalKeysEnabled(false);

    //Show the frame to the player
    frame.setVisible( true );
}

/**
 * Create the JFrame and all user interface components
 */
private void createGUI()
{
    //Setup the frame with an appropriate title
    frame = new JFrame( "Invaders From Space!" );
    
    //Make the window a decent size and position it's top
    //left corner at 0,0
    frame.setBounds( 0, 0, Game.GAME_WIDTH, Game.GAME_HEIGHT );

    //Set the layout of the window
    frame.getContentPane().setLayout( new BorderLayout() );

    //Add the button to the window
    createButtons();        
    
    //Creates a dialog box with a dropdown menu asking the player to make a selection
    Object[] possibleValues = { "1", "2", "3", "4" };
    Object selectedValue = JOptionPane.showInputDialog(null,
        "Select number of rows of Aliens", "",
        JOptionPane.INFORMATION_MESSAGE, null,
        possibleValues, possibleValues[0]);
        
    if (selectedValue == null)
    {
        selectedValue = "1";
    }
    
    int numRows = Integer.parseInt((String)selectedValue);
    
    selectedValue = null;
    
    //Displays a drop-down dialogue box asking the player to select the speed.
    Object[] possibleSpeedValues = { "Slow", "Medium", "Fast" };
    selectedValue = JOptionPane.showInputDialog(null,
        "Select speed", "",
        JOptionPane.INFORMATION_MESSAGE, null,
        possibleSpeedValues, possibleSpeedValues[0]);
        
    if (selectedValue == null)
    {
        selectedValue = "Slow";
    }
    String speedString = (String)selectedValue;

    //Whenever a value is chosen from the dropdown box, 
    //the speed is incremented by one
    int speed = 0;
    if (speedString.equals("Slow"))
    {
        speed = 1;
    }
    else if (speedString.equals("Medium"))
    {
        speed = 2;
    }
    else if (speedString.equals("Fast"))
    {
        speed = 3;
    }

    //Instatiate the class the runs the game
    game = new Game(score, numRows, speed);

    //Add the game to the middle of the JFrame
    frame.getContentPane().add(game, BorderLayout.CENTER );

    //Add a listener to the window so it closes
    //properly
    frame.addWindowListener(
        new WindowAdapter()
        {
            public void windowClosing( WindowEvent e )
            {
                //Call the exit method
                doExit();
            }
        }
    );

}

/**
 * Create buttons needed by the game
 */
private void createButtons()
{
    //Create a JPanel to put the buttons in
    JPanel p = new JPanel(  );
    
    //Create a JPanel to put the menu bar in
    JPanel t = new JPanel(  );
   
    
    //Initialise the buttons
    quit = new JButton( "Quit" );
    restart = new JButton( "Restart" );       
    name = new JLabel (JOptionPane.showInputDialog("Please type your name"));
    name.setForeground(Color.BLACK);
    name.setFont(new Font("Arial", Font.BOLD, 18));

    score = new JLabel ("0");
    score.setForeground(Color.RED);
    score.setFont(new Font("Arial", Font.BOLD, 18));

    //Add an action listener to the buttons
    //so we know when they are pressed
    quit.addActionListener( this );
    restart.addActionListener( this );
    
    //Add the buttons to the button panel
    p.add( quit );
    p.add( restart );
    p.add( name );
    p.add( score );

    //Add the button panel to the bottom of the
    //window
    frame.getContentPane().add( p, BorderLayout.SOUTH );
    
    //Initialise the menubar
    menubar = new JMenuBar();
    
    restart_game = new JMenuItem("Restart Game");
    quit_game = new JMenuItem("Quit Game");
    change_speed = new JMenuItem("Change Speed");
    change_rows = new JMenuItem("Change Rows");
    change_name = new JMenuItem("Change Name");
    
    
    //Add the panel to the top of the
    //window
    frame.getContentPane().add( t, BorderLayout.NORTH);
    
    //Add the menubar to the panel
    t.add( menubar );  

    file = new JMenu("File");
    menubar.add(file, BorderLayout.WEST);
    
    menubar.add( new JSeparator());
    
    options = new JMenu("Options");
    menubar.add(options, BorderLayout.WEST);
    
    restart_game.addActionListener(this);
    quit_game.addActionListener(this);
    
    file.add(restart_game);
    file.add(quit_game);
    options.add(change_speed);
    options.add(change_rows);
    options.add(change_name);

}

/**
 * Make sure the application exits properly
 */
private void doExit()
{
    System.exit( 0 );
}

/**
 * Listen for button presses, etc.
 */
public void actionPerformed( ActionEvent e )
{
    //If the quit button is pressed
    if( e.getSource() == quit )
    {
        //Exit the program
        doExit();
    }
    
    //If the restart button is pressed
    if( e.getSource() == restart )
    {
        game.restart();
        //Screen focus changes to the game window
        game.grabFocus();
    }
    
    //If the restart button is pressed
    if( e.getSource() == restart_game )
    {
        game.restart();
        //Screen focus changes to the game window
        game.grabFocus();
    }
    
    //If the restart button is pressed
    
    if( e.getSource() == quit_game )
    {
        doExit();
    }
   
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    new Invaders();
}

}

if you expect me to go through all that code without even a variable name you must be kidding… I’m getting tired of this post with tons of code and one sentece “help me”.

Edit: and someone should write a game tutorial that isn’t based on space invaders… kind of sick of them too ;D

A quick guess, maybe it has something to do with add(bar) versus setJMenuBar(bar)

Go read the Sun tutorial on Swing Menus. You are doing lots of strange things. You don’t put the menu bar in a sub-panel for example. And JMenuBar does not use BorderLayout so things like menubar.add(file, BorderLayout.WEST); don’t make any sense.

code tags  :-\