将System.out.println转换为JOptionPane?

时间:2013-02-25 21:52:43

标签: java swing joptionpane anagram

想输出这个程序,告诉两个单词是否是字谜。我想知道如何将所有System.out.print命令更改为JOption Pane命令!我很乐意得到任何帮助,因为我是一名程序员,并且应该完成这项任务。

import java.util.Arrays;

public class NewAnagram
{
public static void main(String[] args)
{
    if (args.length != 2)
    {
        System.out.println("2 words have not been entered!");        
    }
    else
    {
        printPhrases(args[0], args[1]);                              
    }
}

public static boolean anagramSearch(String phrase1, String phrase2) 
{

    String ltrsOnlyOne = lettersOnly(phrase1);
    String ltrsOnlyTwo = lettersOnly(phrase2);      
    char[] first = ltrsOnlyOne.toLowerCase().toCharArray();
    char[] second = ltrsOnlyTwo.toLowerCase().toCharArray();

    Arrays.sort(first);
    Arrays.sort(second);

    if (Arrays.equals(first, second))
    {
        return true;
    }
    else
    {
        return false;
    }   
}

public static String lettersOnly(String word) 
{
    int length = word.length();
    StringBuilder end = new StringBuilder(length);
    char j;

    for (int i = (length - 1); i >= 0; i--) 
    {
        j = word.charAt(i);
        if (Character.isLetter(j)) 
        {
            end.append(j);
        }
    }
    return end.toString();
}

public static void printPhrases(String phrase1, String phrase2)
{
    boolean isFound = anagramSearch(phrase1, phrase2);
    if (isFound == true) 
    {
        System.out.println(phrase1 + " is an anagram of "+ phrase2);
    }

    if (isFound == false) 
    {
        System.out.println(phrase1 + " is not an anagram of "+ phrase2);
    }
}

}

1 个答案:

答案 0 :(得分:2)

这样的行
System.out.println(phrase1 + " is not an anagram of "+ phrase2);

将在JOptionPane版本中:

JOptionPane.showMessageDialog(null, phrase1 + " is not an anagram of " + phrase2);

其中第一个参数是对话框的 parent 。由于您的应用程序中没有任何其他窗口,因此只需传递null