谢谢你们帮我解决这个问题。我已经设法完成了这个程序所需的一切,除了我无法得到我需要输出到GUI的结果。我看过其他论坛,有些人说输出到textField而不是textArea,但无论哪种方式我最终都会收到错误。
使用.append:
将outputArea设置为textField时出现错误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时,我没有错误,并打印出用户输入的最高数字。
for(int k = 0; k < list.length; k++){
outputArea.setText(list[k] + " ");
// System.out.print(list[k] + " ");
}
答案 0 :(得分:1)
我在十年内没有使用过Java,但在我看来,例外是抱怨文本字段不接受数字。
尝试在传递给GUI之前将int格式化为String(例如,尝试list[k].toString()
或一些排列,尽管我希望+ " "
能够默默地将值强制转换为字符串。< / p>