gpt4 book ai didi

java - GridBagLayout 中组件的大小

转载 作者:行者123 更新时间:2023-12-02 04:34:14 24 4
gpt4 key购买 nike

我在对齐 GridBagLayout 中的组件时遇到了重大问题,是的,我已经阅读了 Oracle 的教程并尝试更改weightx。

这是目前的样子: enter image description here

基本上我需要实现的是:

JTextFields“Nome”和“Filiação”一直向左延伸,就像“Idade”和“Turma”

底部 JButton 大小相同,中间对齐。

我希望有人能指出我在这里缺少的东西。

这是一个 SSCCE:

 package test1;

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;

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

JFrame jf = new JFrame("Test");
JPanel jp = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);

JLabel labelNome = new JLabel("Nome:");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelNome, gbc);

JTextField tfNome = new JTextField();
gbc.gridx = 1;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfNome, gbc);

JLabel labelIdade = new JLabel("Idade :");
gbc.ipadx = 0;
gbc.gridx = 2;
gbc.fill = GridBagConstraints.NONE;
jp.add(labelIdade, gbc);

JTextField tfIdade = new JTextField();
gbc.gridx = 3;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfIdade, gbc);

JLabel labelEndereco = new JLabel("Endereço :");
gbc.ipadx = 0;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelEndereco, gbc);

JTextField tfEndereco = new JTextField();
gbc.ipadx = 50;
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfEndereco, gbc);

JLabel labelFiliacao = new JLabel("Filiação :");
gbc.ipadx = 0;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelFiliacao, gbc);

JTextField tfFiliacao = new JTextField();
gbc.gridx = 1;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfFiliacao, gbc);

JLabel labelTurma = new JLabel("Turma :");
gbc.ipadx = 0;
gbc.gridx = 2;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelTurma, gbc);

JTextField tfTurma = new JTextField();
gbc.gridx = 3;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfTurma, gbc);

JLabel labelDisciplina = new JLabel("Disciplina :");
gbc.ipadx = 0;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelDisciplina, gbc);

JTextField tfDisciplina = new JTextField();
gbc.ipadx = 50;
gbc.ipady = 0;
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfDisciplina, gbc);

JButton adicionaDisciplina = new JButton("Adicionar disciplina");
gbc.ipadx = 0;
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 4;
jp.add(adicionaDisciplina, gbc);

JButton limparDisciplina = new JButton("Limpar lista de disciplinas");
gbc.gridx = 2;
jp.add(limparDisciplina, gbc);

JButton botaoSalvar = new JButton("Salvar");
gbc.gridx = 0;
gbc.gridy = 5;
jp.add(botaoSalvar, gbc);

JButton botaoCancelar = new JButton("Cancelar");
gbc.gridx = 2;
jp.add(botaoCancelar, gbc);

jf.setSize(500, 550);
jf.add(jp);

jf.setVisible(true);

}

}

最佳答案

JTextFields "Nome" and "Filiação" to stretch all the way to the left, just like "Idade" and "Turma"

不知道什么意思。它们像第二列中的其他文本字段一样向左对齐。

它们可能不会像其他文本字段一样显示在最左边,因为“Endereco”标签长度较长,因此第二列只能从结束的地方开始。

如果您希望文本字段具有相同的大小,那么您需要指定文本字段的大小,并且 GridBagLayout 将遵循该大小。您可以通过在创建文本字段时指定文本字段中的列来完成此操作:

JTextField tfNome = new JTextField(10);

关于java - GridBagLayout 中组件的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31034435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com