本文介绍了JFrame组件的大小和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am learning GridBagLayout and did very simple layout that is attached,There are two very small confusions
1- Size of Buttons i have used New.setPreferredSize(new Dimension(70,23)); is it standard way to make all the buttons same size

2-放置组件的外观不好

2-Placing of components does not look good

现在看到还有更多padding on all sides of components,因此如何正确地放置from top left corner,我应该减小JFrame?的大小还是同时使用frame.pack();都可以,但是不知道什么是标准做法(我有尝试过pagestart等),

now see there is much more padding on all sides of components , so how to put it in right way from top left corner , should i decrease size of JFrame? or use frame.pack(); both work but dont know what is standard practice (i have tried pagestart etc) ,

这是我的代码的外观

    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中可用的最灵活的布局管理器之一.

GridBagLayout will drive you nuts, it's also one of the most flexible layout managers available in the JDK.

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

Don't be afraid to use compound layouts. In the example below, I've moved the navigation controls to there own panel, which makes it much easier to define complex layouts (you can also mix layout managers this way)

GridBagConstraints#fill使您可以确定如何在该单元格中填充组件.您有GridBagConstraints.HORIZONTALGridBagConstraints.VERTICALGridBagConstraints.BOTH ...我认为这是自我解释的意思.

GridBagConstraints#fill allows you to determine how components may be filled within there cell. You have GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL and GridBagConstraints.BOTH ... I think there meaning is self explanatory.

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

In the example below, I've used GridBagConstraints.HORIZONTAL to allow the buttons to fill all the available space within their cell/column

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

I highly recommend that you have a read through How to use GridBagLayout for a better explanation :P

这篇关于JFrame组件的大小和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 06:54