Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (606)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick question about my alignment ...  (Read 835 times)
0 Members and 1 Guest are viewing this topic.
Offline cgru2

Junior Newbie





« Posted 2006-04-22 06:05:07 »

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 <b>main</b> 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();
    }

}
Offline Kova

Senior Devvie





« Reply #1 - Posted 2006-04-22 13:35:01 »

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 Grin
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #2 - Posted 2006-04-22 13:38:37 »

A quick guess, maybe it has something to do with add(bar) versus setJMenuBar(bar)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #3 - Posted 2006-04-22 16:59:04 »

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.

Offline Mr_Light

Senior Devvie


Medals: 1


shiny.


« Reply #4 - Posted 2006-04-22 19:05:19 »

1  
code tags  :-\

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Elsealabs (20 views)
2014-12-28 10:39:27

CopyableCougar4 (21 views)
2014-12-28 02:10:29

BurntPizza (25 views)
2014-12-27 22:38:51

Mr.CodeIt (15 views)
2014-12-27 04:03:04

TheDudeFromCI (20 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (60 views)
2014-12-15 09:26:44

Mr.CodeIt (48 views)
2014-12-14 19:50:38

BurntPizza (98 views)
2014-12-09 22:41:13

BurntPizza (117 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!