Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (736)
Games in Android Showcase (224)
games submitted by our members
Games in WIP (813)
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 1679 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?

 * 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
        //Make our window get the focus

        //Don't allow the focus to move around in our window

        //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
        //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
            new WindowAdapter()
                public void windowClosing( WindowEvent e )
                    //Call the exit method


     * 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.setFont(new Font("Arial", Font.BOLD, 18));
        score = new JLabel ("0");
        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
        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
        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);


     * 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
        //If the restart button is pressed
        if( e.getSource() == restart )
            //Screen focus changes to the game window
        //If the restart button is pressed
        if( e.getSource() == restart_game )
            //Screen focus changes to the game window
        //If the restart button is pressed
        if( e.getSource() == quit_game )

     * @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


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

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.

cybrmynd (138 views)
2017-08-02 12:28:51

cybrmynd (159 views)
2017-08-02 12:19:43

cybrmynd (153 views)
2017-08-02 12:18:09

Sralse (169 views)
2017-07-25 17:13:48

Archive (646 views)
2017-04-27 17:45:51

buddyBro (766 views)
2017-04-05 03:38:00

CopyableCougar4 (1300 views)
2017-03-24 15:39:42

theagentd (1264 views)
2017-03-24 15:32:08

Rule (1237 views)
2017-03-19 12:43:22

Rule (1312 views)
2017-03-19 12:42:17
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51 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‑
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!