【发布时间】:2014-03-03 21:24:45
【问题描述】:
我正在使用 Java 和 GridBagLayout 重新创建图像中看到的表单。为了在星期几下创建网格线,我将带有边框的空 JLabels 插入到空单元格中。这对我来说是完美的,然后我决定让表单上阴影的每个空单元格在 java 中也有阴影,这就是我苦苦挣扎的地方。
我的想法是我可以像使用 x 和 y 坐标一样创建一个“阴影指针”,这个 shadePtr 将以 8 的值开始,因为当循环对一行进行着色时,这是要着色的第一行,然后将 shadePtr 增加 3,因为第 11 行是下一个要着色的行,然后是 14,依此类推。
到目前为止,我获得的唯一一点成功是,如果我注释掉你看到的最后一行代码 (shadePtr = shadePtr + 3),但只有一行被遮蔽。我似乎无法弄清楚我在做什么我在这里做错了,感谢您的时间和精力。
int yPointer = 7;
int xPointer = 3;
int shadePtr = 8;
for (int j = 0; j <= 299; j++)
{
gbc.gridx = xPointer;
gbc.gridy = yPointer;
gbc.gridheight = 1;
gbc.gridwidth = 1;
if (yPointer == 36) calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.BLACK)); //if bottom row
else calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK));
if (yPointer == shadePtr){ //if row number = shadePtr then color the cell
calendarGridLines[j].setOpaque(true);
calendarGridLines[j].setBackground(Color.GRAY);
}
gridbag.setConstraints(calendarGridLines[j], gbc);
rp.add(calendarGridLines[j]);
xPointer++; //go to next cell in row
j++; //use the next jlabel
gbc.gridx = xPointer;
gbc.gridy = yPointer;
gbc.gridheight = 1;
gbc.gridwidth = 1;
if (yPointer == 36) calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK)); //if bottom row
else calendarGridLines[j].setBorder(BorderFactory.createMatteBorder(1, 1, 0, 1, Color.BLACK));
if (yPointer == shadePtr){ //if row number = shadePtr then color the cell
calendarGridLines[j].setOpaque(true);
calendarGridLines[j].setBackground(Color.GRAY);
}
gridbag.setConstraints(calendarGridLines[j], gbc);
rp.add(calendarGridLines[j]);
xPointer++; //go to next cell in row
if(xPointer == 13) //if end of column then go to next row and reset column pointer to 3 and increment shade pointer by 3
{
yPointer++; //go down a row
xPointer = 3;
shadePtr = shadePtr + 3; //when this line is commented out, one row will be colored; when active none are colored
}
}
【问题讨论】:
标签: java swing for-loop logic gridbaglayout