2013-05-16 27 views
0

所以我試圖用JFrame創建一個基本的RPG用戶ID選擇菜單;我選擇使用單選按鈕顯示ID選項。一旦用戶選擇並使用JOptionPane將其打回。所有這些都在MyClass類中完成。當用戶進行ID選擇時,我從.txt文件中讀取,以檢查用戶所做的選擇,因爲每個用戶ID都與一個字符相關聯,該字符的統計信息也包含在.txt文件中。我能夠分割.txt文件的多行,並將所有不同的特徵存儲爲單獨的字符串。我的問題是,現在我想創建MyClass之外的另一個類,它使用JFrame顯示所述特性,因爲在.txt文件的分解在Try-Catch內完成並且基於I已閱讀,這些變量只包含在Try-Catch的範圍內。這是在我提到的第一類中的方法是這樣的:如何在另一個類中使用方法(Try-Catch內部)的變量?

public void checkID() 
{ 

    StringBuilder allInfo=null; //Used to store all the Info 
    String line=null; //Used as condition for loop & to generate the String Builder 
    String strAll2=null; 
    String[] sa=null; 
    String[] sb=null; 
    String[] sd=null; 
    String[] se=null; 
    BufferedReader br=null; 
    //User Id as a string 

    try { 



     br = new BufferedReader(new FileReader("charList.txt")); 
     allInfo= new StringBuilder(); 

     while ((line=br.readLine()) != null) 
     { 
      allInfo.append(line + "\n"); 
     } 

     strAll2=allInfo.toString(); //all the info of charList.txt as one StringBuilder 
     sa=strAll2.split("\n"); //all the info of charList.txt as a String 
     sb=sa[0].split("\t"); //all the info of the first line of the file as individual strings 
     sd=sa[1].split("\t"); //all the info of the second line of the file as individual strings 
     se=sa[2].split("\t"); //all the info of the third line of the file as individual strings 


     if (firstIdButton.isSelected()) 
     { 
      strId=sb[0]; 
     } 
     else 
     { 
      if (secondIdButton.isSelected()) 
     { 
      strId=sd[0]; 
     } 
      else 
      { 
       if (thirdIdButton.isSelected()) 
       { 
        strId=se[0]; 
       } 
      } 
     } 

     ID = Integer.parseInt(strId); 
    }//end of try 
    catch (IOException e) { 
     e.printStackTrace();   

    }//end of catch 



    setVisible(false); //Hides Window 



}//end of check id method 

我宣佈這兩個ID和strId公共int和字符串,分別在現在的主類,我希望做什麼沿着MyClass之外的另一個類中的調用ID的行,並且可能通過幾個if-elses來運行它,以查看要顯示哪個字符。問題是,當我實例化構造函數並嘗試訪問該變量時,它返回0,因爲try-catch的作用域不允許我將它與塊內獲得的值一起傳遞。我也試圖讓我的方法返回ID的價值,但我遇到了同樣的問題外MyClass的

這是我無法實現它

公共類CharacterEditor {

public static void main(String[] args) throws IOException{ 

    MyClass iw= new MyClass(); 
    int theID=iw.ID; 
    System.out.println(theID); 

} }

+0

我不是很理解你的問題是什麼。你真的嘗試過嗎?獲取任何錯誤? – Breavyn

+0

如果您需要註解右括號,那麼您的塊的_far_太長。 –

+0

Colin-I添加了更多代碼來顯示問題出在哪裏。我曾嘗試過這樣做,但沒有得到任何運行時錯誤,但是我在MyClass中爲ID獲取的值與CharacterEditor中獲得的ID的值不匹配;我在CharacterEditor中得到一個'0',這意味着當我這樣稱呼它時,它不能初始化它。 – Marco

回答

1

你需要分解你的課程和他們的責任。您需要模型類:表示您的遊戲角色的類,以及執行遊戲所需計算的類。像

/** 
* A character from the game Toon. 
* @see http://en.wikipedia.org/wiki/Toon_(role-playing_game) 
*/ 
public class Toon implements Serializable { 
    private int muscle; 
    private int zip; 
    private int smarts; 
    private int chutzpah; 
    private String name; 
    private String species; 
    // Constructor, getters, setters, etc. 
} 

你想要的東西您的視圖類或類:自定義JFrames和Swing ActionListeners。這些不應該在驗證ID或從文本文件讀取數據方面做任何工作。

MyClass(這是要避免的,因爲在六個月內,你會忘記你試圖做一個名字),剝奪其所有JFramesJButtons成爲一個控制器。它和兄弟班從文本文件中讀取Toons並將它們放入模型類中。它從視圖中偵聽ActionEvent,嘗試執行用戶想要的操作,並更新模型和視圖,或者向視圖報告錯誤並將模型單獨留下。

0

在你CharacterEditor類你只實例化您的MyClass,只是不會調用checkID()並分配適當的值IDstrId(除非你在構造函數調用它)

MyClass iw= new MyClass(); 
iw.checkID(); // MISSING! 
int theID=iw.ID; 
System.out.println(theID); 

checkID() ID將默認爲0(給予未初始化的int成員變量的默認值),並且strId將保持爲null。你相信try-catch會以某種方式阻止你設置的值在其他地方顯示是不正確的。

Try-catch就像任何其他具有大括號的Java構造一樣創建它自己的Scope,並且在範圍內聲明的任何內容都不可見。但是,如果一個變量已經存在於它的範圍之外(比如你的情況下的ID),那麼在該範圍內對其值進行的任何更改也會反映在該範圍之外。

而且,最好將成員字段聲明爲private,並提供public獲取器(以及可變屬性的設置器),以便在其他類中訪問它們。另外,如@DaveNewton指出,推薦使用else if

相關問題