By default, JPanel
uses a flow layout. To get the vertical arrangement you are looking for, you should be able to do this by using a BoxLayout
with a vertical orientation on your labelPanel
, then add your JLabel
rows.
labelPanel= new JPanel();
//set this up to order things vertically
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.Y_AXIS));
lRows = new JLabel[n];
for (int i=0;i<n;i++){
lRows[i]=new JLabel(Arrays.toString(tri.tri[i]));
//to center your label, just set the X alignment
lRows[i].setAlignmentX(Component.CENTER_ALIGNMENT)
labelPanel.add(lRows[i]);
}
I also threw in a line to center the rows like your picture. Component
comes from the java.awt
package.
You can read up on the different layout managers available by default in the Java Tutorial