JScrollPane格式显示Pascals Triangle [英] JScrollPane formatting to show Pascals Triangle
问题描述
所以我想让我的Jscrollpane显示帕斯卡三角形.我有这个:
so I want to make my Jscrollpane to show Pascals triangle. I have this:
labelPanel= new JPanel();
lRows = new JLabel[n];
for (int i=0;i<n;i++){
lRows[i]=new JLabel(Arrays.toString(tri.tri[i]));
labelPanel.add(lRows[i]);
}
但这不是我想要的,我不确定如何解决它,包括图片.有帮助吗?
But it's not what I want and I am not sure how to fix that, picture included. Any help?
推荐答案
默认情况下,JPanel
使用流布局.要获得所需的垂直排列,您应该能够通过在labelPanel
上使用垂直方向的BoxLayout
进行操作,然后添加JLabel
行.
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]);
}
我还排成一行以使行像您的图片一样居中. Component
来自java.awt
软件包.
I also threw in a line to center the rows like your picture. Component
comes from the java.awt
package.
您可以在 Java教程
这篇关于JScrollPane格式显示Pascals Triangle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!