从JFrame访问方法

时间:2013-04-09 16:31:35

标签: java jframe

我制作了以下课程StringToolsNS123,其中包含许多方法

public class StringToolsNS123 {

private String str;

public StringToolsNS123(String s) {
    str = s;
}

public String getString() {
    return str;
}

public void setString(String s) {
    str = s;
}

public String toString()    {
    return str;
}

public int countWord()    {

     String st = " " + str;
     char curr, prev ;
     int count =0;
     for (int i = 0; i <= st.length()-2; i++)
     {
        prev = st.charAt(i);
        curr = st.charAt(i+1);
        if (Character.isLetter(prev) == false && Character.isLetter(curr) == true)
         {
             count++;
         }
     }
     return count;
}
public void firstCaps (String str)
{
    String temp = " " + str ;
    temp = temp.toLowerCase();
    String newStr= "";
    char prev , curr ; 
    for (int loop = 1; loop < temp.length()-1; loop++)
    {
        prev = temp.charAt(loop - 1);
        curr = temp.charAt(loop);
        if (Character.isLetter(prev)== false && Character.isLetter(curr)== true )

        {
            newStr = newStr + Character.toUpperCase(curr);

        }
        else
        {
            newStr = newStr + curr ;
        }

    }
}
public void removeVowels(String str)
{
    String temp = " " + str ;
    temp = temp.toLowerCase();
    String newStr = " ";
    char prev , curr ;
    final String VOWELS = "aeiouAEIOU";

    for (int i = 1; i < temp.length()-1; i++)
    {
        prev = temp.charAt(i-1);
        curr = temp.charAt(i);
        if (Character.isLetter(prev)== false && VOWELS.indexOf(temp.charAt(i))>= 0 || VOWELS.indexOf(temp.charAt(i))== - 1 )
        {
            newStr = newStr + temp.charAt(i);
        }
    }
   str = newStr ;

}
private String encodeWord (String w )
{
    final String VOWELS = "aeiouAEIOU";
    if (VOWELS.indexOf(w.charAt(0))== -1 )

    {
        w = w.substring(1) + w.charAt(0);

    }
     w = w + "ay";
     return  w; 
}
public void pigLatin (String str)
{
    String singleWord, temp = " " + str + " ";
    int begin = 0;
    temp = temp.toLowerCase ();
    String pigStr = " ";
    char prev, curr;
    for (int loop = 1; loop < temp.length ()-1; loop++)
    {
        prev = temp.charAt(loop-1);
        curr = temp.charAt(loop);
        if (Character.isLetter(prev) == false && 
                Character.isLetter(curr) == (true))
        {
            begin = loop;
        }
        if (Character.isLetter(prev) == true && 
                Character.isLetter(curr) == (false))
    {
        singleWord = temp.substring (begin,loop);
        pigStr = pigStr + encodeWord (singleWord) + " "; 
    }
    }
}
public String reverse(String str){
    int strLeng = str.length()-1;
    String reverse = "", temp = "";

    for(int i = 0; i <= strLeng; i++){
        temp += str.charAt(i);
        if((str.charAt(i) == ' ') || (i == strLeng)){
            for(int j = temp.length()-1; j >= 0; j--){
                reverse += temp.charAt(j);
                if((j == 0) && (i != strLeng))
                {
                    reverse += " ";
                }
            }
            temp = "";
        }
    }

    return reverse;
}




}

我遇到的问题是从同一个包中的JFrame访问这些方法,我试过

   StringToolsNS123 str = new StirngToolsNS123 () ; 

但是它提出错误说 class not found

提前致谢!

2 个答案:

答案 0 :(得分:2)

StringToolsNS123的构造函数需要String

StringToolsNS123 str = new StringToolsNS123("some string");

您需要从getFirstCaps返回一个String才能从JFrame类获取访问权限:

public String firstCaps(String str) {
   ...
   return newStr;
}

通常,像这样的类被实现为具有static方法的实用程序类。在这种情况下,不需要任何实例,其用法是:

String firstCaps = MyStringUtilities.getFirstCaps("some string");

答案 1 :(得分:0)

除了你班级唯一的构造函数需要一个字符串(参见Reimeus的答案)之外,你的电话中还有一个拼写错误:

StringToolsNS123 str = new StirngToolsNS123 () ; 

必须是

StringToolsNS123 str = new StringToolsNS123 ("some text") ; 

请注意“StringToolsNS1232”中的拼写错误。我不确定你是否从实际代码中复制了这个错误,但调用一个不存在的构造函数会导致'构造函数StringToolsNS123()未定义'而不是'找不到类'。