0

I have tried looking at many documentations and tutorials but none of them seem to work together. I am just trying to make a simple "main menu" for a "game" my friend and I are attempting to make. I am able to move the buttons around when there is no background image present, and I am only able to get the background image but I can't move the buttons around. So my question is how can I position JButtons?

Here is my code & a screenshot: What my frame looks like

    package game;
    import java.awt.FlowLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class MainMenu extends JFrame{

    private JButton singPlay = new JButton("Single Player");
    private JButton twoPlay = new JButton("Two Player");

    public MainMenu()
    {   
        JFrame frame = new JFrame("TestTEST");
        JPanel panel = new JPanel();

        frame.setSize(400,500);
        frame.setLocation(700,300);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Austin\\Pictures\\Landscape.jpg")));

        frame.setLayout(new FlowLayout());
        frame.add(singPlay);
        frame.add(twoPlay);

        frame.setSize(399,499);
        frame.setSize(400, 500);
    }

    public static void main(String[] args)
    {
        new MainMenu();

    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • And where do you want the buttons positioned? – MadProgrammer Nov 02 '15 at 20:36
  • I would like the button positioned in the center, vertically like camikr below stated, I tried looking at the documentation, but I just couldn't get it to work with my code. –  Nov 03 '15 at 03:10

4 Answers4

4

So my question is how can I position JButtons?

Use an appropriate layout manager.

You set the layout manager to a FlowLayout so the buttons are displayed at the top of the frame.

Maybe you want to center the buttons on the frame. If so then you can use a GridbagLayout.

Also, don't use setSize(). Use the pack() method. Finally the frame should be made visible AFTER you add the components to the frame.

So your code might look something like:

public MainMenu()
{
    JPanel panel = new JPanel();
    panel.setOpaque(false);
    panel.add(singPlay);
    panel.add(twoPlay);

    JFrame frame = new JFrame("TestTEST");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setContentPane(new JLabel(new ImageIcon("yourfilename.jpg")));
    frame.setLayout(new GridBagLayout());
    frame.add(panel, new GridBagConstraints());
    frame.pack();
    frame.setLocationRelativeTo( null );
    frame.setVisible( true );
}

Or maybe you want the buttons centered vertically. Then you can use a vertical BoxLayout.

Read the Swing tutorial on Layout Managers and decide for yourself what is appropriate.

camickr
  • 321,443
  • 19
  • 166
  • 288
3

Right now you're using a JLabel as a content pane, which doesn't make a ton of sense.

Instead of putting your background in a JLabel, you should extend JPanel and override paintComponent() to draw the background yourself. Here is a great tutorial on performing custom painting.

Once you have your background JPanel, put your JButtons inside that and use it as your content pane.

After that, if you're looking to manually position your JButtons, then you might be looking for a null layout.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Actually, a JLabel isn't a bad idea, if you don't want to resize the background image and are aware that the label won't use the child components to calculate the preferred size. It's just a lot simpler in simple situations, not that I have an issue with using a custom component. Also null layouts are very rarely the solution and introduce a lot more work then they solve – MadProgrammer Nov 02 '15 at 20:34
1

JFrame.setContentFrame() is not how you set a background image. See the selected answer at How to set background image in Java?

Also, you should be doing all these operations in the Swing event thread.

public static void main(String [] args) {
  SwingUtilities.invokeLater( new Runnable() {
     public void run() {
       new MainMenu();
     }
  });
}
Community
  • 1
  • 1
pcarter
  • 1,516
  • 14
  • 21
  • Actually, using setContentPane is a very good way to set the background component of a JFrame. You're linked answer isn't a good example for two reasons, one, it's about applets (and even for JApplet, setContentPane is a good choice) and two, you should never extend paint of top level containers, one of the reasons been because of the JRootPane and contentPane sit over the top of it. – MadProgrammer Nov 02 '15 at 20:29
1

A simple solution is to use GridBagLayout, the following will centre the buttons, on on top of each, vertically and horizontally in the container

public MainMenu() {
    JFrame frame = new JFrame("TestTEST");
    JPanel panel = new JPanel();

    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new JLabel(new ImageIcon("C:\\Users\\Austin\\Pictures\\Landscape.jpg")));

    frame.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.insets = new Insets(4, 4, 4, 4);
    gbc.fill = GridBagConstraints.HORIZONTAL;       frame.add(singPlay, gbc);
    frame.add(twoPlay, gbc);

    frame.pack();
    frame.setLocationRelativeTo(null);

    frame.setVisible(true);
}

As a side note, while JLabel work in this case, you need to be ware that it will calculate it's preferred size from the icon (and text) properties of the label itself, not it's children, JLabel really wasn't designed to hold other components

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366