如何将它们放在Java GUI中

时间:2016-05-29 17:00:02

标签: java arrays user-interface for-loop

我有问题把这些变成gui。有人能帮我吗?我只是一个想要学习更多东西的初学者。

我的问题是我不知道如何将进程放在gui上,因为for循环语句。我不知道如何在单个jtextfield中输入多个数字。

这是我的代码:

public static void main(String[] args) {
        int choice, n, temp;

        System.out.print("Enter number of elements: ");

        Scanner input = new Scanner(System.in);

        n = input.nextInt();

        int[] a = new int[n];

        System.out.println();

        System.out.println("Enter " + n + " different non-negative numbers: ");

        for(int i=0; i<n; i++)
        {
            a[i] = input.nextInt();
        }

        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n-1; j++)
            {
                if(a[j]>a[j+1])
                {
                   temp = a[j];
                   a[j] = a[j+1];
                   a[j+1] = temp;
                }
            }
        }

        System.out.print("The set contains = {");

        for(int k=0; k<n; k++)
        {
            if(k<=n-2)
            {
                System.out.print(a[k] + ", ");
            }
            else
            {
                System.out.println(a[k] + "}");
            }
        }

        System.out.println();

        do
        {
            System.out.println("Pick a relation to see which ordered pairs belong to any of it");
            System.out.println("[1] R = {(a,b)|a < b}");
            System.out.println("[2] R = {(a,b)|a > b}");
            System.out.println("[3] R = {(a,b)|a != b}");
            System.out.println("[4] exit");
            System.out.print("Enter your choice: ");
            choice = input.nextInt();
            switch(choice)
            {
            case 1:
                System.out.println("Solution: ");
                for(int i=0; i<n; i++)
                 {
                     for(int j=0; j<n; j++)
                     {
                         if(a[i] < a[j])
                         {
                            System.out.print("(" + a[i] + ", " + a[j] + ")");
                            System.out.println();
                         }
                     }
                 }
                System.out.println();
                break;

            case 2:
                System.out.println("Solution: ");
                for(int i=n-1 ; i>=0; i--)
                 {
                     for(int j=n-1; j>=0; j--)
                     {
                         if(a[i] > a[j])
                         {
                             System.out.print("(" + a[i] + ", " + a[j] + ")");
                             System.out.println();
                         }
                     }
                 }
                System.out.println();
                break;

            case 3:
                System.out.println("Solution: ");
                for(int i=0; i<n; i++)
                 {
                     for(int j=0; j<n; j++)
                     {
                         if(a[i] != a[j])
                         {
                             System.out.print("(" + a[i] + ", " + a[j] + ")");
                             System.out.println();
                         }
                     }
                 }
                System.out.println();
                break;

            case 4:
            System.exit(0);

            default:
            System.out.println("Invalid input!");
            }
        }while(choice !=5);
    }

1 个答案:

答案 0 :(得分:1)

创建一个与输入数量相同的jtextfield数组。然后在文本字段的监听器类中进行下一个输入。继续这个过程,直到你达到最大输入数量的长度.u可以很容易地编码这个想法