Java 我正在尝试实施和';撤消';JButton在一个简单的银行系统上,它将撤消最后执行的操作
我已经运行了其余的代码,但是我不确定在ActionListener中对Undo按钮做什么。我只有行动听众才能做到这一点Java 我正在尝试实施和';撤消';JButton在一个简单的银行系统上,它将撤消最后执行的操作,java,swing,user-interface,Java,Swing,User Interface,我已经运行了其余的代码,但是我不确定在ActionListener中对Undo按钮做什么。我只有行动听众才能做到这一点 private class UndoButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e) { } 我将在下面发布GUI类的其余部分以供参考,因为这不是一个任务(它是,只是不在GUI中),我感谢任何人能给我的建议 import j
private class UndoButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
我将在下面发布GUI类的其余部分以供参考,因为这不是一个任务(它是,只是不在GUI中),我感谢任何人能给我的建议
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class BankGUI extends JFrame {
private JLabel checking;
private JLabel saving;
private JButton depChecking;
private JButton depSaving;
private JButton witChecking;
private JButton withSaving;
private JButton transferToSaving;
private JButton transferToChecking;
private JButton undo;
private JTextField checkingAmt;
private JTextField savingAmt;
private JTextField depositChk;
private JTextField depositSav;
private JTextField withChk;
private JTextField withSav;
private JTextField transChk;
private JTextField transSav;
public BankGUI() {
super( "Bank Application" );
setLayout(new FlowLayout() );
checkingAmt = new JTextField(10);
checkingAmt.setEditable(false);
checkingAmt.setText("500.00");
add( checkingAmt );
savingAmt = new JTextField(10);
savingAmt.setEditable(false);
savingAmt.setText("0.00");
add(savingAmt);
checking = new JLabel( " Checking ");
add(checking);
saving = new JLabel( " Saving " );
add(saving);
depChecking = new JButton( " Deposit " );
add(depChecking);
depChecking.addActionListener(new DepChkButtonHandler());
depSaving = new JButton( " Deposit " );
add(depSaving);
depSaving.addActionListener(new DepSavButtonHandler());
depositChk = new JTextField(10);
depositChk.setEditable(true);
add(depositChk);
depositSav = new JTextField(10);
depositSav.setEditable(true);
add(depositSav);
witChecking = new JButton( " Withdraw " );
add(witChecking);
witChecking.addActionListener(new WitChkButtonHandler());
withSaving = new JButton( " Withdraw " );
add(withSaving);
withSaving.addActionListener(new WithSavButtonHandler());
withChk = new JTextField(10);
withChk.setEditable(true);
add(withChk);
withSav = new JTextField(10);
withSav.setEditable(true);
add(withSav);
transferToSaving = new JButton( " Transfer " );
add(transferToSaving);
transferToSaving.addActionListener(new TranstoSavButtonHandler());
transferToChecking = new JButton( " Transfer " );
add(transferToChecking);
transferToChecking.addActionListener(new TranstoChkButtonHandler());
transSav = new JTextField(10);
transSav.setEditable(true);
add(transSav);
transChk = new JTextField(10);
transChk.setEditable(true);
add(transChk);
undo = new JButton( " Undo " );
add(undo);
}
private class DepChkButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
DecimalFormat deciFormat = new DecimalFormat();
deciFormat.setMaximumFractionDigits(2);
double depAmt = Double.parseDouble(depositChk.getText());
double chkAmt = Double.parseDouble(checkingAmt.getText());
String newAmt = deciFormat.format(depAmt + chkAmt);
checkingAmt.setText(newAmt);
}
}
private class DepSavButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
DecimalFormat deciFormat = new DecimalFormat();
deciFormat.setMaximumFractionDigits(2);
double depAmt = Double.parseDouble(depositSav.getText());
double savAmt = Double.parseDouble(savingAmt.getText());
String newAmt = deciFormat.format(depAmt + savAmt);
savingAmt.setText(newAmt);
}
}
private class WitChkButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
DecimalFormat deciFormat = new DecimalFormat();
deciFormat.setMaximumFractionDigits(2);
double witAmt = Double.parseDouble(withChk.getText());
double chkAmt = Double.parseDouble(checkingAmt.getText());
String newAmt = deciFormat.format(chkAmt - witAmt);
checkingAmt.setText(newAmt);
}
}
private class WithSavButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
DecimalFormat deciFormat = new DecimalFormat();
deciFormat.setMaximumFractionDigits(2);
double witAmt = Double.parseDouble(withSav.getText());
double savAmt = Double.parseDouble(savingAmt.getText());
String newAmt = deciFormat.format(savAmt - witAmt);
savingAmt.setText(newAmt);
}
}
private class TranstoSavButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
DecimalFormat deciFormat = new DecimalFormat();
deciFormat.setMaximumFractionDigits(2);
double transAmt = Double.parseDouble(transSav.getText());
double savAmt = Double.parseDouble(savingAmt.getText());
double chkAmt = Double.parseDouble(checkingAmt.getText());
String newAmt1 = deciFormat.format(savAmt + transAmt);
String newAmt2 = deciFormat.format(chkAmt - transAmt);
savingAmt.setText(newAmt1);
checkingAmt.setText(newAmt2);
}
}
private class TranstoChkButtonHandler implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
DecimalFormat deciFormat = new DecimalFormat();
deciFormat.setMaximumFractionDigits(2);
double transAmt = Double.parseDouble(transChk.getText());
double savAmt = Double.parseDouble(savingAmt.getText());
double chkAmt = Double.parseDouble(checkingAmt.getText());
String newAmt1 = deciFormat.format(savAmt - transAmt);
String newAmt2 = deciFormat.format(chkAmt + transAmt);
savingAmt.setText(newAmt1);
checkingAmt.setText(newAmt2);
}
}
private class UndoButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
}您需要决定撤销的深度。用户可以撤消最后10个操作,还是仅撤消最近的操作?此决定将确定您是将最后一个操作存储为简单字段,还是存储为某种类型的
事务列表
(某种类型的队列
也可以)
您还应该将事务规范化为某种模型,该模型可以反映应用程序支持的每种事务的详细信息。换句话说,您需要一个交易
对象,其中包含金额
、源账户
、目的账户
等字段,或者以您想要的方式执行。关键是,该事务对象需要能够表示用户可能希望在程序中执行(或撤消)的所有操作
此交易对象将用于正常记账(从帐户中添加或删除资金),但在调整余额后,您将交易保存到lastTransaction
字段或交易
列表中
创建一个名为
undo
的方法,并将其放入undo处理程序中。当用户单击“撤消”按钮时,您的undo
方法只查看最近的事务并将其撤消。例如,如果交易是支票存款100美元,当您撤消它时,您将从支票中减去100美元,而不是向支票中添加100美元。如果您正在处理事务列表,请确保在撤消事务后将其删除。现在只需执行最后一个操作,这是一个好主意!我整天忙于不同的项目,精神上受到了严重的阻碍,但我想你给了我一个新的想法。我很感激。