简体   繁体   English

无法将JButton居中

[英]Can't center JButtons

I need some help centering my JButtons. 我需要一些帮助使JButton居中。 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. 另外,还是可以使用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 . 我使用GridBagLayoutGridBagConstraints代替BoxLayout。

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 . 为了使其居中,我使用了GridBagConstraints#anchor并添加了间距,我使用了GridBagConstraints#insets

The original frame: 原始框架:

在此处输入图片说明

Frame using GridBagLayout: 使用GridBagLayout的框架:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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