如何使用GridBagLayout在Java Swing中“模拟” 12列网格

时间:2019-04-24 17:58:39

标签: java swing layout-manager gridbaglayout

我试图将Java Swing组件放置在12列的网格中,类似于Web开发中CSS所做的事情。

我尝试了以下方法:

package com.example.test;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class AddressWindow extends JFrame {

    public AddressWindow() {

        JLabel addressTypeLabel = new JLabel("Type");
        JLabel addressLineLabel = new JLabel("Address");
        JLabel zipLabel = new JLabel("ZIP");
        JLabel townLabel = new JLabel("Town");
        JLabel countyLabel = new JLabel("County");
        JLabel stateLabel = new JLabel("State");
        JLabel countryLabel = new JLabel("Country");

        JComboBox addressType = new JComboBox();
        JTextField addressLine1 = new JTextField();
        JTextField addressLine2 = new JTextField();
        JTextField zip = new JTextField();
        JTextField town = new JTextField();
        JTextField county = new JTextField();
        JTextField state = new JTextField();
        JTextField country = new JTextField();

        GridBagLayout windowLayout = new GridBagLayout();
        setLayout(windowLayout);
        GridBagConstraints c = new GridBagConstraints();  
        c.fill = GridBagConstraints.HORIZONTAL; 
        c.weightx = 1.0;

        // First Row 
        int posy = 0; c.gridy = posy;

        c.gridx = 1;
        c.gridwidth = 2;
        add(addressTypeLabel, c);

        c.gridx = 3;
        c.gridwidth = 4;
        add(addressType, c);

        c.gridx = 7;
        c.gridwidth = 2;
        add(countryLabel, c);

        c.gridx = 9;
        c.gridwidth = 4;
        add(country, c);

        // Next Row
        posy++; c.gridy = posy;

        c.gridx = 1;
        c.gridwidth = 2;
        add(addressLineLabel, c);

        c.gridx = 3;
        c.gridwidth = 10;
        add(addressLine1, c);

        posy++; c.gridy = posy;

        c.gridx = 3;
        c.gridwidth = 10;
        add(addressLine2, c);

        // Next Row
        posy++; c.gridy = posy;

        c.gridx = 1;
        c.gridwidth = 2;
        add(zipLabel, c);

        c.gridx = 3;
        c.gridwidth = 4;
        add(zip, c);

        c.gridx = 7;
        c.gridwidth = 2;
        add(townLabel, c);

        c.gridx = 9;
        c.gridwidth = 4;
        add(town, c);

        // Next Row
        posy++; c.gridy = posy;

        c.gridx = 1;
        c.gridwidth = 2;
        add(countyLabel, c);

        c.gridx = 3;
        c.gridwidth = 4;
        add(county, c);

        c.gridx = 7;
        c.gridwidth = 2;
        add(stateLabel, c);

        c.gridx = 9;
        c.gridwidth = 4;
        add(state, c);

        setSize(400,200);
        setVisible(true);

    }

    public static void main(String[] args) {  

        AddressWindow f = new AddressWindow();

    }

}

尽管这会创建网格,但不考虑组件(列)的宽度,因为它为每个组件提供了相同的宽度。我显然不了解gridwidth的工作原理。

预期的效果是,在假想的12列中,网格标签将占据2列,而输入类型元素4将构成2 + 4 + 2 + 4 = 12列网格(addressLine1和addressLine2除外,其中一个标签,并且输入字段占10列)。 即,我正在尝试使输入元素的宽度比其相应标签的两倍。

这是结果:

Result

1 个答案:

答案 0 :(得分:0)

  

预期效果是假想的12列中的

您不能只组成列。

列的大小由列中显示的组件的“首选大小”确定。

任何行中最多有4个组件,因此最多有4列。

因此,当您创建JTextField时,您应该执行以下操作:

JTextField textField = new JTextField(10);

这将允许文本字段基于显示10个“ W”字符来确定其首选大小。

所有标签(或1.0以外的其他值)都应为weightx = 0。这意味着标签在调整框架大小时将不会获​​得多余的空间,从而使文本字段变大。

对于地址行,您应该使用gridwidth = 3,以便它跨越最后三列。