GUI从用户输入冻结并输出到JTextField

时间:2013-11-10 21:47:43

标签: java user-interface arraylist jtextfield

我希望我现在只有两个问题。在我之前提出的一个问题上,我对此有所帮助,但现在我遇到了不同的问题。我现在的问题是我需要在JTextField中打印一个已排序的数组。我没有关于如何对数组进行排序的问题我希望在按下冒泡排序按钮时如何让数组打印到JTextField。目前,当我按下按钮时,它会冻结。它冻结在这条线上。

list.add(Integer.valueOf(input.nextInt()));

请帮助谢谢。

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;
ArrayList<Integer> list = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);

/**
 * 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) {
            list.add(Integer.valueOf(input.nextInt()));
//              for(int i = 0; i < list.size(); i++){
//                  
//              }

        }

    });

    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) {

        }
    });
    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) {

        }
    });
    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);

          }
}

需要什么:

  • GUI将有三个按钮代表排序方法o Bubble o Quick o Merge
  • GUI将有一个区域供用户输入5个数字进行排序。将它们存储在数组或arrayList
  • GUI将具有显示排序的每个步骤的区域。这可以是一个区域,每次完成一个步骤,它将替换显示每个步骤的一行或几行。

冒泡排序按钮中的当前更新:

 public void actionPerformed(ActionEvent arg0) {
            userInput = inputArea.getText();
            Scanner input = new Scanner(userInput);
            list.add(Integer.valueOf(input.nextInt()));
            for(int i = 0; i < list.size(); i++){
                outputArea.setText(userInput);
            }

1 个答案:

答案 0 :(得分:1)

不要尝试将扫描仪输入与Swing GUI混合使用。你的问题是通过这样做,你正在绑定Swing事件线程,冻结你的GUI,因为你的程序等待扫描仪输入。

最好通过GUI获得输入,最好是通过JTextFones等JTextComponents,或者如果你必须通过JOptionPane,只要它以与Swing事件线程相配的方式完成。


修改
你的评论:

  
      
  • GUI将有三个按钮代表排序方法o Bubble o Quick o Merge
  •   
  • GUI将有一个区域供用户输入5个数字进行排序。将它们存储在数组或arrayList
  • 中   
  • GUI将具有显示排序的每个步骤的区域。这可以是一个区域,每次完成一个步骤,它将替换显示每个步骤的一行或几行。
  •   

您知道您可以为您的GUI提供5个JTextAreas,每个号码都要输入一个。


编辑2
你说:

  

所以我也在尝试一些新的东西。不确定它是否正确。因此,当按下按钮时,我放入动作侦听器:userInput = inputArea.getText();扫描仪输入=新扫描仪(userInput);这是你在说什么吗?

这可行,但如果你这样做,你需要一个JTextArea,用户必须知道他们的数字应该由一个空格分隔。扫描仪再次可以在这里工作,因为您可以在其上调用nextInt()并获取您的数据(只要数据是数字,并输入正确的数字)。但是,除了在某些特殊情况下,不要将带有System.in的扫描仪与GUI一起使用。还记得在完成后关闭扫描仪。