有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java Swing removeAll()未删除

我正在制作一个排序可视化工具,这是我第一次使用JavaGUI,所以我有点挣扎

此时,一切正常,但每次arraylist更新时,我都想清除屏幕并再次创建矩形。repaint()方法正在工作,但是removeAll()没有

我试过使用validate()revalidate()updateUI(),但没有任何效果

GUI代码

public class Window extends JFrame{
    private ArrayList<Integer> nums;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 400;

    // Constructor
    public Window(ArrayList<Integer> nums){
        this.nums = nums;

        setSize(WIDTH,HEIGHT);
        setTitle("Sorting Visualizer");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        setVisible(true);
    }

    // Update method
    public void Draw(ArrayList<Integer> newNums){
        nums = newNums;
        removeAll();
        repaint();
    }

    // Paint method
    public void paint(Graphics g){
        //g.clearRect(0, 0, (int)getSize().getWidth(), (int)getSize().getHeight()); // shitty clear

        int width = WIDTH / nums.size();

        for(int i = 0; i < nums.size(); i++){
            int height = (HEIGHT / nums.size()) * (nums.get(i));

            g.setColor(Color.BLUE);
            g.drawRect(i * width, HEIGHT - height, width, height);
        }
    }
}

我正在创建窗口并在排序方法中调用Draw(),因此它将在每次for迭代中更新

排序类

public class Sort {
    // GUI instance
    Window w;

    // Constructor
    public Sort(ArrayList<Integer> nums){
        w = new Window(nums);
    }

    //Sorting algorithm
    public ArrayList<Integer> bubbleSort(ArrayList<Integer> nums){

        boolean isSorted = false;

        while(!isSorted){
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }

            for (int i = 0; i < nums.size() - 1; i++){
                if(nums.get(i) > nums.get(i + 1)){
                    int temp = nums.get(i);
                    nums.set(i, nums.get(i+1));
                    nums.set(i + 1, temp);
                }
                // Update GUI
                w.Draw(nums);
            }

            isSorted = isSorted(nums);
        }

        return nums;
    }

    // Private classes
    // Check if ArrayList is sorted
    private boolean isSorted(ArrayList<Integer> nums){
        for (int i = 0; i < nums.size() - 1; i++){
            if(nums.get(i) > nums.get(i + 1)){
                return false;
            }
        }

        return true;
    }
}

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    你能更好地解释什么“不起作用”吗我所做的只是取消对clearRect()的注释,它似乎工作正常

    我认为可能发生的是调用Tread.sleep()阻塞GUI线程。起初,我在GUI线程上运行代码,但在Thread.sleep()中,它大部分时间都被阻塞了。所以我在主线程上运行它,它工作了。要使两个线程正确通信,您需要做一些额外的工作,以便可以在GUI线程上运行窗口代码

    public class CustomPainting {
       
       public static void main( String[] args ) {
          ArrayList<Integer> list = new ArrayList( Arrays.asList( 4,3,2,1 ) );
          new Sort( list ).bubbleSort( list );
    //      SwingUtilities.invokeLater( CustomPainting::start );
       }
    
       private static void start() {
       }
    
    }
    
    class Window extends JFrame {
    
       private ArrayList<Integer> nums;
    
       private static final int WIDTH = 500;
       private static final int HEIGHT = 400;
    
       // Constructor
       public Window( ArrayList<Integer> nums ) {
          this.nums = nums;
    
          setSize( WIDTH, HEIGHT );
          setTitle( "Sorting Visualizer" );
          setDefaultCloseOperation( EXIT_ON_CLOSE );
    
          setVisible( true );
       }
    
       // Update method
       public void Draw( ArrayList<Integer> newNums ) {
          nums = newNums;
    //      removeAll();
          repaint();
       }
    
       // Paint method
       public void paint( Graphics g ) {
          g.clearRect(0, 0, (int)getSize().getWidth(), (int)getSize().getHeight()); // shitty clear
    
          int width = WIDTH / nums.size();
    
          for( int i = 0; i < nums.size(); i++ ) {
             int height = (HEIGHT / nums.size()) * (nums.get( i ));
    
             g.setColor( Color.BLUE );
             g.drawRect( i * width, HEIGHT - height, width, height );
          }
       }
    }
     class Sort {
        // GUI instance
        Window w;
    
        // Constructor
        public Sort(ArrayList<Integer> nums){
            w = new Window(nums);
        }
    
        //Sorting algorithm
        public ArrayList<Integer> bubbleSort(ArrayList<Integer> nums){
    
            boolean isSorted = false;
    
            while(!isSorted){
                try
                {
                    Thread.sleep(400);
                }
                catch(InterruptedException ex)
                {
                    Thread.currentThread().interrupt();
                }
    
                for (int i = 0; i < nums.size() - 1; i++){
                    if(nums.get(i) > nums.get(i + 1)){
                        int temp = nums.get(i);
                        nums.set(i, nums.get(i+1));
                        nums.set(i + 1, temp);
                    }
                    // Update GUI
                    w.Draw(nums);
                    System.err.println( nums );
                }
    
                isSorted = isSorted(nums);
            }
    
            return nums;
        }
    
        // Private classes
        // Check if ArrayList is sorted
        private boolean isSorted(ArrayList<Integer> nums){
            for (int i = 0; i < nums.size() - 1; i++){
                if(nums.get(i) > nums.get(i + 1)){
                    return false;
                }
            }
    
            return true;
        }
    }