Mikkel
Mikkel

Reputation: 1811

Jbutton position on my frame in java dosen't work

I'm making my first 2d game at the moment and working on a menu which contains a Jbutton named "New Game" and a big header.

The problem is that the button keeps being listed at the top of the frame like this:

enter image description here

Have tried to do it with setbounds, but that don't work. Some code of my button:

    private void loadbuttons() {

    JButton button = new JButton("New Game"); 
    button.setBackground(Color.black);
    button.setBorderPainted(false);
    button.setForeground(Color.green);
    button.setLayout(null);
    button.setBounds(100, 0, 220, 500); 

    add(button);

}

What have a done wrong?? Want it to be positioned under the Header :)

EDIT

I have made my 2 frames in this code:

public Application() {

    Menu(); // Game Ui method runs under        

    if (startgame == 1) {
        Gameboard();
    }

}

private void Gameboard() {
    add(new Board()); // We add the board to the center of the JFrame container
    setSize(555, 578);        
    setTitle("Pacmania"); // Sets title
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null); // Sets the window on the center of the screen

}    

private void Menu() {
    add(new Menu(startgame)); // We add the menu to the center of the JFrame container
    setSize(400, 520);        
    setTitle("Pacmania"); // Sets title
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null); // Sets the window on the center of the screen

}

public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            Application ex = new Application();
            ex.setVisible(true); // Starting and showing the board
        }
    });
}

Have i done something wrong here?

Edit 2:

Here is my code where i put my Jbutton.. this class is used to make a header + buttons on my menu and if i later going to get some images on.

public class Menu extends JPanel {

private int startgame;
private Image background;


public Menu(int startgame) {
    this.startgame = startgame;
    setFocusable(true);
    setDoubleBuffered(true);
    setBackground(Color.BLACK);

    loadbuttons();

}

private void loadbuttons() {

    JLabel lbj = new JLabel("Pacmania");
    add(lbj);

    JButton button = new JButton("New Game"); 
    button.setBackground(Color.black);
    button.setBorderPainted(false);
    button.setForeground(Color.green);
    button.setLayout(null);         
    add(button);



}

public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2d = (Graphics2D)g;

    if (startgame == 0) {

        g.setColor(Color.green);
        Font big = new Font("Serif", Font.BOLD, 40);            
        Font small = new Font("Serif", Font.BOLD, 20);

        String msg = "Pacmania";            
        FontMetrics metr = this.getFontMetrics(big);
        g.setFont(big);
        g.drawString(msg, (400 - metr.stringWidth(msg)) / 2,
                520 / 4);
    }

}

}

What i want it too look like:

enter image description here

Upvotes: 0

Views: 120

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Your button is being set likely with a JPanel's default FlowLayout. Suggestion: use a different layout, not sure which ones, as I don't know what your desired GUI should look like, but better layouts that will help you better set the button's position. A bad suggestion that you'll here is to use null layouts. Just avoid these if at all possible since they lead to the production of rigid GUI's that may look good on one platform and screen resolution, but look terrible on all others and are a total witch to debug or enhance.

Then yes, Nitroman is right, you could use a GridBagLayout and place your game title JLabel above your JButton. For example:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class Pacmania extends JPanel {
   private static final String TITLE_TEXT = "Pacmania";
   private static final int PREF_W = 400;
   private static final int PREF_H = PREF_W;
   private static final Color BACKGROUND = Color.black;
   private static final Color FOREGROUND = Color.green;
   private static final Font TITLE_FONT = new Font(Font.SERIF, Font.BOLD, 46);
   private static final int I_GAP = 10;

   public Pacmania() {
      setBackground(BACKGROUND);
      setLayout(new GridBagLayout());

      JLabel label = new JLabel(TITLE_TEXT, SwingConstants.CENTER);
      label.setFont(TITLE_FONT);
      label.setForeground(FOREGROUND);

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 0;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.weightx = 0.0;
      gbc.weighty = 0.0;
      gbc.insets = new Insets(I_GAP, I_GAP, I_GAP, I_GAP);
      add(label, gbc);

      JButton newGameButton = new JButton(new NewGameAction("New Game"));
      gbc.gridy = 1;
      newGameButton.setBackground(BACKGROUND);
      newGameButton.setForeground(FOREGROUND);
      newGameButton.setBorderPainted(false);
      newGameButton.setFocusPainted(false);
      add(newGameButton, gbc);

   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private class NewGameAction extends AbstractAction {
      public NewGameAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         // TODO Finish this!
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Pacmania");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new Pacmania());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

The code is tweakable by changing the values held by its constants.

Upvotes: 0

Steampunkery
Steampunkery

Reputation: 3874

Use Grid Bag Constraints. Here's a good tutorial on it.

Upvotes: 1

Related Questions