从新线程调用时,Repaint()不起作用

时间:2015-04-13 09:57:31

标签: java multithreading swing

我想做一个排序动画。我有一个带有 GridLayout(1,3) JFrame ,因为我有3个排序算法。在每个单元格中,我有 JPanel BorderLayout()。在 JPanel 中心中,我有数字,每个数字都有不同宽度的标尺(我​​将它们水平放置)。对于每种类型的排序,我需要一个线程来调用它们。所以我创建了类 SortThread ,它进行了我的排序(它扩展了 JThread )。每对数字和小节都在中间面板上绘制,类 StickNumber ,扩展 JLabel 。问题是当我启动线程时。 repaint()方法似乎无法正常工作。

此类为 SortThread

public class SortThread extends Thread{
private JPanel sortPanel;
private StickNumber[] listOfNumbers;

public SortThread(JPanel sortPanel)
{
    this.sortPanel = sortPanel;
    Component[] components = sortPanel.getComponents();
    listOfNumbers = new StickNumber[components.length];
    for(int i = 0; i<components.length; i++)
        listOfNumbers[i] = (StickNumber) components[i];
}

public SortThread(){}

public void bubbleSort()
{
    boolean swapped = true;
    int j = 0;
    StickNumber tmp;

    while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < listOfNumbers.length - j; i++) {                                       
            if (listOfNumbers[i].getValue() > listOfNumbers[i+1].getValue()) {                          
                tmp = listOfNumbers[i];
                listOfNumbers[i]=listOfNumbers[i+1];
                listOfNumbers[i+1]=tmp;
                swapped = true;
                sortPanel.validate();
                sortPanel.repaint();
            }
        }                
    }
}
public void run(){
    bubbleSort();
}

这是我的按钮的监听器,它启动了线程:

ActionListener sortListener = new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            if(sortFlag == false)
            {
                sortFlag = true;
                SortThread thread1= new SortThread(centerPanel1);
                thread1.start();
                appFrame.validate();
                appFrame.repaint();
            }

        }
    };
    sortButton.addActionListener(sortListener);

StickNumber 类:

private void doDrawing(Graphics g) {  

    Graphics2D g2 = (Graphics2D) g;
    String number = Integer.toString(value);
    if(dimension!=0)
        setText(number);
    g2.setStroke(new BasicStroke(5));
    g2.draw(new Line2D.Float(dimension*10, 5, height+dimension*10, 5));

}

@Override
public void paintComponent(Graphics g) {

    super.paintComponent(g);
    doDrawing(g);
}

1 个答案:

答案 0 :(得分:0)

使用SwingWorker,假设您的代码完成了应有的操作,而您的问题是EDT被阻止了:

import java.awt.Component;
import java.util.List;
import javax.swing.*;

public class SortSwingWorker extends SwingWorker<Object, Object> {

    private JPanel sortPanel;
    private StickNumber[] listOfNumbers;

    public SortSwingWorker(JPanel sortPanel) {
        this.sortPanel = sortPanel;
        Component[] components = sortPanel.getComponents();
        listOfNumbers = new StickNumber[components.length];
        for (int i = 0; i < components.length; i++) {
            listOfNumbers[i] = (StickNumber) components[i];
        }
    }

    @Override
    protected Object doInBackground() throws Exception {
        boolean swapped = true;
        int j = 0;
        StickNumber tmp;

        while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < listOfNumbers.length - j; i++) {
                if (listOfNumbers[i].getValue() > listOfNumbers[i + 1].getValue()) {
                    tmp = listOfNumbers[i];
                    listOfNumbers[i] = listOfNumbers[i + 1];
                    listOfNumbers[i + 1] = tmp;
                    swapped = true;
                    publish(null);
                }
            }
        }
        return null;
    }

    @Override
    protected void process(List<Object> list) {
        sortPanel.validate();
        sortPanel.repaint();
    }

    //dummy class
    private class StickNumber extends Component {

        public Integer getValue() {
            return null;
        }
    }
}