为什么我写的这个升序降序,字符串不能进行升序降序排列

小辕点 2018-02-28 12:41:15
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import java.awt.event.*;
public class UserSort extends JFrame {
boolean swap = false;
private JPanel contentPane;
private String[] names = new String[]{"lzw", "anti", "zzk", "lyf",
"liuxin", "dongdayong", "gulili", "coolBoy", "newgirl", "myBoy",
"redsky", "pankaihua", "blueBoy"};
private JButton button;
private JButton button1;
private JList list;
/**
* Launch the application.
*/
public static void main(String[] args) {
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("\u7528\u6237\u6392\u5E8F");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 532, 404);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

list = new JList();
list.setBounds(14, 13, 323, 331);
list.setModel(new DefaultListModel(){
public int getSize(){
return names.length;
}
public Object getElementAt(int index){
return names[index];
}
});
contentPane.add(list);

JButton button = new JButton("\u5347\u5E8F");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_button_action(e);
}
});
button.setBounds(387, 10, 113, 27);
contentPane.add(button);

JButton button1 = new JButton("\u964D\u5E8F");
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_button_action(e);
}
});
button1.setBounds(387, 61, 113, 27);
contentPane.add(button1);

JButton button2 = new JButton("\u5173\u95ED");
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
do_button2_action(e);
}
});
button2.setBounds(387, 112, 113, 27);
contentPane.add(button2);
}
protected void do_button_action(final ActionEvent e){
new Thread() {
int[] indexs = new int[2];

public void run() {
if (swap)
return;
swap = true;
for (int i = names.length; --i >= 0;) {// 遍历数组
indexs[0] = i;
for (int j = 0; j < i; j++) {// 遍历并排序所有未排序元素
boolean compare = names[j]
.compareToIgnoreCase(names[j + 1]) > 0;
if (compare && e.getSource() == button || !compare
&& e.getSource() == button1) {// 条件判断
String temp = names[j];// 数组元素交换
names[j] = names[j + 1];
names[j + 1] = temp;
list.repaint();// 更新列表控件
}
try {
sleep(100);
} catch (InterruptedException e1) {
}
indexs[1] = j;
list.setSelectedIndices(indexs);
}
}
swap = false;
}
}.start();
}
protected void do_button2_action(ActionEvent e){
dispose();
}
}
...全文
432 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧