質問

    //creating two lists of gui components
    ArrayList<JLabel> labelList = new ArrayList<JLabel>();
    ArrayList<JTextField> textFieldList = new ArrayList<JTextField>();

    //number of labels and textfields i want to add
    int numOfAt = Data.getInstance().getInstances().numAttributes();

    //for loop, for adding these components
    for (int i = 0; i < numOfAt - 1; i++) {
        String name = Data.getInstance().getInstances().attribute(i).name();
        labelList.add(new JLabel(name));
        labelList.get(i).setText(name);

        textFieldList.add(new JTextField());

        add(labelList.get(i));

        add(textFieldList.get(i));
        textFieldList.get(i).setSize(55, 25);
    }

These code works. Labels and fields is added on my jPanel. But i can't find solution how to place them one below other. jLabel and jTextField is placed next on each other on the first iteration of for loop. I want on second iteration, next two components to place below first two components. I'm using flow layout.

役に立ちましたか?

解決

I'm using flow layout.

Then you need to use a different layout. One that allows you to display components horizontally and vertically.

The easiest would be a GridLayout, but then all components will be displayed at the same size. Another option is a GridBagLayout which give you more flexibility but is more complex to use.

Read the section from the Swing tutorial on Layout Managers and experiment with the different layouts to see what you want to use.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top