简体   繁体   English

将Object传递到扩展JFrame的类中

[英]Pass Object into class that extends JFrame

I need help with passing an Object from one class to another that extends JFrame, whenever that method is called. 每当调用该方法时,我都需要将一个对象从一个类传递到另一个扩展JFrame的帮助。 So far this is what I have. 到目前为止,这就是我所拥有的。 I need to pass the added circle to the NodeTracker class and make sure it displays the added object. 我需要将添加的圆圈传递给NodeTracker类,并确保它显示添加的对象。

public class Nodes
{
   ArrayList<Circle> circles;
   ArrayList<Squares> squares;

   public Node()
   {
       circles = new ArrayList<Circles>();
       squares = new ArrayList<Squares>();
   }

   public void addCircle(Circle c)
   {
       circles.add(c);
       // Here I want to implement a method that allows me to pass "c"
       // to the NodeTracker
   }

}

// All I have so far that opens a blank table.
public class NodeTracker extends JFrame 
{

    private JPanel topPanel;
    private JTable table;
    private JScrollPane scrollPane;

    public NodeTracker()
    {
        setTitle("Node Tracker");
        setSize(700, 700);

        topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);

        String columns[] = { "Color", "Radius" };

        String data[][] =
        {
            //whenever added, would update
            //{circle.color, circle.radius},
            //{circle.color, circle.radius}
        };

        table = new JTable(data, columns);

        scrollPane = new JScrollPane(table);
        topPanel.add(scrollPane, BorderLayout.CENTER);
    }

    //Temporary
    public static void main( String args[] )
    {
        NodeTracker mainFrame = new NodeTracker();
        mainFrame.setVisible( true );
    }
}

Thank you so much. 非常感谢。

// Here I want to implement a method that allows me to pass "c" to the NodeTracker" //这里我想实现一个方法,该方法允许我将“ c”传递给NodeTracker“

No, you don't. 不,你没有。 You want to implement an Observer Pattern . 您要实现观察者模式 Nodes shouldn't care who is listening for changes, only that when addCircle is called, it will tell them. Nodes不必在乎谁在监听更改,而只是在addCircle时会告诉他们。

This way NodeTracker would register an interest in Nodes to be told when a circle is added and would then be able to respond to in some meaningful fashion 这样, NodeTracker将在Nodes上注册感兴趣的对象,以便在添加圆圈时被告知,然后能够以某种有意义的方式进行响应

This is a pretty key concept in Swing and is implemented through it's "listener" API 这是Swing中一个非常关键的概念,并通过其“监听器” API实现

Start by defining a interface which describes the events that the Nodes class will generate 首先定义一个interface ,该interface描述Nodes类将生成的事件

public interface NodesListener {
    public void nodesCircleWasAdded(Nodes source, Circle circle);
}

Next, add support for the new listener into Nodes , allowing other classes to register/deregister interest and for actually triggering the events 接下来,将对新侦听器的支持添加到Nodes ,允许其他类注册/注销兴趣并实际触发事件

public class Nodes {

    ArrayList<Circle> circles;
    ArrayList<Squares> squares;
    private List<NodesListener> listeners;

    public Nodes() {
        circles = new ArrayList<Circles>();
        squares = new ArrayList<Squares>();
        listeners = new ArrayList<>(25);
    }

    public void addNodesListener(NodesListener listener) {
        listeners.add(listener);
    }

    public void addCircle(Circle c) {
        circles.add(c);
        for (NodesListener listener : listeners) {
            listener.nodesCircleWasAdded(this, c);
        }
    }

}

Finally, create an instance of Nodes or pass a pre-existing instance to the class and register your interest in getting notifications from it... 最后,创建一个Nodes实例或将一个预先存在的实例传递给该类,并注册您从该类获取通知的兴趣...

public class NodeTracker extends JFrame {

    private JPanel topPanel;
    private JTable table;
    private JScrollPane scrollPane;
    private Nodes nodes;

    public NodeTracker() {
        setTitle("Node Tracker");
        setSize(700, 700);

        nodes = new Nodes();
        nodes.addNodesListener(new NodesListener() {
            @Override
            public void nodesCircleWasAdded(Nodes source, Circle circle) {
                // A circle was added
            }
        });

        topPanel = new JPanel();
        topPanel.setLayout(new BorderLayout());
        getContentPane().add(topPanel);

        String columns[] = {"Color", "Radius"};

        String data[][]
                        = { //whenever added, would update
                        //{circle.color, circle.radius},
                        //{circle.color, circle.radius}
                        };

        table = new JTable(data, columns);

        scrollPane = new JScrollPane(table);
        topPanel.add(scrollPane, BorderLayout.CENTER);
    }

    //Temporary
    public static void main(String args[]) {
        NodeTracker mainFrame = new NodeTracker();
        mainFrame.setVisible(true);
    }
}

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

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