0

我无法将任何相关帖子转移到我的简单问题。为了进入 GridBagLayout 我写了一个简单的例子:

       JFrame frame = new JFrame();
   JPanel main =new JPanel(); 
   frame.setContentPane(main);
   GridBagLayout gbl=new GridBagLayout();
   GridBagConstraints gbc = new GridBagConstraints();
   main.setLayout(gbl);
   JButton btn1 = new JButton("1");
   JButton btn2 = new JButton("2");

   gbc.gridx=0;
   gbc.gridy=0;
   gbc.gridheight=1;
   gbc.gridwidth=1;
   gbc.fill=GridBagConstraints.WEST;
   gbl.setConstraints(btn1, gbc);
   gbc.gridx=0;
   gbc.gridy=1;
   gbl.setConstraints(btn2, gbc);

   frame.setSize(new Dimension(200,100));

  main.add(btn1);
   main.add(btn2);
   frame.setVisible(true);

在这里,我遇到的问题是 .fill 和 GBConstrains 的任何其他参数都不起作用。我曾经得到窗口中间的两个按钮。

提前谢谢

4

2 回答 2

2

您的配置GridBagConstraint不正确:

  • fill只能取: NONE, VERTICAL,HORIZONTALBOTH.
  • anchor可以使用“单元格”内的相对位置
  • fill并且anchor几乎总是需要使用weightx/weighty
  • 尽量避免使用gridxandgridy因为它们很难维护。而是使用它们的默认值 ( RELATIVE),您可以更改gridwidth/的值gridheight以将其设置为1(或更多,如果需要)REMAINDER并使布局换行到下一行/列(然后按添加到容器中的顺序定位组件.

这是您的工作代码(尽管我不确定您所针对的确切布局):

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGBL {

    protected void initUI() {
        JFrame frame = new JFrame();
        GridBagLayout gbl = new GridBagLayout();
        JPanel main = new JPanel(gbl);
        frame.setContentPane(main);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.weightx = 1.0;
        main.setLayout(gbl);
        JButton btn1 = new JButton("1");
        JButton btn2 = new JButton("2");

        gbl.setConstraints(btn1, gbc);
        gbl.setConstraints(btn2, gbc);

        main.add(btn1);
        main.add(btn2);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestGBL().initUI();
            }
        });
    }

}
于 2013-05-13T16:31:15.613 回答
1

编辑:我制作了一个示例应用程序,演示如何使用重用约束的 GridBadLayout:

    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;



    public class Test1 {

        private JFrame frame;

        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Test1 window = new Test1();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the application.
         */
        public Test1() {
            initialize();
        }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame();
            frame.setBounds(100, 100, 450, 300);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[]{0, 0, 0};
            gridBagLayout.rowHeights = new int[]{0, 0};
            gridBagLayout.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
            gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
            frame.getContentPane().setLayout(gridBagLayout);

            JButton btnTest = new JButton("Test1");
            GridBagConstraints gbc_btnTest = new GridBagConstraints();
            gbc_btnTest.insets = new Insets(0, 0, 0, 5);
            gbc_btnTest.gridx = 0;
            gbc_btnTest.gridy = 0;
            frame.getContentPane().add(btnTest, gbc_btnTest);

            JButton btnTest_1 = new JButton("Test2");
            gbc_btnTest.gridx = 1;
            gbc_btnTest.gridy = 0;
            frame.getContentPane().add(btnTest_1, gbc_btnTest);
        }

    }
于 2013-05-13T16:11:13.043 回答