java - 无法居中 JButtons

标签 java swing layout-manager

我需要一些帮助来使我的 JButton 居中。现在,它们水平居中,但拒绝垂直居中。这是游戏和菜单这两个类的代码。另外,是否可以使用 blocklayout 在我的按钮之间放置空格。

游戏类:

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

菜单类:

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

    }
}

最佳答案

我使用了 GridBagLayout 而不是 BoxLayout|和 GridBagConstraints .

代码:

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

为了让它居中,我使用了 GridBagConstraints#anchor并添加间距, 我用了GridBagConstraints#insets .

原始框架:

enter image description here

使用 GridBagLayout 的框架:

enter image description here

关于java - 无法居中 JButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31282954/

相关文章:

java - PDFBox:将 PDF 转换为图像比预期慢

java - v7 CardView 阴影在禁用硬件加速的 Android 5.0 上消失

java - 添加 JPanel 时仅显示 JFrame 并且不绘制任何内容

java - drawRect() 在某些颜色上无法正常工作

java - 选择列表中的项目后在面板上设置标签

java - Java Socket/Client 之间无法读写

java - JFrame 表单创建(多行、多列和输入)

java - Swing:对齐 [dynamic][static][dynamic] 组件宽度的最简单方法?

java - 请帮助我使用 Java 中的 JFrame 和 BorderLayout

java - 使用 Future 与 CompletableFuture 的 invokeall()