2014-02-06 73 views
1

我正在使用Java和GridBagLayout重新創建圖像中顯示的窗體。爲了在一週內創建網格線,我將帶有邊框的空JLabel插入到空單元格中。這對我來說非常合適,然後我決定讓每個在表單上加陰影的空單元格都用java着色,這就是我掙扎的地方。在我for循環中發現邏輯錯誤

labor record

我的想法是,我可以創造一個「綠蔭指針」像我和我的X和Y座標一樣,這shadePtr將與8值開始,因爲這是第一個行進行shadded,當循環遮住一行時,它會將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 
     } 
    } 

回答

2

到目前爲止,我獲得成功的唯一一點是,如果我註釋掉的代碼的最後一行 你看到(shadePtr = shadePtr + 3)但那麼只有一個行陰影中 ,我似乎無法弄清楚我在這裏做錯了什麼, 會感謝您的時間和精力。

如果我理解你的代碼正確的事情是:

  • yPointer是網格的「行」數。
  • shadePtr是要着色的下一行的索引。

問題是你在每次迭代中(這是很好的)增加yPointer在1個單元shadePtr在3個單位也在每次迭代增加。由於yPointer從7開始,並且shadePtr從8開始,那麼這些變量永遠不會等於,因爲shadePtr將始終大於yPointer

如果您在第二次迭代yPointer == shadePtr == 8中評論最後一行,那是第一個陰影行。但後來yPointer將增加,並且shadePtr仍然8,因此不再有行將被遮擋。

解決你的問題,但讓在最後這個小變化的數字3是很重要的:

int yPointer = 7; 
int xPointer = 3; 
int shadePtr = 8; 

for (int j = 0; j <= 299; j++) { 
    ... 
    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; 
     if((j % 3) == 0) { 
      shadePtr = yPointer; 
     } 
    } 
} 

這意味着,如果j是3的倍數,則下一行應該被遮蔽,從而使這個任務:shadePtr = yPointer增加後yPointer。這將影響這些行號:8,11,14,17,20,23,26,29,32和35.

這樣,您的問題應該通過做一點改變來解決,但注意事項shadePtr實際上是不必要的。你可以有一個簡單的布爾知道,如果該行應陰影:

int yPointer = 7; 
int xPointer = 3; 
boolean shade = false; 

for (int j = 0; j <= 299; j++) { 
    ... 
    if(xPointer == 13) { // end of column 
     yPointer++; //go down a row 
     xPointer = 3; 
     shade = (j % 3) == 0; 
    } 
} 
+0

感謝@ dic19,您的解決方案工作。但是爲了我自己的理解,你能回答這個問題嗎?你說我的shadePtr每迭代增加3次,但是我認爲通過在最後一個else語句中增加shadePtr 3,只有當我到達結尾時該行應該將我的新shadePtr設置爲11,這是我希望下一行被遮擋的位置。我的推理出了什麼問題? –

+0

哦等一下,它剛剛在我身上發現。我必須仔細閱讀你的解釋才能沉入其中。再次感謝!精彩的解釋。 –

+0

@solleks歡迎您!很高興幫助你。理解這有點棘手,但認爲你得到它:) – dic19