Implementation: automatically generate random numbers and sort them;
Basic Idea: The smallest or largest element in the first order of the elements to be sorted is placed at the end of the sorted order until all the data elements to be sorted are arranged.
DESIGN: Main Interface Design-button part-textField-computing part
:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/143245O92-0.jpg "title =" 7c47aof4l0000'tl_nj(7wqaq2.jpg "alt =" 010721912.jpg"/>
Important computing code:
Int index; for (int I = 1; I <array. length; I ++) {index = 0; for (int j = 1; j <= array. length-I; j ++) {if (array [j]> array [index]) {index = j; // returns the maximum value} // returns the array in the position. length-I and index (maximum) two numbers int temp = array [array. length-I]; array [array. length-I] = array [index]; array [index] = temp;
Complete code:
Package com. lixiyu; import java. awt. eventQueue; import java. awt. gridBagConstraints; import java. awt. gridBagLayout; import java. awt. insets; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. util. random; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. JScrollPane; import javax. swing. JTextArea; import javax. swing. border. emptyBorder; public class SelectSort extends JFrame {/***/private static final long serialVersionUID = 6824542513659403529l; // serialization maintains object compatibility with private JPanel contentPane; // container/*** Launch the application. */public static void main (String [] args) {EventQueue. invokeLater (new Runnable () {// call public void run () {try {SelectSort frame = new SelectSort (); frame. setVisible (true);} catch (Exception e) {e. printStackTrace () ;}});}/*** Create the frame. */public SelectSort () {setTitle ("sort the array by selection"); setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); setBounds (100,100,450,300); contentPane = new JPanel (); contentPane. setBorder (new EmptyBorder (5, 5, 5); setContentPane (contentPane); GridBagLayout gbl_contentPane = new GridBagLayout (); iterator = new int [] {0, 0 }; gbl_contentPane.rowHeights = new int [] {0, 0, 0, 0}; gbl_contentPane.columnWeights = new double [] {1.0, Double. MIN_VALUE}; gbl_contentPane.rowWeights = new double [] {1.0, 0.0, 1.0, 0.0, Double. MIN_VALUE}; contentPane. setLayout (rows); JScrollPane scrollPane = new JScrollPane (); GridBagConstraints gbc_scrollPane = new rows (); counts = new Insets (0, 0, 5, 0); counts = GridBagConstraints. BOTH; gbc_scrollPane.gridx = 0; gbc_scrollPane.gridy = 0; contentPane. add (scrollPane, gbc_scrollPane); textArea1 = new JTextArea (); scrollPane. setViewportView (textArea1); JButton button = new JButton ("generate random number"); button. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {response (e) ;}}); GridBagConstraints gbc_button = new GridBagConstraints (); gbc_button.insets = new Insets (0, 0, 5, 0); gbc_button.gridx = 0; gbc_button.gridy = 1; contentPane. add (button, gbc_button); JScrollPane scrollPane_1 = new JScrollPane (); GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints (); counts = new Insets (0, 0, 5, 0 ); gbc_scrollPane_1.fill = GridBagConstraints. BOTH; gbc_scrollPane_1.gridx = 0; gbc_scrollPane_1.gridy = 2; contentPane. add (scrollPane_1, gbc_scrollPane_1); textArea2 = new JTextArea (); Combine (textArea2); JButton button_1 = new JButton ("sort"); button_1.addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {response (e) ;}}); GridBagConstraints gbc_button_1 = new GridBagConstraints (); response = 0; gbc_button_1.gridy = 3; contentPane. add (button_1, gbc_button_1);} private int [] array = new int [10]; private JTextArea textArea1; private JTextArea textArea2; protected void do_button_actionreceivmed (ActionEvent e) {Random random = new Random (); // creates a Random number object textArea1.setText (""); // clears the text field for (int I = 0; I <array. length; I ++) {// initialize the array element array [I] = random. nextInt (50); // generate a random number of less than 50 textArea1.append (array [I] + ""); // In the text field control displayed by the array element} protected void do_button_?action=med (ActionEvent e) {textArea2.setText (""); // clear the text field int index; for (int I = 1; I <array. length; I ++) {index = 0; for (int j = 1; j <= array. length-I; j ++) {if (array [j]> array [index]) {index = j; // returns the maximum value} // returns the array in the position. length-I and index (maximum) two numbers int temp = array [array. length-I]; array [array. length-I] = array [index]; array [index] = temp;} for (int I = 0; I <array. length; I ++) {textArea2.append (array [I] + ""); // display the sorted array elements in the text field }}}
It involves a grid layout manager GridBagLayout. For more information about its usage, see the API documentation. After you have a better understanding of this usage, you should write an article separately to write its usage.
Official explanation of the term:
The GridBagLayout class is a flexible layout manager that aligns components vertically, horizontally or along their baseline without requiring that the components be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area.
This article is from the "ToBeContinued" blog, please be sure to keep this source http://leexy.blog.51cto.com/4136883/1304109