该死的毕设,要thread.sleep在bB实现动画效果,bB单独运行的时候好好的,可是在主界面里鼠标点击事件调用就会卡住,请问怎么解决啊?
//主界面类
package MySort;import java.awt.Color;
import javax.swing.*;
public class mainFrame extends JFrame{ public mainFrame() {
initComponents();
}
private void initComponents() {
bB = new JButton();
setSize(1000,700);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
add(bB);
bB.setBounds(800, 20, 150, 25);
bB.setText("冒泡排序");
bB.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bBMouseClicked(evt);
}
}); private void bBMouseClicked(java.awt.event.MouseEvent evt) {
bDemo demo = new bDemo();
demo.start();
demo.setVisible(true);
demo.bubbleSort();
}
public static void main(String args[]) {
mainFrame myFrame = new mainFrame();
myFrame.setVisible(true);
myFrame.setLocationRelativeTo(null);
}
//bB类
private JButton bB;
}package MySort;import javax.swing.*;
import java.awt.*;public class bDemo extends JFrame {
public void start() {
speed = new JSlider();
setSize(1000,700);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
add(speed);
speed.setBounds(350, 600, 300, 25);
speed.setPaintLabels(true);
speed.setToolTipText("演示速度"); for (int i=0;i<count;++i) {
array[i]=(int) (Math.random()*90+10);
numPane[i] = new JTextPane();
add(numPane[i]);
numPane[i].setText(""+array[i]);
numPane[i].setVisible(true);
numPane[i].setEditable(false);
numPane[i].setBackground(new Color(51, 153, 255));
numPane[i].setBounds(500-count*10+i*20, 100, 20, array[i]*4);
numPane[i].setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
}
}
public void bubbleSort() {
for (int j=count-1;j>0;--j) {
for ( int i=0 ;i<j;++i) {
numPane[i].setBackground(Color.red);
numPane[i+1].setBackground(Color.red);
try {
Thread.sleep(speed.getValue()*10+10);
}
catch(Exception e){}
if (numPane[i].getHeight()>numPane[i+1].getHeight()) swap(i,i+1);
numPane[i].setBackground(new Color(51, 153, 255));
numPane[i+1].setBackground(new Color(51, 153, 255));
}
}
}
private void swap(int num1,int num2) {
Dimension tSize=null;
tSize=numPane[num1].getSize();
numPane[num1].setSize(numPane[num2].getSize());
numPane[num1].setText(""+numPane[num2].getHeight()/4);
numPane[num2].setSize(tSize);
numPane[num2].setText(""+tSize.height/4);
}
//public static void main(String args[]) {
// bDemo demo =new bDemo();
// demo.setLocationRelativeTo(null);
// demo.setVisible(true);
// demo.bubbleSort();
// }
private JSlider speed;
private JTextPane[] numPane = new JTextPane[40];
private int count = 40;
private int[] array = new int[count];
}
//主界面类
package MySort;import java.awt.Color;
import javax.swing.*;
public class mainFrame extends JFrame{ public mainFrame() {
initComponents();
}
private void initComponents() {
bB = new JButton();
setSize(1000,700);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
add(bB);
bB.setBounds(800, 20, 150, 25);
bB.setText("冒泡排序");
bB.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
bBMouseClicked(evt);
}
}); private void bBMouseClicked(java.awt.event.MouseEvent evt) {
bDemo demo = new bDemo();
demo.start();
demo.setVisible(true);
demo.bubbleSort();
}
public static void main(String args[]) {
mainFrame myFrame = new mainFrame();
myFrame.setVisible(true);
myFrame.setLocationRelativeTo(null);
}
//bB类
private JButton bB;
}package MySort;import javax.swing.*;
import java.awt.*;public class bDemo extends JFrame {
public void start() {
speed = new JSlider();
setSize(1000,700);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
add(speed);
speed.setBounds(350, 600, 300, 25);
speed.setPaintLabels(true);
speed.setToolTipText("演示速度"); for (int i=0;i<count;++i) {
array[i]=(int) (Math.random()*90+10);
numPane[i] = new JTextPane();
add(numPane[i]);
numPane[i].setText(""+array[i]);
numPane[i].setVisible(true);
numPane[i].setEditable(false);
numPane[i].setBackground(new Color(51, 153, 255));
numPane[i].setBounds(500-count*10+i*20, 100, 20, array[i]*4);
numPane[i].setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
}
}
public void bubbleSort() {
for (int j=count-1;j>0;--j) {
for ( int i=0 ;i<j;++i) {
numPane[i].setBackground(Color.red);
numPane[i+1].setBackground(Color.red);
try {
Thread.sleep(speed.getValue()*10+10);
}
catch(Exception e){}
if (numPane[i].getHeight()>numPane[i+1].getHeight()) swap(i,i+1);
numPane[i].setBackground(new Color(51, 153, 255));
numPane[i+1].setBackground(new Color(51, 153, 255));
}
}
}
private void swap(int num1,int num2) {
Dimension tSize=null;
tSize=numPane[num1].getSize();
numPane[num1].setSize(numPane[num2].getSize());
numPane[num1].setText(""+numPane[num2].getHeight()/4);
numPane[num2].setSize(tSize);
numPane[num2].setText(""+tSize.height/4);
}
//public static void main(String args[]) {
// bDemo demo =new bDemo();
// demo.setLocationRelativeTo(null);
// demo.setVisible(true);
// demo.bubbleSort();
// }
private JSlider speed;
private JTextPane[] numPane = new JTextPane[40];
private int count = 40;
private int[] array = new int[count];
}
下面是修改后的代码.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.*;public class mainFrame extends JFrame { public mainFrame() {
initComponents();
} private void initComponents() { bB = new JButton(); setSize(1000, 700);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(bB);
bB.setBounds(800, 20, 150, 25);
bB.setText("冒泡排序");
bB.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Thread(){
public void run(){
bDemo demo = new bDemo();
demo.bubbleSort();
}
}.start();
}
});
} public static void main(String args[]) {
mainFrame myFrame = new mainFrame();
myFrame.setVisible(true);
myFrame.setLocationRelativeTo(null);
}
//bB类
private JButton bB;
}import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;public class bDemo extends JFrame { public void start() { speed = new JSlider(); setSize(1000, 700);
setResizable(false);
setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); add(speed);
speed.setBounds(350, 600, 300, 25);
speed.setPaintLabels(true);
speed.setToolTipText("演示速度"); for (int i = 0; i < count; ++i) {
array[i] = (int) (Math.random() * 90 + 10);
numPane[i] = new JTextPane();
add(numPane[i]);
numPane[i].setText("" + array[i]);
numPane[i].setVisible(true);
numPane[i].setEditable(false);
numPane[i].setBackground(new Color(51, 153, 255));
numPane[i].setBounds(500 - count * 10 + i * 20, 100, 20, array[i] * 4);
numPane[i].setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
}
setLocationRelativeTo(null);
setVisible(true);
} public void bubbleSort() {
for (int j = count - 1; j > 0; --j) {
for (int i = 0; i < j; ++i) {
numPane[i].setBackground(Color.red);
numPane[i + 1].setBackground(Color.red);
try {
Thread.sleep(speed.getValue() * 10 + 10);
} catch (Exception e) {
}
if (numPane[i].getHeight() > numPane[i + 1].getHeight()) {
swap(i, i + 1);
}
numPane[i].setBackground(new Color(51, 153, 255));
numPane[i + 1].setBackground(new Color(51, 153, 255));
}
}
} private void swap(int num1, int num2) {
Dimension tSize = null;
tSize = numPane[num1].getSize();
numPane[num1].setSize(numPane[num2].getSize());
numPane[num1].setText("" + numPane[num2].getHeight() / 4);
numPane[num2].setSize(tSize);
numPane[num2].setText("" + tSize.height / 4);
}
public static void main(String args[]) {
bDemo demo = new bDemo();
// demo.bubbleSort();
}
public bDemo(){
numPane = new JTextPane[40];
count = 40;
array = new int[count];
start();
}
private JSlider speed;
private JTextPane[] numPane;
private int count;
private int[] array;
}