我希望目前只有两个问题。在先前提出的问题上,我对此有所帮助,但是现在我遇到了其他问题。我当前的问题是我需要将排序后的数组打印到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);
}
}
需要什么:
气泡排序按钮中的当前更新:
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);
}
不要尝试将Scanner输入与Swing GUI混合使用。您的问题是这样做的,您要绑定Swing事件线程,并在程序等待扫描程序输入时冻结GUI。
最好通过GUI来获取输入,最好是通过JTextComponents(如JTextFields)获得输入,或者(如果必须的话)通过JOptionPane来获取输入,只要它能与Swing事件线程配合使用即可。
编辑
您的评论:
- GUI将具有三个按钮,分别代表排序方法o气泡o快速o合并
- GUI将有一个区域供用户输入5个数字进行排序。将它们存储在array或arrayList中
- GUI将在其中显示排序的每个步骤的区域。这可以是一个区域,每完成一个步骤,它就会替换一行或显示每一步的几行。
您知道可以给您的GUI 5个JTextAreas,每个要输入的数字为1个。
编辑2
您声明:
所以我也在尝试新的东西。不确定是否正确。因此,当按下按钮时,我将其置于动作侦听器中:userInput = inputArea.getText(); 扫描仪输入=新的Scanner(userInput); 这就是你在说的吗?
那可能行得通,但是如果以这种方式这样做,则需要一个JTextArea,并且用户将必须知道其编号应以单个空格分隔。再次,扫描仪可以在这里正常工作,因为您可以调用nextInt()
它并获取您的数据(只要数据是数字,并且输入了正确的数字)。但同样,除非在某些特殊情况下,否则请勿将Scanner和System.in一起用于GUI。还请记住,完成后关闭扫描仪。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句