2009-11-27 92 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來代替,取決於您希望從哪個端刪除該對象。