2014-10-01 78 views
0

我創建了一個程序來顯示紅色和黑色框。當我編譯它時有一個錯誤,說我用來聲明我的JFrame設置可見的變量是不可達的語句。我不知道我做錯了什麼。帶有JFrame的無法訪問的聲明

import javax.swing.*; 
import java.awt.*; 
public class test { 

    public static void main(String[] args) { 
     JFrame theGUI = new JFrame(); 
     theGUI.setTitle("Colour"); 
     theGUI.setSize(300, 300); 
     theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Container pane = theGUI.getContentPane(); 
     pane.setLayout(new GridLayout(8, 8)); 
     Color color1 = Color.black; 
     Color color2 = Color.red; 
     for (int i = 1; 1 <= 64; i++){ 
      JPanel panel = new JPanel(); 
      //Alternate colors on a row 
      if (i % 2 == 0) 
       panel.setBackground(color1); 
      else 
       panel.setBackground(color2); 
      pane.add(panel); 
      // at the end of a row start next row on the other color1 
      if (i % 8 == 0){ 
       Color temp = color1; 
       color1 = color2; 
       color2 = temp; 

      } 

     } 
    theGUI.setVisible(true);  

    } 

} 

回答

0

我相信你現在已經知道了。循環條件是錯誤的......說1 < = 64,應該是我< = 64 ... /Mattias

+0

啊是啊我很愚蠢非常感謝你。我對這一切都很陌生。 – Tom 2014-10-01 14:24:22