《Java字符串数组排序》由会员分享,可在线阅读,更多相关《Java字符串数组排序(5页珍藏版)》请在金锄头文库上搜索。
1、Java 字符串数组排序字符串数组排序运行效果图: 初始无序:升序后:降序后:源码:源码: 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 javax.swing.BoxLayout; import javax.swing.DefaultListMod
2、el; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; import java.awt.Color;public class UserSort extends JFrame p
3、rivate JPanel contentPane;private JList sourceList;private String names = new String “lzw“, “anti“, “zzk“, “lyf“,“liuxin“, “dongdayong“, “gulili“, “coolBoy“, “newgirl“, “myBoy“,“redsky“, “pankaihua“, “blueBoy“ ;private JButton descButton;private JButton ascButton;/* Launch the application.*/public s
4、tatic void main(String args) try UIManager.setLookAndFeel(“com.sun.java.swing.plaf.nimbus.NimbusLoo kAndFeel“); catch (Throwable e) e.printStackTrace();EventQueue.invokeLater(new Runnable() public void run() try UserSort frame = new UserSort();frame.setVisible(true); catch (Exception e) e.printStack
5、Trace(););/* Create the frame.*/public UserSort() setTitle(“u7528u6237u6392u5E8F“);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 340, 313);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);setContentPane(contentPane);GridBagLayout gbl_contentPane = new
6、 GridBagLayout();gbl_contentPane.columnWidths = new int 0, 0, 0 ;gbl_contentPane.rowHeights = new int 0, 0 ;gbl_contentPane.columnWeights = new double 1.0, 0.0,Double.MIN_VALUE ;gbl_contentPane.rowWeights = new double 1.0, Double.MIN_VALUE ;contentPane.setLayout(gbl_contentPane);JScrollPane scrollPa
7、ne = new JScrollPane();GridBagConstraints gbc_scrollPane = new GridBagConstraints();gbc_scrollPane.insets = new Insets(0, 0, 0, 5);gbc_scrollPane.fill = GridBagConstraints.BOTH;gbc_scrollPane.gridx = 0;gbc_scrollPane.gridy = 0;contentPane.add(scrollPane, gbc_scrollPane);sourceList = new JList();sour
8、ceList.setFixedCellHeight(18);sourceList.setSelectionBackground(new Color(135, 206, 250);sourceList.setListData(names);scrollPane.setViewportView(sourceList);JLabel label = new JLabel(“u7528u6237u5217u8868“);scrollPane.setColumnHeaderView(label);JPanel panel = new JPanel();GridBagConstraints gbc_pan
9、el = new GridBagConstraints();gbc_panel.fill = GridBagConstraints.VERTICAL;gbc_panel.gridx = 1;gbc_panel.gridy = 0;contentPane.add(panel, gbc_panel);panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS);ascButton = new JButton(“u5347u5E8F“);ascButton.addActionListener(new ActionListener() public vo
10、id actionPerformed(ActionEvent e) do_button_actionPerformed(e););panel.add(ascButton);descButton = new JButton(“u964Du5E8F“);descButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) do_button_actionPerformed(e););panel.add(descButton);JButton closeButton = new JBu
11、tton(“u5173u95ED“);closeButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) do_closeButton_actionPerformed(e););panel.add(closeButton);protected void do_button_actionPerformed(final ActionEvent e) for (int i = names.length; -i = 0;) / 遍历数组for (int j = 0; j 0;if (compare / 数组元素交换namesj = namesj + 1;namesj + 1 = temp;sourceList.setListData(names); / 把数组设置到JList空间中protected void do_closeButton_actionPerformed(ActionEvent e) dispose();