JButton 的 GridBagLayout 格式化

GridBagLayout formatting for JButtons

我和我的搭档正在为一个游戏编写这个程序。他将 GridBagLayout 用于我们的网格,我正在尝试解决网格的一些问题。

代码如下:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

@SuppressWarnings("serial")
public class NewGame extends JFrame{

    private int width = 500, height = 500, xSquares = 4, ySquares = 4;
    Font buttonFont = new Font("Times New Roman", Font.PLAIN, 15);
    endGame end = new endGame();
    
    public NewGame() {
        super("OnO");

        // gridbaglayout is flexible but kinda complicated
        GridBagLayout Griddy = new GridBagLayout();
        this.setLayout(Griddy);
        JPanel p = new JPanel();
        p.setLayout(Griddy);
        this.add(p);

        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.BOTH;

        c.weightx = 1;
        c.weighty = 1;

        c.gridwidth = 4;
        c.gridheight = 4;

        NewBoard board = new NewBoard(xSquares, ySquares);
        // board at top left
        c.gridx = 0;
        c.gridy = 0;

        this.add(board, c);
        
        // find a way to make buttons smaller
                        
        JButton EndGame = new JButton("End");
        EndGame.setBackground(Color.black);
        EndGame.setForeground(Color.white);
        EndGame.setFont(buttonFont);
        EndGame.addActionListener(end);

        JButton Undo = new JButton("Undo");
        Undo.setBackground(Color.black);
        Undo.setForeground(Color.white);
        Undo.setFont(buttonFont);

        JButton NewGame = new JButton("New Game");
        NewGame.setBackground(Color.black);
        NewGame.setForeground(Color.white);
        NewGame.setFont(buttonFont);

        // fit 3
        c.gridwidth = 1;
        c.gridheight = 1;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(EndGame, c);
        
        c.gridx = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(Undo, c);
        
        c.gridx = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        p.add(NewGame, c); 
        
 
        this.setPreferredSize(new Dimension(width, height));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pack();
        this.setLocation(450, 30);
        this.setVisible(true);
    }
    
    public class endGame implements ActionListener {
        public void actionPerformed(ActionEvent event) {
                System.exit(0);
            } 
    }
    
    public class newGame implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            
        }
    }
    
}

当这个代码运行与我们程序中的另一个类时,endGameUndoNewGame按钮与面板重叠:

我想找到一种方法将 3 个按钮单独显示在板的上方或下方 space,但是我修改了很长时间的代码,但找不到解决方案然而。我不确定我应该在这里做什么。

首先,变量名不应该以大写字符开头。学习并遵循 Java 命名约定。

I want to find a way to display the 3 buttons either above or below the board

最简单的解决方案是不为框架使用 GridBagLayout。

只需使用框架的默认 BorderLayout。然后,您可以将 JPanel 与 FlowLayout 用于按钮,将 GridLayout 用于电路板面板。

基本逻辑是:

JPanel buttons = new JPanel(); 
buttons.add(endGame);
buttons.add(undo);
buttons.add(newGame);
this.add(button, BorderLayout.PAGE_START);

NewBoard board = new NewBoard(xSquares, ySquares);
this.add(board, BorderLayout.CENTER);

默认情况下,JPanel 使用 FlowLayout,因此按钮将显示在一行中。

阅读 Layout Managers 上的 Swing 教程以获取更多信息和工作示例,以更好地理解此建议的工作原理。