使用撤消按钮在 Java ArrayList 中移动
我知道我在这里缺少一些简单的东西。除了移动 ArrayList 之外,我已经完成了所有作业。这是计算器的撤消功能,我需要从 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();
}
}
Status Class
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 中取出然后将其删除。
I know that I am missing something simple here. I have got this homework all done except for moving through my ArrayList. This is a undo feature of a calculator that I need to pull and remove a object from an ArrayList. Here is the method:
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
}
}
The whole Class is here
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();
}
}
Status Class
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;
}
}
Thanks for any help. I know I am missing it on how to pull the object out of the ArrayList and then remove it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要的是从集合中取出对象并将其删除的功能,您可能需要考虑使用 队列 或 Deque 相反,取决于您想要从哪一端删除对象的需要。
If what you want is capability to take object out of a collection and remove it, you might want to consider to use Queue or Deque instead, depend on your need from which end you want to remove the object.