2009-11-27 61 views
0

我知道我在这里缺少一些简单的东西。除了移动我的ArrayList外,我已经完成了所有的作业。这是一个计算器的撤销功能,我需要从ArrayList中取出和移除一个对象。下面是方法:用撤销按钮移动Java ArrayList

public void actionPerformed(ActionEvent e) 
    { 
     Status state; 
     state = new Status(operand1, operator, operand2, displayBox.getText()); 
     //ArrayList that I am coping into 
     listOfStates.add(state); 
     super.actionPerformed(e); 


     if(e.getSource() == undo) 
     { 
      Status previousState = (Status) listOfStates.get(listOfStates.size()- 1); 
      displayBox.setText(" "); 
       displayBox.setText(displayBox.getText() + previousState.op1); 
//This is where I need help at? This calls a method of Status that only returns op1 IE 
//first operator 

      } 
     } 

全班同学正在这里

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.util.*; 
/** 
* 
* 
* 
*/ 
public class BetterCalculator extends Calculator 

{ 
    //attributes 
    protected JButton undo; 
    protected ArrayList<Status> listOfStates; 
    private int numClicks = 0; 





    public BetterCalculator() 
    { 
     super(); 
     listOfStates = new ArrayList<Status>(); 

    } 


    public void createUserInterface3() 
    { 
     createUserInterface2(); 
     undo = new JButton("undo"); 
     jPanel.add(undo); 
     undo.setBackground(Color.red); 
     undo.setToolTipText("This is the undo feature"); 
     undo.addActionListener(this); 

    } 


    public void actionPerformed(ActionEvent e) 
    { 
     Status state; 
     state = new Status(operand1, operator, operand2, displayBox.getText()); 
     //ArrayList that I am coping into 
     listOfStates.add(state); 
     super.actionPerformed(e); 


     if(e.getSource() == undo) 
     { 
      Status previousState = (Status) listOfStates.get(listOfStates.size()- 1); 
      displayBox.setText(" "); 
       displayBox.setText(displayBox.getText() + previousState.op1); 

      } 
     } 


    public static void main(String[] args) 
    { 

     BetterCalculator myCalc; 
     myCalc = new BetterCalculator(); 
     myCalc.createUserInterface3(); 



    } 
} 

状态类

import java.util.*; 
import java.awt.event.*; 
import java.awt.*; 
/** 
* Write a description of class Status here. 
* 
* 
* This is a class to get the status for the undo feature 
*/ 
public class Status 
{ 
    //attributes 
    protected double op1; 
    protected char opt; 
    protected double op2; 
    protected String soFar; 
    //constructors 



    public Status(double o1, char op, double o2, String sf) 
    { 
     op1 = o1; 
     opt = op; 
     op2 = o2; 
     soFar = sf; 
    } 



    //Methods 
    public double getOp1() 
    { 
     return op1; 
    } 


    public char getOpt() 
    { 
     return opt; 
    } 


    public double getOp2() 
    { 
     return op2; 
    } 






} 

感谢您的帮助。我知道我错过了如何将对象拉出ArrayList然后将其删除。

+0

首先“整体”类不编译 – OscarRyz 2009-11-27 00:55:04

+0

我想我把太多的代码抱歉。 “整个类是具有我遇到问题的方法的整个类,动作actionPerformed方法,主要是if语句,整个类不是类名,类名是BetterCalculator。 – 2009-11-27 01:47:27

回答

1

如果您希望能够将对象从某个集合中取出并将其删除,则可能需要考虑使用QueueDeque来代替,取决于您希望从哪个端删除该对象。