Java Swing removeAll()未删除

Java Swing removeAll()未删除,java,swing,Java,Swing,我正在制作一个排序可视化工具,这是我第一次使用JavaGUI,所以我有点挣扎 此时,一切正常,但每次arraylist更新时,我都想清除屏幕并再次创建矩形。repaint()方法正在工作,但是removeAll()没有 我尝试了validate(),revalidate(),updateUI(),但没有任何效果 GUI代码 public class Window extends JFrame{ private ArrayList<Integer> nums; pri

我正在制作一个排序可视化工具,这是我第一次使用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);
        }
    }
}

谢谢你的帮助。

你能更好地解释什么“不起作用”吗?我所做的只是取消你的
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;
    }
}
公共类自定义绘制{
公共静态void main(字符串[]args){
ArrayList=新的ArrayList(Arrays.asList(4,3,2,1));
新排序(列表).bubbleSort(列表);
//调用器(CustomPaint::start);
}
私有静态void start(){
}
}
类窗口扩展JFrame{
私人ArrayList nums;
专用静态最终整数宽度=500;
专用静态最终内部高度=400;
//建造师
公共窗口(ArrayList nums){
this.nums=nums;
设置尺寸(宽度、高度);
setTitle(“排序可视化工具”);
setDefaultCloseOperation(关闭时退出);
setVisible(真);
}
//更新方法
公共作废提款(ArrayList newNums){
nums=newNums;
//removeAll();
重新油漆();
}
//油漆方法
公共空间涂料(图g){
g、 clearRect(0,0,(int)getSize().getWidth(),(int)getSize().getHeight());//该死的清除
int width=width/nums.size();
对于(int i=0;inums.get(i+1)){
int temp=nums.get(i);
nums.set(i,nums.get(i+1));
数值设置(i+1,温度);
}
//更新图形用户界面
w、 抽签(nums);
系统错误打印项次(nums);
}
isSorted=isSorted(nums);
}
返回nums;
}
//私人班级
//检查ArrayList是否已排序
私有布尔isSorted(ArrayList nums){
对于(int i=0;inums.get(i+1)){
返回false;
}
}
返回true;
}
}

您能更好地解释什么“不起作用”吗?我所做的只是取消您的
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;
    }
}
公共类自定义绘制{
公共静态void main(字符串[]args){
ArrayList=新的ArrayList(Arrays.asList(4,3,2,1));
新排序(列表).bubbleSort(列表);
//调用器(CustomPaint::start);
}
私有静态void start(){
}
}
类窗口扩展JFrame{
私人ArrayList nums;
专用静态最终整数宽度=500;
专用静态最终内部高度=400;
//建造师
公共窗口(ArrayList nums){
this.nums=nums;
设置尺寸(宽度、高度);
setTitle(“排序可视化工具”);
setDefaultCloseOperation(关闭时退出);
setVisible(真);
}
//更新方法
公共作废提款(ArrayList newNums){
nums=newNums;
//removeAll();
重新油漆();
}
//油漆方法
公共空间涂料(图g){
g、 clearRect(0,0,(int)getSize().getWidth(),(int)getSize().getHeight());//该死的清除
int width=width/nums.size();
对于(int i=0;inums.get(i+1)){
int temp=nums.get(i);
nums.set(i,nums.get(i+1));
数值设置(i+1,温度);