从GridBagLayout开始

时间:2017-07-22 21:54:28

标签: java swing gridbaglayout

我的问题是,当我不熟悉ipadx和ipady时。我的按钮粘在一起,但是当我用ipadx和ipady弄乱时,我发现第三个按钮移动了一个细胞。为什么以及如何解决这个烂摊子? 代码:

public class Testagain extends JFrame {

    private JPanel pan1;
    private JPanel pan2;
    private JPanel pan3;


    private JButton btn1;
    private JButton btn2;
    private JButton btn3;


    public Testagain()
    {
        super("Panel TEST GRID");
        super.setSize(500, 500);

        btn1 = new JButton(" 1");
        pan1 = new JPanel(new GridBagLayout());
        GridBagConstraints con1 = new GridBagConstraints();
        con1.gridx = 0;
        con1.gridy = 0;
        con1.ipadx = 80;
        con1.ipady = 60;
        pan1.add(btn1,con1);

        btn2 = new JButton("2");
        pan2 = new JPanel(new GridBagLayout());
        GridBagConstraints con2 = new GridBagConstraints();
        con2.ipady = 60;
        con2.ipadx = 0;
        con2.gridx = 1;
        con2.gridy = 0;
        pan1.add(btn2,con2);



        btn3 = new JButton("3");
        pan3 = new JPanel(new GridBagLayout());
        GridBagConstraints con3 = new GridBagConstraints();
        con3.ipady = 60;
        con3.ipadx = 0;
        con3.gridx = 2;
        con3.gridy = 0;


        pan3.add(btn3,con3);


        Container tain = super.getContentPane();
        tain.setLayout(new GridBagLayout());

        tain.add(pan1,con1);
        tain.add(pan2,con2);
        tain.add(pan3,con3);






        super.setVisible(true);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

}

1 个答案:

答案 0 :(得分:1)

您要将btn1btn2添加到pan1,因此pan2已被赋予其自己的单元格...

btn1 = new JButton(" 1");
pan1 = new JPanel(new GridBagLayout());
//...
pan1.add(btn1,con1);

btn2 = new JButton("2");
pan2 = new JPanel(new GridBagLayout());
//...
pan1.add(btn2,con2);

所以,我稍微使用了你的代码,删除了一些复杂的问题,这些复杂性让我头脑发麻,并且能够产生预期的输出

Expected Output

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;

public class Testagain extends JFrame {

    private JPanel pan1;
    private JPanel pan2;
    private JPanel pan3;

    private JButton btn1;
    private JButton btn2;
    private JButton btn3;

    public Testagain() {
        super("Panel TEST GRID");
        super.setSize(500, 500);

        GridBagConstraints con1 = new GridBagConstraints();
        btn1 = new JButton(" 1");
        btn2 = new JButton("2");
        btn3 = new JButton("3");

        Container tain = super.getContentPane();
        tain.setLayout(new GridBagLayout());

        con1.gridx = 0;
        con1.gridy = 0;
        con1.ipadx = 80;
        con1.ipady = 60;
        tain.add(btn1, con1);
        con1.ipadx = 0;
        con1.gridx = 1;
        con1.gridx = 60;
        tain.add(btn2, con1);
        con1.gridx = 2;
        tain.add(btn3, con1);

        super.setVisible(true);
        super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

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

我不知道为什么你使用中间面板来简单地添加一个按钮,但由于按钮和面板使用相同的约束,对我来说似乎有些“奇怪”,但这可能是一个更大的快照,我不知道

相关问题