2012-10-24 16 views
0
107054600 
003008529 
000000070 
004905100 
800700900 
012003000 
700000006 
200609004 
036010000 

這些文件以這種方式存儲Sudoku拼圖。將填充數字值的文件分配給JLabels的二維陣列

我必須能夠將這些謎題讀入到2d int數組和2d Jlabel數組中。

它僅在讀入Int數組時纔有效。然而,當我嘗試分配與當前數第j標籤的圖標價值正處理for循環我得到這個編譯器錯誤:

The constructor javax.swing.JLabel(int) is undefined

這是爲什麼?

相關的代碼如下:

JLabel[][] gridLabels = new JLabel[9][9]; 
int [][] grid = new int [9][9]; // Game Array 

try { 
String fileName1 = new Scanner(new File("./temp.txt")).useDelimiter("\\Z").next(); 
Scanner fin; 
fin = new Scanner(new File(fileName1)); 
//Reads in the puzzle from a temporary text file 

for(int row=0; row<9; row++) 
{ 
String line = fin.next(); 
for(int col=0; col<9; col++) 
    { grid[row][col] = line.charAt(col)-'0'; 
    gridLabels[row][col] = new JLabel(line.charAt(col)-'0'); //PROBLEM HERE// 
    } 
} 
    } 
    catch (FileNotFoundException exception) 
    {System.out.println("Incorrect Selection"); // Catches incorrect selection 
    } 
    catch (NoSuchElementException exception) 
    {System.out.println("No Such Element"); 
    } 

回答

0

int和錯誤說JLabel沒有接受一個int任何參數的構造函數。

嘗試new JLabel(String.valueOf(line.charAt(col)-'0'))

1

在構造函數中javax.swing.JLabel你可以傳遞一個String但不是int。 您應該將您的int轉換爲String,然後再將其傳遞給Label構造函數。 您可以從intString轉換爲String.valueOf(i)

here轉換結果的line.charAt(col)-'0'