GridBagLayout无法正常工作

时间:2013-10-19 06:21:34

标签: java swing layout gridbaglayout

我使用绝对定位(setBounds和null布局) 现在开始练习布局经理, 此代码使用gridbag布局,但很少有组件未显示,或者单元格或其他内容存在一些问题 请帮忙!

import java.util.StringTokenizer;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
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);
        setVisible(true);
        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,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));

        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)); // this is not displayed
        addComponent(clear,3,2,4,2,new Insets(4,4,4,4));

        addComponent(negate,0,3,1,3,new Insets(4,4,4,4)); // these 4 are not visible
        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));
        pack();
    }
    public static void main(String...args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Calculator();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:4)

首先,为使用布局做得好!

立即跳出来的是值传递给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 / gridheight 1x1,到目前为止很好。

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

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

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

我将所有gridwidthgridheight值更正为1x1后,我就能得到这个

enter image description here

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

        });
    }

}