2013-11-14 38 views
0

謝謝你們幫助我解決這個問題。我已經設法完成了這個程序所需的一切,除了我不能得到需要輸出到GUI的結果。我已經看過其他論壇,有些人說輸出到textField而不是textArea,但任何一種方式,我仍然最終得到一個錯誤。不能輸出到圖形用戶界面,可以輸出到控制檯

這是我的錯誤,當我outputArea使用.append設置爲文本框:

The method append(int) is undefined for the type JTextField. 

我只是好奇,我應該用什麼這個問題。

import java.awt.EventQueue; 
import javax.swing.*; 
import java.awt.event.*; 
import java.util.*; 

public class Sorting { 

private JFrame frame; 
private JTextArea inputArea; 
private JTextField outputArea; 
String userInput; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Sorting window = new Sorting(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public Sorting() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setTitle("Sorting"); 
    frame.getContentPane().setLayout(null); 

    JButton bubbleButton = new JButton("Bubble Sort"); 
    bubbleButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      String userInput = inputArea.getText(); 
      String[] output = userInput.split(" "); 
      int[] list = new int[output.length]; 

      for(int i = 0; i < output.length; i++){ 
       try{ 
        list[i] = Integer.parseInt(output[i]); 
       } catch (NumberFormatException nfe){}; 
      } 
      bubbleSort(list); 
      for(int k = 0; k < list.length; k++){ 
       outputArea.setText(list[k] + " "); 
//     System.out.print(list[k] + " "); 
      } 

     } 

    }); 

    bubbleButton.setBounds(10, 211, 114, 23); 
    frame.getContentPane().add(bubbleButton); 

    JButton mergeButton = new JButton("Merge Sort"); 
    mergeButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String userInput = inputArea.getText(); 
      String[] output = userInput.split(" "); 
      int[] list = new int[output.length]; 

      for(int i = 0; i < output.length; i++){ 
       try{ 
        list[i] = Integer.parseInt(output[i]); 
       } catch (NumberFormatException nfe){}; 
      } 
      mergeSort(list); 
      for(int k = 0; k < list.length; k++){ 
       System.out.print(list[k] + " "); 
      } 
     } 
    }); 
    mergeButton.setBounds(305, 211, 114, 23); 
    frame.getContentPane().add(mergeButton); 

    JButton quickButton = new JButton("Quick Sort"); 
    quickButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String userInput = inputArea.getText(); 
      String[] output = userInput.split(" "); 
      int[] list = new int[output.length]; 

      for(int i = 0; i < output.length; i++){ 
       try{ 
        list[i] = Integer.parseInt(output[i]); 
       } catch (NumberFormatException nfe){}; 
      } 
      quickSort(list); 
      for(int k = 0; k < list.length; k++){ 
       System.out.print(list[k] + " "); 
      } 
     } 
    }); 
    quickButton.setBounds(163, 211, 114, 23); 
    frame.getContentPane().add(quickButton); 

    inputArea = new JTextArea(); 
    inputArea.setBounds(10, 36, 414, 51); 
    frame.getContentPane().add(inputArea); 

    outputArea = new JTextField(); 
    outputArea.setEditable(false); 
    outputArea.setBounds(10, 98, 414, 59); 
    frame.getContentPane().add(outputArea); 
    outputArea.setColumns(10); 

    JLabel label = new JLabel("Please Enter 5 Numbers"); 
    label.setHorizontalAlignment(SwingConstants.CENTER); 
    label.setBounds(10, 11, 414, 14); 
    frame.getContentPane().add(label); 

} 

protected void quickSort(int[] list) { 
    quickSort(list, 0, list.length - 1); 
} 

private void quickSort(int[] list, int first, int last) { 
    if(last > first){ 
     int pivotIndex = partition(list, first, last); 
     quickSort(list, first, pivotIndex -1); 
     quickSort(list, pivotIndex + 1, last); 
    } 

} 

private int partition(int[] list, int first, int last) { 
    int pivot = list[first]; 
    int low = first + 1; 
    int high = last; 

    while(high > low){ 
     while(low <= high && list[low] <= pivot) 
      low++; 

     while(low <= high && list[high] > pivot) 
      high--; 
     if(high > low){ 
      int temp = list[high]; 
      list[high] = list[low]; 
      list[low] = temp; 
     } 

    } 

    while(high > first && list[high] >= pivot) 
     high--; 

    if(pivot > list[high]){ 
     list[first] = list[high]; 
     list[high] = pivot; 
     return high; 
    } 
    else{ 
    return first; 
    } 
} 

protected void mergeSort(int[] list) { 
    if(list.length > 1){ 
     int[] firstHalf = new int[list.length/2]; 
     System.arraycopy(list, 0, firstHalf, 0, list.length/2); 
     mergeSort(firstHalf); 

     int secondHalfLength = list.length - list.length/2; 
     int[] secondHalf = new int[secondHalfLength]; 
     System.arraycopy(list, list.length/2, secondHalf, 0, secondHalfLength); 
     mergeSort(secondHalf); 

     merge(firstHalf, secondHalf, list); 
    } 
} 

private void merge(int[] list1, int[] list2, int[] temp) { 
    int current1 = 0; 
    int current2 = 0; 
    int current3 = 0; 

    while(current1 < list1.length && current2 < list2.length) { 
     if(list1[current1] < list2[current2]) 
      temp[current3++] = list1[current1++]; 
     else 
      temp[current3++] = list2[current2++]; 
    } 

    while(current1 < list1.length) 
     temp[current3++] = list1[current1++]; 

    while(current2 < list2.length) 
     temp[current3++] = list2[current2++]; 

} 

protected void bubbleSort(int[] list) { 
    boolean needNextPass = true; 
    for(int k = 1; k < list.length && needNextPass; k++){ 
     needNextPass = false; 
     for (int i = 0; i < list.length - k; i++){ 
      if(list[i] > list[i + 1]){ 
       int temp = list[i]; 
       list[i] = list[i + 1]; 
       list[i + 1] = temp; 

       needNextPass = true; 
      } 
     } 
    } 

} 
} 

當我有我的設置是這樣的冒泡排序按鈕outputArea我沒有錯誤,並且打印出用戶inputed的最高數字。

for(int k = 0; k < list.length; k++){ 
       outputArea.setText(list[k] + " "); 
//     System.out.print(list[k] + " "); 
      } 
+0

我發現關於這一切的一切是如何將int數組轉換爲字符串數組,我相信我有,但我只是不知道如何輸出到GUI。我可以輸出到Eclipse中的控制檯,但我不能輸出到GUI。我只需要知道如何從控制檯的輸出到GUI。 – user2929005

回答

1

我在十年內還沒有用過Java,但在我看來,例外是抱怨文本字段不接受數字。

嘗試在傳遞到GUI之前將您的int格式化爲字符串(例如嘗試list[k].toString()或某種置換,但我希望+ " "無論如何都將該值無聲地強制爲字符串)。

+0

+「」確實消除了錯誤,它只輸出列表的最高值。氣泡排序顯然會排序用戶輸入的任何數字我不能想到正確的方式來獲得輸出顯示數組。除非我沒有正確設置陣列。 – user2929005

+0

我假設setText函數將該字段的內容設置爲該值;也就是說,它會覆蓋以前的值。你可能想要找到一個像.appendText()這樣的方法,或者在循環中構建一個StringBuffer,並在循環之後調用setText(StringBuffer)一次。 –