繁体   English   中英

Java GridBagLayout 无法正确设置我的 GUI

[英]Java GridBagLayout can't properly set my GUI

我正在尝试制作一个井字游戏。 对于 3x3 tictactoe 表,我使用了 9 个按钮。 但是,按钮上方和下方的 jtext 似乎更改了每个 3x3 按钮的长度。 有没有办法可以将 3x3 按钮设置为相同大小而不受其他组件的干扰。

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Dimension;


public class makeGUI {

    JFrame frame;

    public void initialise() {

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        JPanel playPanel = new JPanel();

        playPanel.setPreferredSize(new Dimension(300, 500));

        playPanel.setBackground(Color.WHITE);


        playPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JTextField field = new JTextField(5);
        field.setEditable(false);
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = 0;
        playPanel.add(field, c);




        JButton button = new JButton("");
        //c.weightx = 0.5;  
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 2;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);


        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);

        button = new JButton("");
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 3;
        c.gridwidth = 1;
        button.setPreferredSize(new Dimension(40, 40));
        playPanel.add(button, c);






        field = new JTextField(5);
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 4;
        playPanel.add(field, c);

        button = new JButton("Submit");
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 4;
        playPanel.add(button, c);

        frame.add(playPanel);
        frame.setTitle("A Simple Card Game");
        frame.setSize(400, 700);
        frame.pack();
        frame.setVisible(true);

    }

}

这就是我现在所拥有的:

在此处输入图片说明

我想要这样的东西:

在此处输入图片说明

这就是我将这个 GUI 分成几个部分的方式:

在此处输入图片说明

  • 中间的红色边框区域, GridLayout 网格布局将确保每个单元格的宽度都是它所包含的最高组件的最宽和高度。
  • 底部的绿色边框区域将是一个居中的FlowLayout
  • 蓝色边框外面板将使用BorderLayout 这反过来将包含:
    • PAGE_START的标签
    • CENTER的网格布局
    • PAGE_ENDFlowLayout

暂无
暂无

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

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