使用GridBagLayout的不受欢迎的结果

时间:2015-06-23 06:05:14

标签: java swing layout-manager gridbaglayout

我一直在尝试通过Youtube视频学习,但我必须有一些关于GridBagLayout的内容。

基本上我期望的是4个按钮,一个在另一个下面,高度为2个网格,但似乎无论我设置gridheight的值是什么,按钮都保持相同的大小

可编辑的代码:

package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class Test{

 public static void main(String []args){
    System.out.println("Hello World");

    JFrame jay = new JFrame("test");
    JPanel jp = new JPanel(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    JButton j1 = new JButton("Button 1");
    JButton j2 = new JButton("Button 22222222222222");
    JButton j3 = new JButton("Button 333333");
    JButton j4 = new JButton("4");

    gc.gridx = 0; gc.gridy = 0;
    gc.insets = new Insets(5,10,5,10);
    gc.gridheight = 2; //This is where the problem is;
    gc.fill = GridBagConstraints.BOTH;
    jp.add(j1,gc);
    gc.gridx = 0; gc.gridy = 2;
    jp.add(j2,gc);
    gc.gridx = 0; gc.gridy = 4;
    jp.add(j3,gc);
    gc.gridx = 0; gc.gridy = 6;
    jp.add(j4,gc);
    jay.add(jp);
    jay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jay.setSize(300,350);
    jay.setVisible(true);

 }
}

如果有人解释我是什么决定了网格的大小?它只是组件大小?

1 个答案:

答案 0 :(得分:2)

gridheight指定组件将展开的行数,假设这些行中有组件(任何空行将自动调整为0

  

那我怎么去实现更高的按钮呢?添加空白组件?

一种可能的解决方案是使用ipady添加到组件的preferredSize属性中(您也可以使用ipadx作为宽度)

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();

            JButton j1 = new JButton("Button 1");
            JButton j2 = new JButton("Button 22222222222222");
            JButton j3 = new JButton("Button 333333");
            JButton j4 = new JButton("4");

            gc.insets = new Insets(5, 10, 5, 10);
            gc.ipady = 50;
            gc.gridwidth = GridBagConstraints.REMAINDER;
            gc.fill = GridBagConstraints.BOTH;
            add(j1, gc);
            add(j2, gc);
            add(j3, gc);
            add(j4, gc);
        }

    }
}

有关详细信息,请查看How to Use GridBagLayout