代码之家  ›  专栏  ›  技术社区  ›  bob9123

GridBagLayout格式错误。JTextFields位置错误

  •  0
  • bob9123  · 技术社区  · 11 年前

    我根本无法让JTextFields正确对齐。现在,程序如下所示: .

    现在,分配编号已正确对齐。然而,Mark JTextfield从7开始,Weight JTextfield从最后一个Mark开始。

    现在我想要的是一切都正确对齐。这意味着在任务1中,同一行下有标记和权重JTextFields。这应该一直到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”时,它的格式错误与原始屏幕截图相同。

    1 回复  |  直到 11 年前
        1
  •  1
  •   deezy    11 年前

    运行 for 添加 JLabels JTextFields ,您需要重置 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);
    }
    

    结果如下: This is the result: