2013-11-28 76 views
1

我試圖獲得outputArray的值在類中初始化LightsOutPuzzle在我的課,解決。但調用類之前打印陣列無法正確訪問使用另一個類的陣列

1)中的元素之後調用解決後解決LightsOutPuzzle 2),這些元件是不相等的,這是因爲如果它已被重新初始化。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import java.util.StringTokenizer; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 

public class LightsOutPuzzle extends JFrame implements ActionListener { 

    public JFrame f; 
    public JPanel p1; 
    public JPanel p2; 
    public JButton[] buttonArray; 
    public JButton solve; 
    public int nRows = 5; 
    public int nColumns = 5; 
    public int nTotalButtons = nRows * nColumns; 
    public int[] outputArray = new int[25]; 

    public void create() { 
     init(); 
     gui(); 
    } 

    public void gui() { 
     f = new JFrame("Lights Out Puzzle"); 
     f.setVisible(true); 
     f.setSize(500,500); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     p2 = new JPanel(); 

     solve = new JButton("Solve"); 

     p2.add(solve); 

     f.add(p1); 
     f.add(p2, BorderLayout.SOUTH); 

     solve.addActionListener(new Action()); 
    } 

    public void init() {  
     p1 = new JPanel(); 
     p1.setLayout(new GridLayout(nRows, nColumns, 0, 0)); 

     int[] inputArray = new int[25]; 
     int i=0; 

     buttonArray = new JButton[nTotalButtons]; 
     for (int nNum = 0; nNum < nTotalButtons; nNum++) 
     { 
      buttonArray[nNum] = new JButton(""); 
      add(buttonArray[nNum]); 
      //clickability 
      buttonArray[nNum].addActionListener(this); 
      buttonArray[nNum].setBackground(Color.yellow); 
      buttonArray[nNum].setActionCommand("" + nNum); 
     } 

     //reading config file 
     BufferedReader br = null; 
     try { 
      String line; 
      br = new BufferedReader(new FileReader("input.txt")); 

      while ((line = br.readLine()) != null) { 
       StringTokenizer stringTokenizer = new StringTokenizer(line, " "); 
       while (stringTokenizer.hasMoreElements()) { 
        inputArray[i] = Integer.parseInt(stringTokenizer.nextElement().toString()); 
        i++; 
       } 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null) 
        br.close(); 

      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     //drawing text input 
     for(i=0; i<nTotalButtons; i++){ 
      if(inputArray[i] == 1) 
       buttonArray[i].setBackground(Color.white); 
      p1.add(buttonArray[i]); 
     } 
    } 

    public void save() { 
     int i = 0;  
     for (i=0; i<25; i++) { 
      if(buttonArray[i].getBackground() == Color.yellow) 
       outputArray[i] = 1; 
      else 
       outputArray[i] = 0; 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     //conditions for the puzzle 
    } 

    public class Action implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      int i; 

      save(); 
      for(i=0; i<25; i++) 
       System.out.println(outputArray[i]); 
      new solve(); 
     } 
    } 

    public static void main(String[] args) { 
     LightsOutPuzzle lop = new LightsOutPuzzle(); 
     lop.create(); 
    } 
} 

此時,所有outputArray的元素等於1。但是,當我打印他們使用其它類,解決,的所有元素outputArray現在等於0

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import java.util.*; 

public class solve extends LightsOutPuzzle { 
private JFrame f; 
private JPanel p1; 

int[] inputArray = new int[25]; 
int[] zeroArray = new int[25]; 
int[] UIState = new int[25]; 
int[] boardState = new int[25]; 
Queue<int[]> queue = new LinkedList<int[]>(); 

public solve() { 
    gui(); 
    init(); 
} 

public void gui() { 
    f = new JFrame("Solution"); 
    f.setVisible(true); 
    f.setSize(500,500); 
    f.setLocationRelativeTo(null); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    p1 = new JPanel(); 
    p1.setLayout(new GridLayout(nRows, nColumns, 0, 0)); 

    buttonArray = new JButton[nTotalButtons]; 
    for (int nNum = 0; nNum < nTotalButtons; nNum++) { 
     buttonArray[nNum] = new JButton(""); 
     add(buttonArray[nNum]); 
     //buttonArray[nNum].setBackground(Color.white); 
     buttonArray[nNum].setActionCommand("" + nNum); 
     p1.add(buttonArray[nNum]); 
    } 

    f.add(p1); 
} 

public void init() { 
    //drawing text input 
    int i; 
     for(i=0; i<25; i++) 
      System.out.println(outputArray[i]); 
    for(i=0; i<nTotalButtons; i++){ 
     if(outputArray[i] == 1) 
      buttonArray[i].setBackground(Color.yellow); 
    } 
} 

public static void main(String[] args) { 
} 

} 

我也很感謝有關在Java中正確編程實踐的提示。這是一個漫長的時間,因爲我在這個語言編碼,所以我只是想從我所記得創建一個程序..

+2

提示#1:[SSCCE =短,自包含,正確(可編譯),示例](http://sscce.org/) – Domi

回答

1

的問題是,你立足於(你的按鈕的顏色),你的決定變量變因爲您正在重新創建gui方法中從solve調用的按鈕。因此,您的初始1再次變爲0,這就是在init期間打印的內容。

重要提示:請勿將GUI屬性用於核心邏輯。

1

即使solve類延伸LightsOutPuzzle,它是一個不同的對象,所以它有自己的副本outputArray。在要打印的outputArray實例的代碼在LightsOutPuzzle的第一部分已被初始化的:

的ActionListener在LightsOutPuzzle

public class Action implements ActionListener { 
     public void actionPerformed (ActionEvent e) { 
      int i; 

      save(); 
      for(i=0; i<25; i++) 
        { 
         /////////////PRINTS OUT outputArray IN LightsOutPuzzle 
         System.out.println(outputArray[i]); 
        } 

        ////CREATES NEW solve OBJECT WITH ITS OWN outputArray COPY 
        new solve(); 
     } 
    } 

然後,當你調用solve構造函數時,打印solve尚未初始化且全部爲零的副本:

初始化方法在求解類

public void init() { 
    //drawing text input 
    int i; 
     for(i=0; i<25; i++) 
        { 
         //////PRINTS OUT outputArray IN solve CLASS 
         System.out.println(outputArray[i]); 
        } 
        for(i=0; i<nTotalButtons; i++){ 
         if(outputArray[i] == 1) 
          buttonArray[i].setBackground(Color.yellow); 
         } 
     } 
+0

它們不同?那麼我如何訪問LightsOutPuzzle中的那個? – Gannicus

+0

我不確定你的意圖是爲了在類中繼承LightsOutPuzzle,因爲你應該繼承子類來共享類之間的行爲,而你並沒有這樣做。我認爲你是繼承分享狀態,但狀態不會自動在同一類或子類的不同副本之間共享。一目瞭然,不繼承LightsOutPuzzle,並通過LightsOutPuzzle狀態(只是outputArray如果這就是你關心)的解決構造函數。還有一點提示 - 遵循Java編碼約定和CamelCase類名稱。所以class'solve'應該是'Solve'。 – lreeder

+0

明白了。但是將outputArray傳遞給Solve類最簡單的方法是什麼? – Gannicus