GridBagLayout只创建一行

时间:2016-04-07 16:46:44

标签: java swing layout layout-manager gridbaglayout

我有一个关于GriBagLayout的小问题。我正在尝试使用GridBagLayout创建一个面板,该面板应包含一个几行的按钮和标签,以更改Arduino Pins的状态。

import javax.swing.*;
import java.awt.*;
public class ControlPanel extends JPanel {

    public ControlPanel() {
        for (int index = 0; index < 14; index++) {
            GridBagConstraints c_label = new GridBagConstraints();
            c_label.gridx = 0;
            c_label.gridy = index;
            JLabel label = new JLabel("Pin: " + index);
            add(label, c_label);

            JButton inputOutput = new JButton("INPUT");
            //Add Actionlistener here

            GridBagConstraints c_inputOutput = new GridBagConstraints();
            c_inputOutput.gridx = 1;
            c_inputOutput.gridy = index;
            add(inputOutput, c_inputOutput);
        }    
        setLayout(new GridBagLayout());
    }
}

我目前面临的问题是所有标签和按钮背靠背对齐,这看起来非常混乱。

0 个答案:

没有答案