实现:输入数组,并排序

计算部分代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
protected void do_button_actionPerformed(ActionEvent e) {
        String text = arrayField.getText();// 获取用户输入
        text=text.trim();       //去除首尾空格
        if("".equals(text)){
            JOptionPane.showMessageDialog(null,"请输入要排序的数组内容");
            return;
        }
        String[] arrayStr = text.split(" {1,}");// 拆分输入为数组
        int[] array = new int[arrayStr.length];// 创建整数类型数组
        sortArea.setText("数组原有内容:\n");
        for (String string : arrayStr) {// 输出原有数组内容
            sortArea.append(string + "    ");
        }
        for (int i = 0; i < array.length; i++) {// 初始化整形数组
            array[i] = Integer.parseInt(arrayStr[i]);
        }
        sortArea.append("\n");
        Arrays.sort(array);// 使用sort方法对整形数组进行排序
        sortArea.append("数组排序后的内容:\n");
        for (int value : array) {// 输出排序后的数组内容
            sortArea.append(value + "    ");
        }
    }
            
    protected void do_arrayField_keyPressed(KeyEvent e) {
        char key = e.getKeyChar();// 获取用户按键字符
        String mask = "0123456789 " + (char8;// 定义规范化字符模板
        if (mask.indexOf(key) == -1) {// 判断按键字符是否属于规范化字符范围
            e.consume();// 取消非规范化字符的输入有效性
        }
    }
}

效果图:

163028158.jpg

完整代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package com.lixiyu;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
public class SortExamplePerfect extends JFrame {
       
    /**
     *
     */
    private static final long serialVersionUID = 6185462970912293135L;
    private JPanel contentPane;
    private JTextField arrayField;
    private JTextArea sortArea;
       
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        catch (Throwable e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SortExamplePerfect frame = new SortExamplePerfect();
                    frame.setVisible(true);
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
       
    /**
     * Create the frame.
     */
    public SortExamplePerfect() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100100450300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5555));
        setContentPane(contentPane);
        setTitle("使用Sort方法对数组进行排序");
        contentPane.setLayout(null);
           
        JLabel label = new JLabel(
                "输入数组内容,空格为数组元素分隔符");
        label.setBounds(6626518);
        contentPane.add(label);
           
        arrayField = new JTextField();
        arrayField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
            }
               
            @Override
            public void keyTyped(KeyEvent e) {
                char key = e.getKeyChar();
                String mask = "0123456789 " + (char8;
                if (mask.indexOf(key) == -1) {
                    e.consume();
                }
            }
               
        });
        arrayField.setBounds(63642230);
        contentPane.add(arrayField);
        arrayField.setColumns(10);
           
        JButton button = new JButton("排序");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(164789030);
        contentPane.add(button);
           
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(6118422138);
        contentPane.add(scrollPane);
           
        sortArea = new JTextArea();
        sortArea.setLineWrap(true);
        scrollPane.setViewportView(sortArea);
    }
       
    protected void do_button_actionPerformed(ActionEvent e) {
        String text = arrayField.getText();// 获取用户输入
        text=text.trim();       //去除首尾空格
        if("".equals(text)){
            JOptionPane.showMessageDialog(null,"请输入要排序的数组内容");
            return;
        }
        String[] arrayStr = text.split(" {1,}");// 拆分输入为数组
        int[] array = new int[arrayStr.length];// 创建整数类型数组
        sortArea.setText("数组原有内容:\n");
        for (String string : arrayStr) {// 输出原有数组内容
            sortArea.append(string + "    ");
        }
        for (int i = 0; i < array.length; i++) {// 初始化整形数组
            array[i] = Integer.parseInt(arrayStr[i]);
        }
        sortArea.append("\n");
        Arrays.sort(array);// 使用sort方法对整形数组进行排序
        sortArea.append("数组排序后的内容:\n");
        for (int value : array) {// 输出排序后的数组内容
            sortArea.append(value + "    ");
        }
    }
       
    protected void do_arrayField_keyPressed(KeyEvent e) {
        char key = e.getKeyChar();// 获取用户按键字符
        String mask = "0123456789 " + (char8;// 定义规范化字符模板
        if (mask.indexOf(key) == -1) {// 判断按键字符是否属于规范化字符范围
            e.consume();// 取消非规范化字符的输入有效性
        }
    }
}

本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1304300,如需转载请自行联系原作者