如何使用GridBagLayout和GridLayout实现最佳对齐?

时间:2017-07-12 11:06:04

标签: java swing layout-manager grid-layout gridbaglayout

我想在我的程序中尝试使用GridBagLayout,但遗憾的是我不清楚它,我也试过实现GridLayout,但两者都给了我不同的问题。让我向您展示代码和输出图片,以进一步澄清它:

package iKleen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class ikleenRegister {

    JFrame frame;
    JPanel phonePanel, fieldPanel, mainPanel;
    JLabel name, email, password, address, mobile, l_register;
    JTextField nameField, emailField, passwordField, addressField, mobileField, countryCode;
    JButton b_register;
    GridBagConstraints c;

    public void launchGUI()
    {
        frame = new JFrame("iKleen - Register / Create Free Account");

        //phonePanel and its components
        phonePanel = new JPanel();
        phonePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        mobileField = new JTextField(8);
        countryCode = new JTextField(2);
        countryCode.setText("+91");
        countryCode.setEnabled(false);
        phonePanel.add(countryCode);
        phonePanel.add(mobileField);

        //fieldPanel and its components
        fieldPanel = new JPanel();
        fieldPanel.setLayout(new GridBagLayout());
        fieldPanel.setBorder(BorderFactory.createEmptyBorder(25,25,25,25));
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        name = new JLabel("Name: ");
        email = new JLabel("Email ID: ");
        password = new JLabel("Password: ");
        address = new JLabel("Address: ");
        mobile = new JLabel("Mobile Number: ");
        nameField = new JTextField(15);
        emailField = new JTextField(15);
        passwordField = new JTextField(15);
        addressField = new JTextField(20);
        c.gridx=0;
        c.gridy=0;
        fieldPanel.add(name, c);
        c.gridx=1;
        c.gridy=0;
        fieldPanel.add(nameField, c); 
        c.gridx=0;
        c.gridy=1;
        fieldPanel.add(email, c);
        c.gridx=1;
        c.gridy=1;
        fieldPanel.add(emailField, c);
        c.gridx=0;
        c.gridy=2;
        fieldPanel.add(password, c);
        c.gridx=1;
        c.gridy=2;
        fieldPanel.add(passwordField, c);
        c.gridx=0;
        c.gridy=3;
        fieldPanel.add(address, c);
        c.gridx=1;
        c.gridy=3;
        fieldPanel.add(addressField, c);
        c.gridx=0;
        c.gridy=4;
        fieldPanel.add(mobile, c);
        c.gridx=1;
        c.gridy=4;
        fieldPanel.add(phonePanel, c);

        //mainPanel and its components
        mainPanel = new JPanel();
        mainPanel.setBorder(BorderFactory.createEmptyBorder(25,25,25,25));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        Font font = new Font("MS Sans Serif", Font.BOLD, 18);
        l_register = new JLabel("Create a free account");
        l_register.setFont(font);
        l_register.setAlignmentX(Component.CENTER_ALIGNMENT);
        b_register = new JButton("Create Account");
        b_register.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(l_register);
        mainPanel.add(fieldPanel);
        mainPanel.add(b_register);

        //final frame settings
        frame.setContentPane(mainPanel);
        frame.pack();
        centerFrame();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void centerFrame() {
        Dimension currentScreen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((currentScreen.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((currentScreen.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }
}

输出: gridbaglayout

package iKleen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class ikleenRegister {

    JFrame frame;
    JPanel phonePanel, fieldPanel, mainPanel;
    JLabel name, email, password, address, mobile, l_register;
    JTextField nameField, emailField, passwordField, addressField, mobileField, countryCode;
    JButton b_register;

    public void launchGUI()
    {
        frame = new JFrame("iKleen - Register / Create Free Account");

        //phonePanel and its components
        phonePanel = new JPanel();
        phonePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
        mobileField = new JTextField(15);
        countryCode = new JTextField(2);
        countryCode.setText("+91");
        countryCode.setEnabled(false);
        phonePanel.add(countryCode);
        phonePanel.add(mobileField);

        //fieldPanel and its components
        fieldPanel = new JPanel();
        fieldPanel.setLayout(new GridLayout(5,2,3,3));
        fieldPanel.setBorder(BorderFactory.createEmptyBorder(25,25,25,25));
        name = new JLabel("Name: ");
        email = new JLabel("Email ID: ");
        password = new JLabel("Password: ");
        address = new JLabel("Address: ");
        mobile = new JLabel("Mobile Number: ");
        nameField = new JTextField(15);
        emailField = new JTextField(15);
        passwordField = new JTextField(15);
        addressField = new JTextField(15);
        fieldPanel.add(name);
        fieldPanel.add(nameField);
        fieldPanel.add(email);
        fieldPanel.add(emailField);
        fieldPanel.add(password);
        fieldPanel.add(passwordField);
        fieldPanel.add(address);
        fieldPanel.add(addressField);
        fieldPanel.add(mobile);
        fieldPanel.add(phonePanel);

        //mainPanel and its components
        mainPanel = new JPanel();
        mainPanel.setBorder(BorderFactory.createEmptyBorder(25,25,25,25));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        Font font = new Font("MS Sans Serif", Font.BOLD, 18);
        l_register = new JLabel("Create a free account");
        l_register.setFont(font);
        l_register.setAlignmentX(Component.CENTER_ALIGNMENT);
        b_register = new JButton("Create Account");
        b_register.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(l_register);
        mainPanel.add(fieldPanel);
        mainPanel.add(b_register);

        //final frame settings
        frame.setContentPane(mainPanel);
        frame.pack();
        centerFrame();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void centerFrame() {
        Dimension currentScreen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((currentScreen.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((currentScreen.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }
}

输出: gridlayout

在gridbaglayout中,数字字段与其他字段不对齐,我希望它们在起始点对齐,就像jlabels一样。 在网格布局中,他们在jfields和jlabels之间存在太大的差距,我尝试了很多解决方案,遗憾的是无济于事。

3 个答案:

答案 0 :(得分:3)

对于GridLayout:

"差距"你在jlabels和jtextfield之间看到的原因是,在GridLayout中,每个单元格的宽度和高度完全相同。 因此,由于你的jtextfields比标签更宽,它们会变宽以适应它们的细胞。

所以,如果你仍然希望使用没有这个差距的GridLayout,你可以:

  • 尝试限制您的jtextfields,给定构造函数的列数不同(当前最大值为20)。
  • 将您的jlabels与右侧对齐。这将导致空白间隙位于标签的左侧。

当然,这些不是解决方案,它们是一种妥协,因为您的小组会改变方面。

我建议您使用GridBagLayout,更改您的" phonePanel"布局。 如果您不关心phonePanel与其他文本字段的宽度相同,则可以使用左对齐的flowlayout:

phonePanel.setLayout (new FlowLayout(FlowLayout.LEFT, 0, 0));

您将得到以下结果:

enter image description here

如果您希望具有相同的宽度,可以将BorderLayout设置为phonePanel,在BorderLayout.WEST上添加国家/地区代码(以使其具有固定大小)并在中心添加mobileField(以便让它占用所有额外的空间)。

phonePanel.setLayout(new BorderLayout());
phonePanel.add(countryCode, BorderLayout.WEST);
phonePanel.add(mobileField, BorderLayout.CENTER); 

结果如下:

enter image description here

最后,您可以使用insets在标签和文本字段之间插入一点间隙。

答案 1 :(得分:2)

您的两次尝试(外部面板为GridBagLayoutGridLayout)都非常接近解决方案。 问题是FlowLayout的{​​{1}}。 在这两种变体中,您可以通过将其替换为phonePanel(零间隙)来解决问题:

BorderLayout

对于您的 //phonePanel and its components phonePanel = new JPanel(); phonePanel.setLayout(new BorderLayout(0, 0)); mobileField = new JTextField(15); countryCode = new JTextField(2); countryCode.setText("+91"); countryCode.setEnabled(false); phonePanel.add(countryCode, BorderLayout.WEST); phonePanel.add(mobileField, BorderLayout.CENTER); 变体,请执行以上修复:

enter image description here

对于您的GridLayout变体,请执行上述修复 为了更好看,您可以通过以下方式在组件周围引入一些填充:

GridBagLayout

enter image description here

答案 2 :(得分:1)

对于GridBagLayout,请更新以下代码行。您正在使用FlowLayout.CENTER将移动号码字段移动到面板中间。

phonePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));