简体   繁体   English

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

[英]How do I stretch a component over multiple columns using GridBagLayout?

I've been learning about layouts in Java Swing and am currently making a simple calculator.我一直在学习 Java Swing 中的布局,目前正在制作一个简单的计算器。 I will be using a text field for input/output and I want to stretch it over 2 columns(i am using three total) but when I try to stretch it, it resizes items in the first column.我将使用文本字段进行输入/输出,我想将其拉伸到 2 列(我总共使用了三列),但是当我尝试拉伸它时,它会调整第一列中项目的大小。 Here's the code and a screenshot.这是代码和屏幕截图。

Screenshot of the app:应用截图:

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

Note:笔记:

Swing offsets/indexes start at 0.摆动偏移/索引从 0 开始。

Normally when people use the GridBagLayout the gridx/gridy would start at 0.通常当人们使用 GridBagLayout 时,gridx/gridy 将从 0 开始。

However, I see that you are using gridx/gridy with starting values of 1. This should work.但是,我看到您正在使用起始值为 1 的 gridx/gridy。这应该可以工作。 It will just mean that column 0 and row 0 will have a size of 0.这只是意味着第 0 列和第 0 行的大小为 0。

To be consistent with how most people use the GridBagLayout, you may want to change your code to use 0 as the starting offset for gridx/gridy.为了与大多数人使用 GridBagLayout 的方式保持一致,您可能希望将代码更改为使用 0 作为 gridx/gridy 的起始偏移量。

My advice below assumes you will convert your code to use 0 as the offsets for the grids.我下面的建议假设您将代码转换为使用 0 作为网格的偏移量。

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

You only have 3 columns.您只有 3 列。 You can't just randomly give a component:你不能只是随机给出一个组件:

  1. a gridx of 5, it can only be 0, 1, 2. In this case you want 1 gridx 为 5,它只能是 0、1、2。在这种情况下,你想要 1
  2. a gridwidth of 20, it can only be 1, 2, 3. In this case you want 2.网格宽度为 20,它只能是 1、2、3。在这种情况下,您需要 2。

If you keep 1 as the starting grid offset then you would need to use 2 for the gridx.如果您将 1 作为起始网格偏移量,那么您需要为 gridx 使用 2。

Edit:编辑:

it resizes items in the first column它调整第一列中项目的大小

I think the issue is the following:我认为问题如下:

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

The 20 tells the text field to size itself to display 20 "W" characters (which makes the text field large relative to the buttons. 20 告诉文本字段调整自身大小以显示 20 个“W”字符(这使得文本字段相对于按钮变大。

Therefore the buttons in columns 1 and 2 will be half the size of the text field.因此,第 1 列和第 2 列中的按钮将是文本字段大小的一半。

Maybe try:也许尝试:

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

to see if that is any better.看看是否更好。

Or maybe a better option is to just use:或者也许更好的选择是使用:

textResult = new JTextField("0");

Now the size of the text field will be controlled by the size of the buttons.现在文本字段的大小将由按钮的大小控制。

Change the line换线

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

to

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

The problem was that your text field has a minimum width that is larger than 2 of your columns combined so it's stretching the 2 columns which stretch everything else.问题是您的文本字段的最小宽度大于 2 列的总和,因此它会拉伸 2 列,从而拉伸其他所有内容。

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

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