TAGS :Viewed: 8 - Published at: a few seconds ago

[ How to position a JButton around the screen? ]

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();

    }
}

Answer 1


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.

Answer 2


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.

Answer 3


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

Answer 4


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