2016-05-29 22 views
1

我有問題把它們變成gui。有人能幫我嗎?我只是一個想學更多東西的初學者。如何把這些在Java 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

使用相同的尺寸的輸入數JTextField的陣列。然後在文本字段的監聽器類中進行下一個輸入。繼續做這個過程,直到你達到輸入的最大數目的長度。我可以很容易地編碼這個想法