Implementation: enter an array and sort it.
Calculation Code:
Protected void do_button_actionreceivmed (ActionEvent e) {String text = arrayField. getText (); // get user input text = text. trim (); // remove the first and last spaces if ("". equals (text) {JOptionPane. showMessageDialog (null, "Enter the content of the array to be sorted"); return;} String [] arrayStr = text. split ("{1,}"); // The split input is an array int [] array = new int [arrayStr. length]; // create an integer array sortArea. setText ("original array content: \ n"); for (String string: arrayStr) {// output the original array content sortArea. append (string + "") ;}for (int I = 0; I <array. length; I ++) {// initialize the Integer array [I] = Integer. parseInt (arrayStr [I]);} sortArea. append ("\ n"); Arrays. sort (array); // use the sort method to sort the integer array sortArea. append ("array sorted content: \ n"); for (int value: array) {// output the sorted array content sortArea. append (value + "") ;}} protected void do_arrayField_keyPressed (KeyEvent e) {char key = e. getKeyChar (); // obtain the String mask = "0123456789" + (char) 8; // define the canonicalized character template if (mask. indexOf (key) =-1) {// determines whether the key character is within the canonicalized character range e. consume (); // cancel the input validity of nonstandard characters }}}
:
650) this. width = 650; "src =" http://img1.51cto.com/attachment/201310/163028158.jpg "title =" %3@'8%i3_hb53ajq%6segie.jpg "alt =" 163028158.jpg"/>
Complete code:
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 = bytes; 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 () {setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,450,300); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5, 5); setContentPane (contentPane); setTitle ("Sort Arrays Using Sort"); contentPane. setLayout (null); JLabel label = new JLabel ("input array content, space is the array element separator"); label. setBounds (6, 6,265, 18); 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" + (char) 8; if (mask. indexOf (key) =-1) {e. consume () ;}}); arrayField. setBounds (6, 36,422, 30); contentPane. add (arrayField); arrayField. setColumns (10); JButton button = new JButton ("sort"); button. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {do_button_actionreceivmed (e) ;}}); button. setBounds (164, 78, 90, 30); contentPane. add (button); JScrollPane scrollPane = new JScrollPane (); scrollPane. setBounds (6,118,422,138); contentPane. add (scrollPane); sortArea = new JTextArea (); sortArea. setLineWrap (true); scrollPane. setViewportView (sortArea);} protected void do_button_actionreceivmed (ActionEvent e) {String text = arrayField. getText (); // get user input text = text. trim (); // remove the first and last spaces if ("". equals (text) {JOptionPane. showMessageDialog (null, "Enter the content of the array to be sorted"); return;} String [] arrayStr = text. split ("{1,}"); // The split input is an array int [] array = new int [arrayStr. length]; // create an integer array sortArea. setText ("original array content: \ n"); for (String string: arrayStr) {// output the original array content sortArea. append (string + "") ;}for (int I = 0; I <array. length; I ++) {// initialize the Integer array [I] = Integer. parseInt (arrayStr [I]);} sortArea. append ("\ n"); Arrays. sort (array); // use the sort method to sort the integer array sortArea. append ("array sorted content: \ n"); for (int value: array) {// output the sorted array content sortArea. append (value + "") ;}} protected void do_arrayField_keyPressed (KeyEvent e) {char key = e. getKeyChar (); // obtain the String mask = "0123456789" + (char) 8; // define the canonicalized character template if (mask. indexOf (key) =-1) {// determines whether the key character is within the canonicalized character range e. consume (); // cancel the input validity of nonstandard characters }}}
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1304300