回复 2# 的帖子
代码有啊package paixuyanshixitong;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class paixuyanshixitong extends JFrame{
private JLabel[]n;
private JTextField in;
private JTextField choice;
private JLabel select;
private JButton maopao;
private JButton kuaisu;
private JButton dui;
public paixuyanshixitong(){
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500,300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setAlwaysOnTop(true);
JLabel l = new JLabel("输入10组内数字:");
select=new JLabel("选择一种排序方法");
maopao=new JButton("2冒泡排序");
kuaisu=new JButton("1快速排序");
dui=new JButton("3堆排序");
l.setBounds(10,18,120,20);
add(l);
in=new JTextField("10,212,7,456,33,2,55,6,50,97");
in.setBounds(110,18,250,20);
add(in);
select.setBounds(20,130,150,30);
add(select);
maopao.setBounds(90,200,150,30);
add(maopao);
kuaisu.setBounds(90,160,150,30);
add(kuaisu);
dui.setBounds(90,240,150,30);
add(dui);
choice=new JTextField();
setTitle("排序演示");
kuaisu.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e){sort1();}});
n=new JLabel[10];
for(int i=0; i<n.length; i++){
n[i]=new JLabel();
n[i].setFont(new Font("",Font.BOLD,14));
n[i].setForeground(Color.gray);
n[i].setHorizontalAlignment(JLabel.CENTER);
add(n[i]);
}
}
private void sort1(){
try{
String src = in.getText();
src=src.replaceAll("[^0-9,]","");
String[] sp = src.split(",");
final int[] v = new int[sp.length];
int left=0,w=40,offx=0,offy=95;
for(int i=0; i<n.length;i++)n[i].setText(null);
for(int i=0; i<sp.length; i++){
n[i].setText(sp[i]);
v[i] = Integer.valueOf(sp[i]);
n[i].setSize(w,20);
n[i].setLocation((left+=w)+offx,offy);
}
new Thread(){
public void run(){
kuaisu.setEnabled(false);
s(500);
try{
for(int i=0; i<n.length-1; i++){
if(v[i]>v[i+1]){
JLabel l=n[i];
n[i]=n[i+1];
n[i+1]=l;
n[i].setForeground(Color.blue);
for(int k=0; k<7; k++){
n[i].setVisible(k%2==0);
s(123);
}
swap(n[i],n[i+1]);
n[i].setForeground(Color.gray);
v[i]+=v[i+1];
v[i+1]=v[i]-v[i+1];
v[i]-=v[i+1];
i-=i==0?1:2;
}
}
}catch(Exception e){}
kuaisu.setEnabled(true);
}
private void sort2() {
throw new UnsupportedOperationException("Not yet implemented");
}
private void swap(JLabel a, JLabel b) {
JLabel t = a;
a=b;
b=t;
Point pa=a.getLocation();
Point pb=b.getLocation();
int x1,x2,y1,y2;
x1=pa.x;
x2=pb.x;
y1=pa.y;
y2=pb.y;
int delay=10;
while(x1<(x1+x2)/2){
a.setLocation(++x1,y1++);
b.setLocation(--x2,y2--);
s(delay);
}
while(x1<pb.x){
a.setLocation(++x1,y1--);
b.setLocation(--x2,y2++);
s(delay);
}
a.setLocation(pb);
b.setLocation(pa);
}
private void s(int i) {
try {
sleep(i);
} catch (Exception e) {}
}
}.start();
}catch(Exception e){e.printStackTrace();setTitle("请检查输入的数据,只能输入10组哦");}
}
public static void main(String[] args) {
new paixuyanshixitong().setVisible(true);
}
}
大哥你能不能帮我把这个程序补全啊!非常感谢!!