【问题标题】:GridBagLayout format error. JTextFields in wrong positionsGridBagLayout 格式错误。 JTextFields 在错误的位置
【发布时间】:2015-07-18 21:48:16
【问题描述】:

我根本无法让我的 JTextField 正确对齐。现在程序看起来像这样:

现在作业编号已正确对齐。但是 Mark JTextfield 从 7 开始,Weight JTextField 从最后一个 Mark 开始。

现在我想要的是一切都正确对齐。在作业 1 中的含义是在同一行下有标记和权重 JTextField。这应该一直到 7(或者我选择的行数)。

代码:

    public class test{

    private static final Insets normalInsets = new Insets(10,10,0,10);
    private static final Insets finalInsets = new Insets(10,10,10,10);





    private static JPanel createMainPanel(){
        GridBagConstraints gbc = new GridBagConstraints();

        //Adding the JPanels. Panel for instructions
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        int gridy = 0;

        //JLabel for the Instructions
        JTextArea instructionTextArea = new JTextArea(5, 30);
        instructionTextArea.setEditable(false);
        instructionTextArea.setLineWrap(true);
        instructionTextArea.setWrapStyleWord(true);

        JScrollPane instructionScrollPane = new JScrollPane(instructionTextArea);
        addComponent(panel, instructionScrollPane, 0, gridy++, 3, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);

        //JLabels for Assignment
        JLabel label1 = new JLabel("Assignment");
        label1.setHorizontalAlignment(JLabel.CENTER);
        addComponent(panel, label1, 0, gridy, 1, 1, finalInsets,GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);

        //JLabel for Mark
        JLabel label2 = new JLabel("Mark");
        label2.setHorizontalAlignment(JLabel.CENTER);
        addComponent(panel, label2, 1, gridy, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);

        //JLabel for Weight.
        JLabel label3 = new JLabel("Weight");
        label3.setHorizontalAlignment(JLabel.CENTER);
        addComponent(panel, label3, 2, gridy++, 1, 1, finalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);



        //JLabel Number for the number list of assignments at the side.
        for(int i = 1; i<=7; i++){
            String kok = String.valueOf(i);
        JLabel number = new JLabel(kok);
        number.setHorizontalAlignment(JLabel.CENTER);
        addComponent(panel, number, 0, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
        }

        //JTextfield for Mark
        for(int i=0; i<7; i++){
        JTextField mark = new JTextField(20);
        mark.setHorizontalAlignment(JLabel.CENTER);
        addComponent(panel, mark, 1, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
        }

        //JTextfield for Weight
        for(int i=0; i<7; i++){
        JTextField weight = new JTextField(20);
        weight.setHorizontalAlignment(JLabel.CENTER);
        addComponent(panel, weight, 2, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
        }

        return panel;

    }

    private static void addComponent(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, Insets insets, int anchor, int fill ){
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0D,1.0D,anchor,fill,insets,0,0);
        container.add(component,gbc);
    }

    public static void main(String[] args) {    
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createMainPanel());
        frame.pack();
        frame.setVisible(true);
        new test();
    }
}

我是一个糟糕的新 Java 程序员,所以请放轻松 :)。

更新~~~~~

 After running the `for` loops which add the `JLabels` and `JTextFields`, you will need to reset `gbc.gridy = 1`. This way the loop will start adding components from the top row.

//JLabel Number for the number list of assignments at the side.
for(int i = 1; i<=7; i++){
    String kok = String.valueOf(i);
    JLabel number = new JLabel(kok);
    number.setHorizontalAlignment(JLabel.CENTER);
    addComponent(panel, number, 0, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
}
gbc.gridy = 1;

//JTextfield for Mark
for(int i=0; i<7; i++){
    JTextField mark = new JTextField(20);
    mark.setHorizontalAlignment(JLabel.CENTER);
    gridy = 1; //The code only partly works when I include this
    addComponent(panel, mark, 1, gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
}
gbc.gridy = 1;

这就是它的样子:

只有权重 JTextField 正确对齐。当我不添加“gridy=1”时,它的格式错误与我的原始屏幕截图相同。

【问题讨论】:

  • 您需要在每个循环之间执行gridy = 1; 以确保每列都从网格顶部开始。
  • @aioobe 我认为做 'gridy++' 会使 y 增加一,所以它应该已经进一步下降了?
  • 但你并没有将它重置回起始位置
  • @MadProgrammer 我是个白痴。谢谢。

标签: java swing jtextfield layout-manager gridbaglayout


【解决方案1】:

运行添加JLabelsJTextFieldsfor 循环后,您需要重置gbc.gridy = 2。这样循环将从顶行开始添加组件。此外,在每次使用addComponent() 时,将gridy++ 更改为gbc.gridy++

gbc.gridy = 2;

//JLabel Number for the number list of assignments at the side.
for(int i = 1; i<=7; i++){
    String kok = String.valueOf(i);
    JLabel number = new JLabel(kok);
    number.setHorizontalAlignment(JLabel.CENTER);
    addComponent(panel, number, 0, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);
}
gbc.gridy = 2;

//JTextfield for Mark
for(int i=0; i<7; i++){
    JTextField mark = new JTextField(20);
    addComponent(panel, mark, 1, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
}
gbc.gridy = 2;

//JTextfield for Weight
for(int i=0; i<7; i++){
    JTextField weight = new JTextField(20);
    weight.setHorizontalAlignment(JLabel.CENTER);
    addComponent(panel, weight, 2, gbc.gridy++, 1, 1, normalInsets, GridBagConstraints.CENTER,GridBagConstraints.NONE);
}

结果如下所示:

【讨论】:

  • 感谢您的回答,但是发生了一些非常奇怪的事情。现在 GUI 可以在我的测试代码上正确显示,但是当我将代码放入我的真实代码中时,它仍然没有得到修复。我已经更新了我的帖子。
  • 好的,我缩小了问题的范围。如果我输入 gridy = 1; ,您的答案似乎只能部分起作用在 JTextField 标记代码的中间。我将通过屏幕截图/代码更改来更新我的帖子
  • 我在createMainPanel方法下把gridy变量改成了gridytwo,还是一样的错误。如果我在 Mark JTextField 代码下添加 gridytwo,它只能部分工作。
  • 对不起,如果我不清楚。我指的是gbc,而不是gridy
  • 再次,我完全复制了您的代码,并且当我在标记代码中输入 gridy = 1 时,Weight JTextFields 似乎才对齐:/
猜你喜欢
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2020-07-18
  • 2012-07-26
相关资源
最近更新 更多