我在GridBagLayout中调整组件时遇到了重大问题,是的,我已经阅读了Oracle的教程,并尝试更改weightx。
目前的情况如下:
基本上我需要实现的是:
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);
}
}
答案 0 :(得分:2)
JTextFields“Nome”和“Filiação”一直延伸到左边,就像“Idade”和“Turma”
不知道这意味着什么。它们与左侧对齐,就像第二列中的其他文本字段一样。
它们可能不会显示为远离其他文本字段,因为“Endereco”标签长度较长,因此第二列只能在结束时开始。
如果您希望文本字段具有相同的大小,则需要指定文本字段的大小,GridBagLayout将遵循大小。您可以通过在创建文本字段时在文本字段中指定列来执行此操作:
JTextField tfNome = new JTextField(10);