java - 无法输出到GUI,可以输出到控制台

标签 java user-interface output

感谢你们在这个特定问题上为我提供了如此多的帮助。我已经成功完成了该程序所需的所有操作,但无法获得需要输出到 GUI 的结果。我看过其他论坛,有些人说输出到文本字段而不是文本区域,但无论哪种方式我仍然最终收到错误。

这是使用 .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;
            }
        }
    }

}
}

当我在冒泡排序按钮中设置我的输出区域时,我没有错误并打印出用户输入的最大数字。

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

最佳答案

我已经有十年没有使用过 Java 了,但在我看来,异常(exception)是提示文本字段不接受数字。

在传递到 GUI 之前尝试将 int 格式化为字符串(例如,尝试 list[k].toString() 或一些排列,尽管我期望 + "" 无论如何都会默默地将值强制为字符串)。

关于java - 无法输出到GUI,可以输出到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981904/

相关文章:

java - 在将 spring security 与 spring websocket 集成时,在套接字 Controller 方法中访问用户名和

java - 处理具有未知异常类的堆栈跟踪

c# - 选中时隐藏轨迹栏控件周围的虚线轮廓

maven - Maven 可以变得不那么冗长吗?

java - 在java代码中插入Mysql查询

java - Android-Magento-如何使用XML-RPC在Android中获取多个产品的详细信息

jquery - JQuery UI 中的自定义可调整大小的句柄

css - TornadoFX addClass 不添加CSS

c++ - istringstream 的五倍输出

java - 将文件读入数组返回错误的元素