-1

Essentially, I am trying to add a home screen with 4 buttons, 3 difficulty buttons and a play button. I add the buttons to a JPanel and add the JPanel with a BoxLayout of Center. Why does the buttons still go all the way off to the right? Setting the icon for a JLabel on and adding it to the home screen JPanel is a possible mess up the flow of components? I want the difficulty buttons to be on top of the of the gif with the Play button at the bottom. Thanks for your help.

 //container
    snake = new JFrame();
    snake.setLayout(new BorderLayout());

    //home screen panel
    homeScreen = new JPanel();
    homeScreen.setLayout(new BoxLayout(homeScreen, BoxLayout.X_AXIS));
    homeScreen.setPreferredSize(new Dimension(320, 320));

    JLabel bg = new JLabel();
    ImageIcon icon = new ImageIcon("HomeBG.gif");
    icon.getImage().flush();
    bg.setIcon(icon);
    homeScreen.add(bg);

    easy = new JButton("Easy");
    medium = new JButton("Medium");
    hard = new JButton("Hard");
    play = new JButton("Play");


   //button listeners code here

    homeScreen.add(easy);
    homeScreen.add(medium);
    homeScreen.add(hard);
    homeScreen.add(play);

    snake.add(homeScreen, BorderLayout.CENTER);
    snake.setTitle("Snake Game");
    snake.pack();
    snake.setVisible(true);
    snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GameLayout

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
LeggoMaEggo
  • 512
  • 1
  • 9
  • 24
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Oct 30 '15 at 06:29

2 Answers2

2

You need to change your code as shown below.

snake = new JFrame();
snake.setLayout(new BorderLayout());

//home screen panel
homeScreen = new JPanel(new BorderLayout());
//homeScreen.setLayout(new BoxLayout(homeScreen, BoxLayout.X_AXIS));
homeScreen.setPreferredSize(new Dimension(320, 320)); // probably you need to remove this line!

JLabel bg = new JLabel();
ImageIcon icon = new ImageIcon("HomeBG.gif");
icon.getImage().flush();
bg.setIcon(icon);
homeScreen.add(bg);

easy = new JButton("Easy");
medium = new JButton("Medium");
hard = new JButton("Hard");
play = new JButton("Play");


//button listeners code here
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(easy);
buttonsPanel.add(medium);
buttonsPanel.add(hard);
buttonsPanel.add(play);

homeScreen.add(buttonsPanel, BorderLayout.NORTH);

snake.add(homeScreen, BorderLayout.CENTER);
snake.setTitle("Snake Game");
snake.pack();
snake.setVisible(true);
snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • Finally! This adds all the button to the top of the of the game. I'm sure I can take it from here. Thank you so much! – LeggoMaEggo Oct 30 '15 at 06:51
  • 1
    [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Oct 30 '15 at 08:02
  • I always do setPreferredSize. I think its a nice thing to do. Or else it'll be some other size. – LeggoMaEggo Oct 30 '15 at 09:29
  • *"I always do setPreferredSize."* Override it if need be, but don't set it. *"I think its a nice thing to do."* There's no accounting for what people think.. – Andrew Thompson Oct 30 '15 at 18:51
2

I would use a compound layout for this. Put the level buttons in a (panel in a) FlowLayout. Put the play button in a 2nd FlowLayout. Add those panels to the PAGE_START and PAGE_END of a BorderLayout. Add a label containing the GIF to the CENTER of the same border layout.

BTW - the level buttons should be radio buttons (in a button group - BNI).

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LayoutManagersWithIcon {

    private JComponent ui = null;

    LayoutManagersWithIcon() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JPanel levelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        ui.add(levelPanel, BorderLayout.PAGE_START);
        levelPanel.add(new JRadioButton("Easy"));
        levelPanel.add(new JRadioButton("Medium"));
        levelPanel.add(new JRadioButton("Hard"));

        JPanel startPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        ui.add(startPanel, BorderLayout.PAGE_END);
        startPanel.add(new JButton("Play"));

        JLabel label = new JLabel(new ImageIcon(
                new BufferedImage(400, 100, BufferedImage.TYPE_INT_RGB)));
        ui.add(label);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                LayoutManagersWithIcon o = new LayoutManagersWithIcon();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433