java - 如何使用 GridBagLayout 使 JLabel 1 列宽,然后使 JTextField 2 列宽(总宽度 3 列)

标签 java swing layout-manager

我已经阅读了 GridBagLayout 的文档,但我无法理解它。我基本上想完成这样的事情:

enter image description here

我制作了一些示例代码来帮助我解决这个问题。我如何修改此代码来实现此目的?

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


public class Main {
    public static void main(String[] args) {

        JLabel label = new JLabel("label");
        JTextField field = new JTextField();

        JLabel label2 = new JLabel("label2");
        JTextField field2 = new JTextField();

        JPanel jp = new JPanel();
        jp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        //gbc.weightx = ??
        jp.add(label, gbc);
        gbc.gridx = 1;
        gbc.gridwidth = 2;
        //gbc.weightx = ??
        jp.add(field, gbc);

        JPanel jp2 = new JPanel();
        jp2.setLayout(new GridBagLayout());
        GridBagConstraints gbc2 = new GridBagConstraints();
        gbc2.fill = GridBagConstraints.BOTH;
        gbc2.gridx = 0;
        gbc2.gridy = 0;
        gbc2.gridwidth = 1;
        //gbc2.weightx = ??
        jp2.add(label2, gbc2);
        gbc2.gridx = 1;
        gbc2.gridwidth = 2;
        //gbc2.weightx = ??
        jp2.add(field2, gbc2);

        JPanel jpContainer = new JPanel();
        jpContainer.setLayout(new BoxLayout(jpContainer, BoxLayout.Y_AXIS));
        jpContainer.add(jp);
        jpContainer.add(jp2);

        JFrame f = new JFrame();
        f.setSize(300, 100);
        f.setContentPane(jpContainer);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}
<小时/>

编辑:将 JTextArea 更改为 JTextField

最佳答案

使用 GridBagLayout columnWidths,您可以手动调整宽度,然后将 GridBagConstraints 填充设置为 GridBagConstraints.HORIZONTAL :

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;


public class Example extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Example frame = new Example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Example() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] {100, 0};
        gbl_contentPane.rowHeights = new int[]{0, 0, 0};
        gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
        gbl_contentPane.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        contentPane.setLayout(gbl_contentPane);

        JLabel lblNewLabel = new JLabel("jlabel");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        contentPane.add(lblNewLabel, gbc_lblNewLabel);

        textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("jlabel2");
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.anchor = GridBagConstraints.WEST;
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 0, 5);
        gbc_lblNewLabel_1.gridx = 0;
        gbc_lblNewLabel_1.gridy = 1;
        contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);

        textField_1 = new JTextField();
        GridBagConstraints gbc_textField_1 = new GridBagConstraints();
        gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField_1.gridx = 1;
        gbc_textField_1.gridy = 1;
        contentPane.add(textField_1, gbc_textField_1);
        textField_1.setColumns(10);
    }

}

当然,如果你想保留 1/3 Label 和 2/3 JTextField,你可以考虑使用 MigLayout,如下所示:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MigLayoutExample extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MigLayoutExample frame = new MigLayoutExample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MigLayoutExample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new MigLayout("", "[grow 33][grow 66]", "[][]"));

        JLabel lblNewLabel = new JLabel("New label");
        contentPane.add(lblNewLabel, "cell 0 0");

        textField = new JTextField();
        contentPane.add(textField, "cell 1 0,growx");
        textField.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("New label");
        contentPane.add(lblNewLabel_1, "cell 0 1");

        textField_1 = new JTextField();
        contentPane.add(textField_1, "cell 1 1,growx");
        textField_1.setColumns(10);
    }

}

关于java - 如何使用 GridBagLayout 使 JLabel 1 列宽,然后使 JTextField 2 列宽(总宽度 3 列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23935363/

相关文章:

java - 如何在JPanel上布局这些组件?

java - LinearLayoutManager setReverseLayout() == true 但项目从底部堆叠

java - 如何查找多个标签的动态 xpath 以及 div 内的文本

在服务器上部署后,java spring web 应用程序不起作用

java - Hibernate SessionFactory 与 EntityManagerFactory

java - 如何用java获取注册表值?

java - 有关 Java gridbaglayout 的帮助吗?

java - 将项目添加到 Apple

java - 如何阻止在货币之前生成?

Java NetBeans 和 MySQL