0

我使用绝对定位(setBounds 和 null 布局),现在开始练习布局管理器,此代码使用 gridbag 布局,但没有显示一些组件,要么单元格有问题,要么有其他问题,请帮忙!

导入 java.util.StringTokenizer;
导入 java.awt.event.*;
导入 java.awt.*;
导入 javax.swing.*;
类计算器扩展了 JFrame
{
    JButton add,sub,mul,div,sin,cos,tan,clear,negate,inverse,zero,一,二,三,四,五,六,七,八,九,equalTo,percentage,sqrt;
    JTextField 输入;
    GridBagLayout gbl;
    private void addComponent(组件组件,int gridx,int gridy,int gridwidth,int gridheight,Insets insets)
    {
        添加(组件,新 GridBagConstraints(gridx,gridy,gridwidth,gridheight,1.0,1.0,GridBagConstraints.CENTER,GridBagConstraints.BOTH,插图,0,0));
    }
    计算器()
    {
        //setSize(500,400);
        设置可见(真);
        setLayout(gbl=new GridBagLayout());
        设置默认关闭操作(JFrame.EXIT_ON_CLOSE);

        添加=新的JButton(“+”);
        子=新的JButton(“-”);
        mul=new JButton("*");
        div = new JButton("/");
        罪=新的JButton(“罪”);
        cos= 新的 JButton("cos");
        tan=new JButton("tan");
        清除=新的 JButton("C");
        否定=新的JButton(“否定”);
        逆=新的JButton(“1/x”);
        零=新的JButton(“0”);
        一=新的JButton(“1”);
        二=新的JButton(“2”);
        三=新的JButton(“3”);
        四=新的JButton(“4”);
        五=新的JButton(“5”);
        六=新的JButton(“6”);
        七=新的JButton(“7”);
        八 = 新的 JButton("8");
        九=新的JButton(“9”);
        equalTo= 新 JButton("=");
        百分比=新的 JButton("%");
        sqrt=新的 JButton("sqrt");
        输入 = 新的 JTextField(20);

        addComponent(input,0,0,0,1,new Insets(10,10,100,4)); //tldr

        addComponent(add,0,1,1,1,new Insets(4,4,4,4));
        addComponent(sub,1,1,2,1,new Insets(4,4,4,4));
        addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // 这个不显示
        addComponent(div,3,1,4,1,new Insets(4,4,4,4));

        addComponent(sin,0,2,1,2,new Insets(4,4,4,4));
        addComponent(cos,1,2,2,2,new Insets(4,4,4,4));
        addComponent(tan,2,2,3,2,new Insets(4,4,4,4)); // 这个不显示
        addComponent(clear,3,2,4,2,new Insets(4,4,4,4));

        addComponent(negate,0,3,1,3,new Insets(4,4,4,4)); // 这4个不可见
        addComponent(inverse,1,3,2,3,new Insets(4,4,4,4));
        addComponent(zero,2,3,3,3,new Insets(4,4,4,4));
        addComponent(one,3,3,4,3,new Insets(4,4,4,4));
        盒();
    }
    公共静态无效主要(字符串...参数)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            公共无效运行()
            {
                新计算器();
            }
        });
    }
}
4

1 回答 1

5

首先,很好地尝试使用布局!

立即跳出来的事情是价值观正在传递给gridwidthgridheight

addComponent(add,0,1,1,1,new Insets(4,4,4,4));
addComponent(sub,1,1,2,1,new Insets(4,4,4,4));
addComponent(mul,2,1,3,1,new Insets(4,4,4,4)); // this is not displayed
addComponent(div,3,1,4,1,new Insets(4,4,4,4));

基本上这就是说,添加add到网格 x/y 位置 0x1,使用gridwidth/ gridheightof 1x1,到目前为止很好。

然后sub在 x/y 位置1x1添加gridwidth/ gridheightof 1x2,好吧...

然后mul在 x/y 位置2x1添加gridwidth/ gridheightof 1x3,现在我们开始遇到问题......基本上,sub实际上是在我们的单元格中消耗,覆盖了我们的一部分!

gridwidthgridheight允许您定义组件将扩展到多少个单元格,大多数情况下,您希望这是1x1

gridwidth一旦我将所有和gridheight值更正为1x1,我就能得到这个

在此处输入图像描述

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class Calculator extends JFrame {

    JButton add, sub, mul, div, sin, cos, tan, clear, negate, inverse, zero, one, two, three, four, five, six, seven, eight, nine, equalTo, percentage, sqrt;
    JTextField input;
    GridBagLayout gbl;

    private void addComponent(Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets) {
        add(component, new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets, 0, 0));
    }

    Calculator() {
        //setSize(500,400);
        setLayout(gbl = new GridBagLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        add = new JButton("+");
        sub = new JButton("-");
        mul = new JButton("*");
        div = new JButton("/");
        sin = new JButton("sin");
        cos = new JButton("cos");
        tan = new JButton("tan");
        clear = new JButton("C");
        negate = new JButton("neg");
        inverse = new JButton("1/x");
        zero = new JButton("0");
        one = new JButton("1");
        two = new JButton("2");
        three = new JButton("3");
        four = new JButton("4");
        five = new JButton("5");
        six = new JButton("6");
        seven = new JButton("7");
        eight = new JButton("8");
        nine = new JButton("9");
        equalTo = new JButton("=");
        percentage = new JButton("%");
        sqrt = new JButton("sqrt");
        input = new JTextField(20);

        addComponent(input, 0, 0, 0, 1, new Insets(10, 10, 100, 4)); //tldr

        addComponent(add, 0, 1, 1, 1, new Insets(4, 4, 4, 4));
        addComponent(sub, 1, 1, 1, 1, new Insets(4, 4, 4, 4));
        addComponent(mul, 2, 1, 1, 1, new Insets(4, 4, 4, 4)); // this is not displayed
        addComponent(div, 3, 1, 1, 1, new Insets(4, 4, 4, 4));

        addComponent(sin, 0, 2, 1, 1, new Insets(4, 4, 4, 4));
        addComponent(cos, 1, 2, 1, 1, new Insets(4, 4, 4, 4));
        addComponent(tan, 2, 2, 1, 1, new Insets(4, 4, 4, 4)); // this is not displayed
        addComponent(clear, 3, 2, 1, 1, new Insets(4, 4, 4, 4));

        addComponent(negate, 0, 3, 1, 1, new Insets(4, 4, 4, 4)); // these 4 are not visible
        addComponent(inverse, 1, 3, 1, 1, new Insets(4, 4, 4, 4));
        addComponent(zero, 2, 3, 1, 1, new Insets(4, 4, 4, 4));
        addComponent(one, 3, 3, 1, 1, new Insets(4, 4, 4, 4));
        pack();
        setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Calculator();
            }

        });
    }

}
于 2013-10-19T06:45:20.427 回答