简体   繁体   中英

Can't center JButtons

I need some help centering my JButtons. Right now, they are centered horizontally, but they refuse to center vertically. Here is the code for the two classes Game and Menu. Also, is there anyway to put spaces between my buttons using blocklayout.

Game class:

package main;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame{

    private static final long serialVersionUID = -7071532049979466544L;
    public static final int WIDTH = 1280, HEIGHT = WIDTH/12 * 9;
    private Handler handler;
    public STATE state = STATE.Menu;
    JPanel screen;

    //State of the game
    public enum STATE{
        Menu,
        DeckConstructor,
        Game;
    }

    //Game Launch
    public Game() {

        screen = new JPanel(new CardLayout());
        screen.add(new Menu(this).getMenu(), "MENU");
        screen.add(new DeckConstructor(this).getDeckConstructor(), "DECK_CONSTRUCTOR");

        getContentPane().add(screen,BorderLayout.CENTER);
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        pack();
        setVisible(true);

    }

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

Menu class:

package main;
import java.awt.event.*;
import java.awt.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;


public class Menu extends JFrame implements ActionListener{

    private JPanel buttonPanel;
    private JButton play;
    private JButton deckConstructor;
    private JButton quit;
    private Game game;


    public Menu(Game game){
        this.game = game;
        buttonPanel = new JPanel();

        play = new JButton("Play");
        play.addActionListener(this);
        deckConstructor = new JButton("Deck Constructor");
        deckConstructor.addActionListener(this);
        quit = new JButton("Quit");
        quit.addActionListener(this);

        buttonPanel.add(play);
        buttonPanel.add(deckConstructor);
        buttonPanel.add(quit);
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
        play.setAlignmentX(Component.CENTER_ALIGNMENT);
        deckConstructor.setAlignmentX(Component.CENTER_ALIGNMENT);
        quit.setAlignmentX(Component.CENTER_ALIGNMENT);

    }

    public JPanel getMenu(){
        return buttonPanel;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(deckConstructor.equals((JButton) e.getSource())){
            CardLayout c1 = (CardLayout)(game.screen.getLayout());
            c1.show(game.screen, "DECK_CONSTRUCTOR");
        }

    }
}

Instead of a BoxLayout, I used GridBagLayout and GridBagConstraints .

Code:

public Menu(Game game) {
    this.game = game;
    buttonPanel = new JPanel(new GridBagLayout());

    play = new JButton("Play");
    play.addActionListener(this);

    deckConstructor = new JButton("Deck Constructor");
    deckConstructor.addActionListener(this);

    quit = new JButton("Quit");
    quit.addActionListener(this);

    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 0;
    c.anchor = GridBagConstraints.CENTER;
    // top, left, bottom, right
    c.insets = new Insets(0, 0, 50, 0);
    buttonPanel.add(play, c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    c.anchor = GridBagConstraints.CENTER;
    // top, left, bottom, right
    c.insets = new Insets(0, 0, 50, 0);
    buttonPanel.add(deckConstructor, c);

    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 2;
    c.anchor = GridBagConstraints.CENTER;
    buttonPanel.add(quit, c);
}

To center it, I used GridBagConstraints#anchor and to add spacing, I used GridBagConstraints#insets .

The original frame:

在此处输入图片说明

Frame using GridBagLayout:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM