该计划旨在运行一个5秒计时器,在每5秒结束时,程序要么存入或取出一定数量的钱,但由于某种原因,这不会运行!我们还必须添加一个启动和停止模拟按钮。我该怎么办?谢谢你的帮助
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bank;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
/**
*
* @author B00640241
*/
public class MainMenu2 extends javax.swing.JFrame {
JTextArea textOpen;
double BalanceCheck;
private Object intialDeposit;
private final
/**
* Creates new form MainMenu2
*/
public MainMenu2() {
initComponents();
setLocationRelativeTo(null);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated
// Code">//GEN-BEGIN:initComponents
private void initComponents() {
currentAccount = new javax.swing.JButton();
savingAccount = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
transactionHistoryA = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Main Menu");
currentAccount.setText("Current Account ");
currentAccount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
currentAccountActionPerformed(evt);
}
});
savingAccount.setText("Savings Account ");
savingAccount.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
savingAccountActionPerformed(evt);
}
});
jLabel1.setText("Transaction History ");
jLabel2.setText("Errors");
transactionHistoryA.setColumns(20);
transactionHistoryA.setRows(5);
jScrollPane1.setViewportView(transactionHistoryA);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(currentAccount, javax.swing.GroupLayout.DEFAULT_SIZE, 140,
Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(savingAccount, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 115,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(currentAccount).addComponent(savingAccount))
.addGap(3, 3, 3)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
.addComponent(jScrollPane2)).addContainerGap()));
pack();
}// </editor-fold>//GEN-END:initComponents
private void savingAccountActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_savingAccountActionPerformed
BankAccount SC = new SavingsAccount();
savingAccount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainMenu2.this.IB();
}
});
}// GEN-LAST:event_savingAccountActionPerformed
private void currentAccountActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_currentAccountActionPerformed
currentAccount.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BankAccount CA = new CurrentAccount();
MainMenu2.this.IB();
String IB;
transactionHistoryA.append("Opening Balance £" + IB + ".\n");
Timer timer = new Timer();
setVisible(true);
TimerTask myTask;
myTask = new TimerTask() {
int balance = Integer.parseInt(IB);
@Override
public void run() {
Random num = new Random();
int dW = num.nextInt(2);
int randomNumber = num.nextInt(500);
dW = dW * 2;
dW = dW - 1;
if (((balance + dW * randomNumber)) < -200) {
int avail = balance + 200;
transactionHistoryA.append(
"Deposit/ Withdraw : " + dW * randomNumber + " Operation can't be completed.\n");
transactionHistoryA.append(" Insufficient Funds.\n");
transactionHistoryA.append("Amount that can be withdrawn : £" + avail + ".\n");
} else {
balance = balance + (dW * randomNumber);
transactionHistoryA.append("Desposit/Withdraw : " + dW * randomNumber);
transactionHistoryA.append(".\n" + "Account Balance : " + balance + " .\n");
transactionHistoryA.append(".\n");
}
if (dW * randomNumber > 500) {
balance = balance + 10;
}
}
};
}
});
}// GEN-LAST:event_currentAccountActionPerformed
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed" desc=" Look and feel setting
// code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.
* html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainMenu2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainMenu2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainMenu2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainMenu2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainMenu2().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton currentAccount;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jTextArea2;
private javax.swing.JButton savingAccount;
private javax.swing.JTextArea transactionHistoryA;
// End of variables declaration//GEN-END:variables
public void IB() {
BankAccount CA = new CurrentAccount();
BalanceCheck = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Initial Balance"));
CA.balance = BalanceCheck;
transactionHistoryA.append("" + CA.balance + ".\n");
}
}