java - 如何将这些放入 Java GUI 中

标签 java arrays user-interface for-loop

我在将这些转换成 gui 时遇到问题。有人能帮我吗?我只是一个初学者,想了解更多。

我的问题是由于 for-loop 语句,我不知道如何将进程放在 gui 上。我不知道如何在单个 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);
    }

最佳答案

创建一个 jtextfields 数组,其大小与输入数量相同。然后在文本字段的监听器类中进行下一个输入。继续执行此过程,直到达到最大输入数的长度。您可以轻松地编写此想法

关于java - 如何将这些放入 Java GUI 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512607/

相关文章:

java - 通过扫描文件系统查找直接和间接子类

java - Jersey 2.5 : Replacement for JResponse?

java - 为什么 String.endsWith 和 String.startsWith 不一致?

c# - 如何按类实例的属性值对包含类对象的数组进行排序?

java - 如何根据项目的主序列列表对一小部分项目进行排序?

java - 仅当谓词输入不是空字符串时使用 Guava 过滤对象

java - 将数据从文件打印到数组

java - 如何在运行时启用/禁用 SWT Text WRAP

c++ - 再次关于在 QT 中将窗口大小调整为其子项的大小

java - 在 Swing 中使用具有绝对布局的滚动条