微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 GridBagLayout 在多列上拉伸组件?

如何解决如何使用 GridBagLayout 在多列上拉伸组件?

我一直在学习 Java Swing 中的布局,目前正在制作一个简单的计算器。我将使用文本字段进行输入/输出,我想将它拉伸到 2 列(我总共使用了三列),但是当我尝试拉伸它时,它会调整第一列中项目的大小。这是代码和屏幕截图。

应用截图:

https://i.stack.imgur.com/fCACd.png

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.*;

public class Calculator extends JFrame{

    JButton but1,but2,but3,but4,but5,but6,but7,but8,but9,but0,butPlus,butMinus,clearall;

    JTextField textResult;

    int num1,num2;

    public static void main(String[] args){
    
        new Calculator();
        
    }

    public Calculator(){
            
        this.setSize(400,400);
        this.setLocationRelativeto(null);
        this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Calculator");
    
        JPanel thePanel = new JPanel();
    
        thePanel.setLayout(new GridBagLayout());
    
        GridBagConstraints gridConstraints = new GridBagConstraints();
    
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 0;
        gridConstraints.gridwidth = 1;
        gridConstraints.gridheight = 1;
        gridConstraints.weightx = 50;
        gridConstraints.weighty = 100;
        gridConstraints.insets = new Insets(5,5,5);
        gridConstraints.anchor = GridBagConstraints.CENTER;
        gridConstraints.fill = GridBagConstraints.BOTH;
    
        textResult = new JTextField("0",20);
    
        Font font = new Font("Helvetica",Font.PLAIN,18);
        textResult.setFont(font);
    
        but1 = new JButton("1");
        but2 = new JButton("2");
        but3 = new JButton("3");
        but4 = new JButton("4");
        but5 = new JButton("5");
        but6 = new JButton("6");
        but7 = new JButton("7");
        but8 = new JButton("8");
        but9 = new JButton("9");
        butPlus = new JButton("+");
        but0 = new JButton("0");
        butMinus = new JButton("-");
        clearall = new JButton("C");
    
        thePanel.add(clearall,gridConstraints);
        gridConstraints.gridx = 1;
        gridConstraints.gridwidth = 2;
    
        thePanel.add(textResult,gridConstraints);
        gridConstraints.gridwidth = 1;
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 1;
        thePanel.add(but1,gridConstraints);
        gridConstraints.gridx = 1;
        thePanel.add(but2,gridConstraints);
        gridConstraints.gridx = 2;
        thePanel.add(but3,gridConstraints);
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 2;
        thePanel.add(but4,gridConstraints);
        gridConstraints.gridx = 1;
        thePanel.add(but5,gridConstraints);
        gridConstraints.gridx = 2;
        thePanel.add(but6,gridConstraints);
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 3;
        thePanel.add(but7,gridConstraints);
        gridConstraints.gridx = 1;
        thePanel.add(but8,gridConstraints);
        gridConstraints.gridx = 2;
        thePanel.add(but9,gridConstraints);
        gridConstraints.gridx = 0;
        gridConstraints.gridy = 4;
        thePanel.add(butPlus,gridConstraints);
        gridConstraints.gridx = 1;
        thePanel.add(but0,gridConstraints);
        gridConstraints.gridx = 2;
        thePanel.add(butMinus,gridConstraints);
    
    
        this.add(thePanel);
    
        this.setVisible(true);
    }
}

解决方法

注意:

摆动偏移/索引从 0 开始。

通常当人们使用 GridBagLayout 时,gridx/gridy 将从 0 开始。

但是,我看到您正在使用起始值为 1 的 gridx/gridy。这应该可以工作。这只是意味着第 0 列和第 0 行的大小为 0。

为了与大多数人使用 GridBagLayout 的方式保持一致,您可能需要将代码更改为使用 0 作为 gridx/gridy 的起始偏移量。

我下面的建议假设您将代码转换为使用 0 作为网格的偏移量。

gridConstraints.gridx = 5;
gridConstraints.gridwidth = 20;
thePanel.add(textResult,gridConstraints);

您只有 3 列。你不能随便给一个组件:

  1. gridx 为 5,它只能是 0、1、2。在这种情况下,你想要 1
  2. 网格宽度为 20,它只能是 1、2、3。在这种情况下,您需要 2。

如果你保持 1 作为起始网格偏移,那么你需要为 gridx 使用 2。

编辑:

它调整第一列中项目的大小

我认为问题如下:

textResult = new JTextField("0",20);

20 告诉文本字段调整自身大小以显示 20 个“W”字符(这使得文本字段相对于按钮变大。

因此第 1 列和第 2 列中的按钮将是文本字段大小的一半。

也许试试:

textResult = new JTextField("0",10);

看看是否更好。

或者也许更好的选择是使用:

textResult = new JTextField("0");

现在文本字段的大小将由按钮的大小控制。

,

换行

textResult = new JTextField("0",20);

textResult = new JTextField("0");

问题在于您的文本字段的最小宽度大于 2 列的总和,因此它会拉伸 2 列,从而拉伸其他所有内容。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。