1

我正在学习 GridBagLayout并做了非常简单的布局,有两个非常小的困惑
1-Size of Buttons我使用 New.setPreferredSize(new Dimension(70,23)); 的是使所有按钮大小相同的标准方法吗

2-元件的摆放不好看

在此处输入图像描述

现在看到还有更多padding on all sides of components,那么如何以正确的方式放置它from top left corner ,我应该减小大小JFrame? 还是同时使用 frame.pack();这两种工作但不知道什么是标准做法(我已经尝试过pagestart等),

这就是我的代码的样子

    frame= new JFrame("Hello ");
    frame.setSize(300, 150);
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(this);  
    frame.setResizable(false);
    frame.setLocation(0, 0);
    p1= new JPanel (new GridBagLayout());

    gbc = new GridBagConstraints();

    lblname= new JLabel("Name");
    gbc.gridx=0; 
    gbc.gridy=0;  

    p1.add(lblname, gbc);

    textname = new JTextField(11);
    gbc.gridx=1; 
    gbc.gridy=0 ; 

    p1.add(textname, gbc);

    New=  new JButton("New"); 
    gbc.gridx=2; 
    gbc.gridy=0;
    New.setPreferredSize(new Dimension(70,23));
    p1.add(New, gbc);

    lblEmail = new JLabel("Email "); 
    gbc.gridx=0; 
    gbc.gridy=1;
    p1.add(lblEmail , gbc); 

    TextEmail = new JTextField(11);
    gbc.gridx=1; 
    gbc.gridy=1; 
    p1.add(TextEmail,gbc);

    Edit = new JButton("Edit") ;
    gbc.gridx= 2 ;
    gbc.gridy=1; 
    Edit.setPreferredSize(new Dimension(70,23));

    p1.add(Edit , gbc); 

    lblgender= new JLabel("Gender"); 
    gbc.gridx=0; 
    gbc.gridy=2; 
    p1.add(lblgender, gbc); 

    TextGender= new JTextField(11);
    gbc.gridx=1; 
    gbc.gridy=2; 
    p1.add(TextGender, gbc); 

    Gender= new JButton("Gender");
    gbc.gridx=2;
    gbc.gridy=2;
    Gender.setPreferredSize(new Dimension(70,23));

    p1.add(Gender, gbc);

    pre= new JButton("<<");
    gbc.gridx=0; 
    gbc.gridy=3; 
    p1.add(pre, gbc);

    count =  new JTextField(5);
    gbc.gridx=1; 
    gbc.gridy=3; 
    p1.add(count, gbc);

    next= new JButton(">>");
    gbc.gridx=2; 
    gbc.gridy=3; 
    next.setPreferredSize(new Dimension(70,23));

    p1.add(next, gbc);


    p1.setVisible(true);
    frame.add(p1);
    frame.setVisible(true);
4

1 回答 1

4

GridBagLayout会让你发疯,它也是 JDK 中最灵活的布局管理器之一。

不要害怕使用复合布局。在下面的示例中,我将导航控件移到了自己的面板中,这使得定义复杂布局变得更加容易(您也可以通过这种方式混合布局管理器)

GridBagConstraints#fill允许您确定如何在单元格中填充组件。你有GridBagConstraints.HORIZONTAL,GridBagConstraints.VERTICALGridBagConstraints.BOTH... 我认为意义是不言自明的。

在下面的示例中,我曾经GridBagConstraints.HORIZONTAL允许按钮填充其单元格/列中的所有可用空间

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TestLayout20 {

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

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

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

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            JLabel lblname = new JLabel("Name");
            gbc.gridx = 0;
            gbc.gridy = 0;

            add(lblname, gbc);

            JTextField textname = new JTextField(11);
            gbc.gridx = 1;
            gbc.gridy = 0;

            add(textname, gbc);

            JLabel lblEmail = new JLabel("Email ");
            gbc.gridx = 0;
            gbc.gridy = 1;
            add(lblEmail, gbc);

            JTextField TextEmail = new JTextField(11);
            gbc.gridx = 1;
            gbc.gridy = 1;
            add(TextEmail, gbc);

            JLabel lblgender = new JLabel("Gender");
            gbc.gridx = 0;
            gbc.gridy = 2;
            add(lblgender, gbc);

            JTextField TextGender = new JTextField(11);
            gbc.gridx = 1;
            gbc.gridy = 2;
            add(TextGender, gbc);

            JButton New = new JButton("New");
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.insets = new Insets(0, 12, 0, 0);
            gbc.gridx = 2;
            gbc.gridy = 0;
            add(New, gbc);

            JButton edit = new JButton("Edit");
            gbc.gridx = 2;
            gbc.gridy = 1;

            add(edit, gbc);

            JButton Gender = new JButton("Gender");
            gbc.gridx = 2;
            gbc.gridy = 2;

            add(Gender, gbc);

            JPanel pnlNav = new JPanel(new GridBagLayout());
            gbc.insets = new Insets(12, 0, 0, 0);
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(pnlNav, gbc);

            JTextField count = new JTextField(5);
            gbc = new GridBagConstraints();
            gbc.gridx = 1;
            gbc.gridy = 0;
            pnlNav.add(count, gbc);

            JButton pre = new JButton("<<");
            gbc.anchor = GridBagConstraints.WEST;
            gbc.weightx = 1;
            gbc.gridx = 0;
            gbc.gridy = 0;
            pnlNav.add(pre, gbc);

            JButton next = new JButton(">>");
            gbc.anchor = GridBagConstraints.EAST;
            gbc.gridx = 2;
            gbc.gridy = 0;
            pnlNav.add(next, gbc);

        }
    }
}

我强烈建议您阅读如何使用 GridBagLayout以获得更好的解释:P

于 2013-03-10T08:29:46.517 回答