JButton 的 GridBagLayout 格式 [英] GridBagLayout formatting for JButtons

查看:39
本文介绍了JButton 的 GridBagLayout 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My partner and I are writing this program for a game. He used GridBagLayout for our grid and I'm trying to troubleshoot some problems with the grid.

代码如下:

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 按钮与棋盘重叠:

When this code is run with the other classes in our program, the endGame, Undo, and NewGame buttons overlap with the board:

我想找到一种方法,在单独的空间中显示板子上方或下方的 3 个按钮,但我已经修改了很长时间的代码,但还没有找到解决方案.我不确定我应该在这里做什么.

I want to find a way to display the 3 buttons either above or below the board in its individual space, but I've tinkered with the code for a long time and can't find the solution yet. I'm not sure what I should be doing here.

推荐答案

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

First of all, variable names should NOT start with an upper case character. Learn and follow Java naming conventions.

我想找到一种方法来显示板子上方或下方的 3 个按钮

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

最简单的解决方案是不对框架使用 GridBagLayout.

Simplest solution is to not use a GridBagLayout for the frame.

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

Just use the default BorderLayout of the frame. Then you can use a JPanel with the FlowLayout for your buttons, and a GridLayout for your board panel.

基本逻辑是:

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,因此按钮将显示在一行上.

By default a JPanel uses a FlowLayout so the buttons will be displayed on a single row.

阅读关于布局管理器的Swing教程了解更多信息信息和工作示例,以更好地了解此建议的工作原理.

Read the Swing tutorial on Layout Managers for more information and working examples to better understand how this suggestion works.

这篇关于JButton 的 GridBagLayout 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆