资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
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.DefaultListModel; 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 private 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 static 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.printStackTrace(););/* 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 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 scrollPane = 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();sourceList.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_panel = 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 void 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 JButton(“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();
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号