GridBagLayout标签重叠

时间:2015-06-21 15:55:14

标签: java

我的最后两个标签是重叠的,我无法弄清楚原因。我不认为网格中的任何内容是重叠的我把它画出来了。 'right'是GridBagLayout面板的名称。如果我摆脱最后两个标签并为chickenCostLabel分配1到weightx和weighty,它就可以正常工作。

    JLabel chickenLabel = new JLabel();
    chickenLabel.setText("Chicken");
    gbc3.gridx = 0;
    gbc3.gridy = 1;
    gbc3.weighty = 0;
    gbc3.weightx = 0;
    right.add(chickenLabel, gbc3);

    chickenCountLabel = new JLabel();
    chickenCountLabel.setText("Count: ");
    gbc3.gridx = 0;
    gbc3.gridy = 2;
    gbc3.weighty = 0;
    gbc3.weightx = 0;
    right.add(chickenCountLabel, gbc3);

    chickenCostLabel = new JLabel();
    chickenCostLabel.setText("Cost: ");
    gbc3.gridx = 0;
    gbc3.gridy = 3;
    gbc3.weighty = 0;
    gbc3.weightx = 0;
    right.add(chickenCostLabel, gbc3);

    JButton chickenButton = new JButton();
    chickenButton.setPreferredSize(new Dimension(50, 50));
    gbc3.gridx = 1;
    gbc3.gridy = 1;
    gbc3.gridheight = 3;
    gbc3.weighty = 0;
    gbc3.weightx = 0;
    chickenButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            queueBuyNum.add(1);
        }
    });
    right.add(chickenButton, gbc3);

    JLabel blobLabel = new JLabel();
    blobLabel.setText("Blob");
    gbc3.gridx = 0;
    gbc3.gridy = 4;
    gbc3.weighty = 0;
    gbc3.weightx = 0;
    right.add(blobLabel, gbc3);

    blobCountLabel = new JLabel();
    blobCountLabel.setText("Count: ");
    gbc3.gridx = 0;
    gbc3.gridy = 5;
    gbc3.weighty = 1;
    gbc3.weightx = 1;
    right.add(blobCountLabel, gbc3);

0 个答案:

没有答案