本文介绍了在撤消的ArrayList的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经叫'行'行对象的ArrayList。我做了我自己的行类绘制一些约束线。它包括在面板中选择两个点和一个线绘制连接两点。每次创建一个线路,它被添加到'行'。线绘制在一个面板中。

I've an ArrayList of Line Objects called 'lines'. I made my own line class to draw lines with some constraints. It involves selecting two points in a panel and a line is drawn connecting the two points. Everytime a line is created, it is added to the 'lines'. The lines are drawn in a panel.

在我的面板涂料的功能如下:

The paint function in my panel looks like this:

   public void paintComponent(Graphics g){

       super.paintComponent(g);

       for(final Line r:lines){

            r.paint((Graphics2D)g);

       }
    }

和每次被点击面板上的两个点,一个新行被创建。

And everytime two points are clicked on the panel, a new line is created.

class Board extends JPanel{

 public void placeLine(){
  Point p1,p2;
  JLabel l1,l2;
  ...
  lines.add(new Line(p1,p2,l1,l2));
  this.repaint();
 }
 public void deleteLine(Line l){
  lines.remove(l);
 }
}

我要创造这个一个UndoableEdit中,每次我给撤消,撤消方法必须恢复到上次操作(i.e.creating线或删除线)。我试图撤消在JTextArea中事件,但我无法弄清楚如何建立一个自定义撤消在的ArrayList事件的变化。建议做一个这样的例子。

I want to create an UndoAbleEdit in this, and everytime i give undo, the undo method must revert to the last action(i.e.creating a line or deleting a line). I've tried undo for events in JTextArea but i couldn't figure out how to build a custom undo for event changes in ArrayLists. Suggest an example for doing this.

,我很抱歉没有张贴它作为一个SSCCE ..这是一个庞大的工程,这几乎是不可能创建一个SSCCE。

And i'm really sorry for not posting it as an SSCCE.. It is a huge project and it is almost impossible to create an SSCCE.

推荐答案

我要创建并存储Runnable对象作出撤消一些堆栈结构的变化,弹出并根据需要运行它们。
为了您的例子:

I would create and store Runnable objects for making undo changes in some stack structure, popping and running them as needed.For your example:

class Board extends JPanel {
    ArrayList lines = new ArrayList();
    Stack<Runnable> undo = new Stack<Runnable>();

    public void placeLine() {
        Point p1, p2;
        JLabel l1, l2;


        final Line line = new Line(p1, p2, l1, l2);
        lines.add(line);
        undo.push(new Runnable() {
            @Override
            public void run() {
                lines.remove(line);
                this.repaint();
            }
        });

        this.repaint();
    }

    public void deleteLine(final Line l) {
        lines.remove(l);
        undo.push(new Runnable() {
            @Override
            public void run() {
                lines.add(l);
            }
        });
    }


    public void undo() {
        undo.pop().run();
    }
}

这篇关于在撤消的ArrayList的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:33