对于我为Tapestry做的作业,我必须从表格中显示一串钻石。这是我到目前为止有:如何使用挂毯组件来绘制钻石t:loop
代码Index.java
public class Index
{
@Property
private Integer number;
@Property
private String [] table;
public Index() {
number = 9;
int temp = 0;
String tmp = "-";
table = new String[number * number];
if(singleCell == null)
singleCell="";
for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
table[temp] = tmp;
temp++;
}
}
}
@OnEvent(component="diamond")
Object onDiamondLink() {
String swapValue = "*";
int sum = number/2 ;
int x1 = number-1;
int sumY = number/2;
int y1 = number+1;
int temp = x1 + sumY;
for (int i = 0; i < table.length; i++) {
table[i] = "-";
}
for (int i = 0; i < table.length; i++) {
if(i == sum) {
table[i] = swapValue;
sum = sum + x1;
}
if (i == sumY) {
table[i] = swapValue;
sumY = sumY + y1;
}
}
System.out.println("link diamond is activate");
return null;
}
public boolean isStartRow(){
return (myIndex%9 ==0);
}
public boolean isEndRow(){
return (myIndex%9 == 8);
}
public String getStartTR(){
return "<tr >";
}
public String getEndTR(){
return "</tr>";
}
代码index.tml的:
<t:actionlink t:id="diamond" >Diamond table</t:actionlink>
<br/>
<h1>Result:</h1>
<table border="1" >
<t:loop t:source="table" t:value="singleCell" index="MyIndex">
<t:if test="startRow">
<t:outputraw value="startTR"/>
</t:if>
<td width="20px">
${singleCell}
</td>
<t:if test="endRow">
<t:outputraw value="endTR"/>
</t:if>
</t:loop>
</table>
此代码生成的输出:
- - - - * - - - -
- - - * - * - - -
- - * - - - * - -
- * - - - - - * -
* - - - - - - - *
- - - - - - - * -
* - - - - - * - -
- * - - - * - - -
- - * - * - - - -
正确我需要的输出是这样的:
- - - - * - - - -
- - - * - * - - -
- - * - - - * - -
- * - - - - - * -
* - - - - - - - *
- * - - - - - * -
- - * - - - * - -
- - - * - * - - -
- - - - * - - - -
任何想法都将非常有帮助。
你几乎已经钉上了它, 你唯一错过的就是RowCount - ColCount == number/2部分,请参阅下面的答案。你应该清楚这一点。 – nikhil
我对这两个答案感到非常失望。不是因为内容,而是因为原则......这个问题清楚地表明“这是为了我的家庭作业”,你们没有任何解释就给出了代码。人们不会通过复制/粘贴代码来学习。 – Coeffect
@Mannimarco我做了一个更新,他可以尝试使用outputraw组件来尝试显示为html。前面的信息可以在google或tapestry.org找到 – sfrj