将更改更新到另一个类 [英] updating change to another class

查看:29
本文介绍了将更改更新到另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,我被困在这里...我想要做的是将一个 Java 文件中的数组列表的更改更新到另一个文件中的 JPanel.我正在对数组列表进行排序,因此无法手动完成.有没有一种方法可以告诉"JPanel 疼痛中发生了什么?

I'm new to java and I'm stuck here...What I want to do is to update the changes of an arraylist in one java file to JPanel in another file. I'm doing sorting to the arraylist so it can't be done manually. Is there a way I can "tell" the JPanel what's going on in the soring?

我有 BubbleSort.java 进行排序(有效),Animation.java 包含 JPanel 和 JFrame 类(用于显示未排序的直方图).

I have BubbleSort.java which does the sorting (works), Animation.java which contains the JPanel and JFrame class (works for displaying unsorted histograms).

这是家庭作业,所以我不想在这里发布我的代码.但如果我的描述不起作用,我会发布它.

It's homework so I prefer not to post my code here. But if my description doesn't work I'll post it.

谢谢!

更新:作业需要冒泡排序,所以我必须这样做.https://www.youtube.com/watch?v=wB7ovstyH4E这是它应该是什么.我只有前几秒未排序的黄色直方图和有效的排序方法.

Updating: Well the homework requires bubble sort so I have to. https://www.youtube.com/watch?v=wB7ovstyH4E Here is what it suppose to be. I only have the unsorted yellow histograms in the first few seconds and a working sorting method.

推荐答案

有什么方法可以告诉"JPanel 排序中发生了什么?

Is there a way I can "tell" the JPanel what's going on in the sorting?

有很多方法可以做到.最直接的方法是保留当前未排序列表的引用并在面板类中执行排序.每次从列表中交换 2 个元素时,调用 repaint() 重新绘制列表中元素的当前位置.

There are many ways to do it. The most direct way would be keeping a reference of the current unsorted list and perform the sorting in the panel class. Every time when 2 elements are swapped from the list, invoke repaint() to repaint the current positions of the elements in the list.

然而,更优雅的方法是使用观察者模式,即在DrawingPanel 和执行排序的类之间建立契约.

However, the more elegant way will be using Observer pattern by establishing a contract between the DrawingPanel and the class executing the sort.

DrawingPanel 可以实现一个 Observer 接口,而 SortAlgorightm 类实现一个 Observable 接口.

The DrawingPanel can implements an Observer interface, while the SortAlgorightm class implements an Observable interface.

您可以让 DrawingPanel排序类 中每次交换 2 个元素时得到通知.

You can let the DrawingPanel be notified everytime 2 elements are swapped in the sorting class.

在您的观察者模式中,您将拥有以下接口:

In your Observer Pattern, you will have the following interfaces:

public interface Observer
{
    public void update(ArrayList<Integer> list);    
}

public interface Observable
{
    public void register(Observer o);
    public void unregister(Observer o);
    public void notifyObservers();
}

在 GUI 和排序算法之间建立契约:

class DrawingPanel extends JPanel implements Observer{
    //Other attributes and initializations not shown
    @Override
    public void update(ArrayList<Integer> list){
        this.list = list;   //You can choose to receive element 
                            //indexs which got swapped instead (this is up to you)
        repaint();          //Repaint your current display when list is updated
    }

    //Register myself with the sorting algorithm class
    public void registerWith(Observable ob){
        if(ob != null)
            ob.register(this);
    }
}

在您的 SortingAlgorithm 类中,启用它以向所有已向此类注册的观察者发送更新:

In your SortingAlgorithm class, enable it to send updates to all observers which already registered itself with this class:

class SortingAlgorithm implements Observable{
    private ArrayList<Observer> observers;  //keep a list of observers for notifying
    //Other attributes and initializations not shown

    @Override
    public void register(Observer o){
        observers.add(o);       
    }

    @Override
    public void unregister(Observer o){
        observers.remove(o);
    }

    @Override
    public void notifyObservers(){
        for(Observer o : observers)
            o.update(list);  //Update all observers with your latest list updates
    }

    public void bubbleSort(){
        //Your sorting happens here..
        for(int i=0; i < n; i++){
            for(int j=1; j < (n-i); j++){
                if(intArray[j-1] > intArray[j]){
                    //swap the elements!
                    temp = intArray[j-1];
                    intArray[j-1] = intArray[j];
                    intArray[j] = temp;

                    //Notify GUI to update screen
                    notifyObservers();
                }    
            }
        }
    } 
}

有了上述内容,GUI 将在您想要的时候更新.在这种情况下,由于我们将 notifyObservers(observers); 放在 bubbleSort() 中,特别是在元素交换时,因此 GUI 只会在列表更改时更新.

With the above, the GUI will be updated when ever you want it to. In this case, since we placed notifyObservers(observers); in the bubbleSort(), particularly when elements are swapped, hence the GUI will only be updated when the list changes.

即使您不在 JPanel 上显示 GUI,而是在其他 contentPanes 上显示,也可以应用相同的逻辑.只需让类处理 UI 实现 Observer 并将其注册到 SortingClass.

Even if you are not displaying your GUI on JPanel but other contentPanes, the same logic can be applied. Just let the class handling UI to implement the Observer and register it to the SortingClass.

如果您只有 1 个 观察者,您甚至不需要保留 观察者 的列表.在我的示例中,您可以随时调整小细节.

If you have only 1 observer, you don't even need to keep a list of Observers. You can always tweak on the minor details in my example.

如果您不希望 GUI 在交换 2 个元素时更新,您可以随时将 notifyObservers(); 移动到您想要更新的另一个位置.

If you do not want the GUI to update when 2 elements are swapped, you can always move the notifyObservers(); to another location where you want an update.

这篇关于将更改更新到另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆