简体   繁体   English

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

[英]Repaint() doesn't work when called from new thread

I want to do a sorting animation. 我想做一个排序动画。 I've got a JFrame with GridLayout(1,3) because I have 3 sorting alghorithms. 我有一个带有GridLayout(1,3)JFrame ,因为我有3种排序算法。 In each cell I have a JPanel with BorderLayout() . 在每个单元格中,我都有一个带有BorderLayout()JPanel In the center of the JPanel I have the numbers and for each number a stick with different widths(I placed them horizontally). JPanel中心 ,我有数字,每个数字都有一根宽度不同的棍子(我将它们水平放置)。 For each type of sorting I need a thread to call them. 对于每种类型的排序,我都需要一个线程来调用它们。 So I created class SortThread which does my sorting (it extends JThread ). 因此,我创建了进行排序的SortThread类(它扩展了JThread )。 Each pair of numbers and sticks are painted on the center panel with class StickNumber which extends JLabel . 每对数字和摇杆都在中心板上绘制了StickNumber类, 该类扩展了JLabel The problem is when I start the thread. 问题是当我启动线程时。 The repaint() method doesn't seem to work. repaint()方法似乎不起作用。

Here is class SortThread : 这是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();
}

Here is the listener for my button which starts the thread: 这是启动线程的我的按钮的侦听器:

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 class: 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);
}

Using SwingWorker, assuming your code does what it should and your problems is the EDT is blocked: 使用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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM