我正在学习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();都可以,但是不知道什么是标准做法(我尝试过等),

这就是我的代码的样子

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

最佳答案

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

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

GridBagConstraints#fill允许您确定如何在该单元格中填充组件。您有GridBagConstraints.HORIZONTALGridBagConstraints.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);

        }
    }
}


我强烈建议您通读How to use GridBagLayout以获取更好的解释:P

关于java - JFrame组件的大小和位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15320180/

10-10 07:37