0

Alright, I figured out how to add a background picture but how do i make the buttons show. I'm making pong if you're interested.

Here is my Code:

public class Gui extends JFrame{
private JButton JB;
private JButton EB;

public Gui(){
    super("Pong");

    JPanel outside = new JPanel();
    JPanel inside = new JPanel();
    setLayout(new BorderLayout());
    this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));

    outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS));
    inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));

    outside.add(Box.createHorizontalStrut(280));
    outside.add(inside);
    outside.add(Box.createHorizontalStrut(20));

    inside.add(Box.createVerticalStrut(20));
    JLabel title = new JLabel("Pong");
    inside.add(title);
    inside.add(Box.createVerticalStrut(20));

    JButton btt1 = new JButton("Start");
    Dimension d = new Dimension(200,40);

    btt1.setSize(d);
    btt1.setMinimumSize(d);
    btt1.setMaximumSize(d);
    btt1.setPreferredSize(d);

    JButton btt2 = new JButton("Credits");
    btt2.setSize(d);
    btt2.setMinimumSize(d);
    btt2.setMaximumSize(d);
    btt2.setPreferredSize(d);
    JButton btt3 = new JButton("Exit");
    btt3.setSize(d);
    btt3.setMinimumSize(d);
    btt3.setMaximumSize(d);
    btt3.setPreferredSize(d);

    inside.add(btt1);
    inside.add(Box.createVerticalStrut(5));
    inside.add(btt2);
    inside.add(Box.createVerticalStrut(5));
    inside.add(btt3);
    inside.add(Box.createVerticalStrut(20));

    add(outside);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800,600);
    this.setVisible(true);
    this.setResizable(false);


}

}

Thanks for responding.

Neithrik
  • 2,002
  • 2
  • 20
  • 33
  • 1
    The reason your buttons aren't showing is provided in the answer below; you've replaced the content pane with a `JLabel`. I've marked this question as a duplicate because I believe the linked question is the fundamental reason for and solution to your issue. You'll have to take a different approach to setting the background image, rather than using a `JLabel` in this way. If you believe I did this in error, let me know. – Jason C Apr 16 '15 at 18:33

1 Answers1

2
//setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
setLayout(new BorderLayout());

You set the layout but then you replace the content pane of the frame so you loose the layout manager.

You need to set the layout after you set the content pane.

Note you can only use the label as the background is the image of the label is larger than the components being added to the label.

Also, don't attempt to manipulate the size of the buttons by using setSize(), setPreferredSize(), setMinimumSize() and setMaximumSize(). Let the button display at its preferred size.

If you want the button to all be the same size, then add the buttons to a panel using a GridLayout first. You can specify the spacing between components when you create the GridLayout. Or you can use a GridBagLayout, which will allow you so specify a constraint that "fills" the width of each cell.

camickr
  • 321,443
  • 19
  • 166
  • 288