0

所以我正在尝试使用 JFrames 创建一个基本的 RPG 用户 ID 选择菜单;我选择使用单选按钮显示 ID 选项。一旦用户选择了 ID,我就使用 JOptionPane 将其打回原处。所有这些都是在 MyClass 类中完成的。当用户进行 ID 选择时,我从 .txt 文件中读取以检查用户所做的选择,因为每个 userID 都与一个 Character 相关联,该 Character 的统计信息也包含在 .txt 文件中。我能够分解 .txt 文件的多行并将所有不同的特征存储为单独的字符串。我的问题是,现在我想在 MyClass 之外创建另一个使用 JFrames 显示所述特征的类,我在传递变量时遇到了麻烦,因为 .txt 文件的分解是在 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 声明为 public int 和 String 现在,我希望在 MyClass 之外的另一个类中按照调用 ID 的方式做一些事情,并可能通过几个 if-else 运行它查看要显示的字符。问题是,当我实例化构造函数并尝试访问变量时,它返回 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);

} }

4

2 回答 2

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.
}

你想要你的视图类或类:你的 customJFrames和 Swing ActionListeners。这些在验证 id 或从文本文件中读取数据方面应该不起作用。

你的MyClass(这是一个你想避免的名字,因为在六个月内你会忘记你试图做什么),把它全部剪掉,JFrames变成JButtons一个控制者。它和兄弟类Toons从文本文件中读取并将它们放入模型类中。它从视图中侦听ActionEvents,尝试执行用户想要的操作,然后更新模型和视图,或者向视图报告错误并保留模型。

于 2013-05-16T03:17:53.180 回答
0

在你的CharacterEditor类中,你只是实例化你的MyClassand 只是不会调用并为andcheckID()分配正确的值(除非你在你的构造函数中调用它)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并提供publicgetter(和可变属性的 setter),以便在其他类中访问它们。此外,正如@DaveNewton所指出的,else if建议使用 。

于 2013-05-16T03:24:11.140 回答