我已经完成了我的任务,这是为了模拟(使用计时器)通过两种不同类型的帐户(当前或储蓄)发生的一系列随机交易。我有程序工作,除了有一件小事我试图纠正,但无法开始工作。
我有一个JLabel,它显示了模拟开始时随机尝试的事务的输出。唯一的问题是,当我选择其他帐户时,例如当前而不是储蓄,并开始模拟,其他帐户交易数据仍显示在其他帐户随机交易发生时。
我想要的是,当我模拟当前帐户,并且JLabel已填充了交易数据时,当我点击储蓄帐户并开始模拟时,将清除当前帐户中的交易数据,以便只存储新的储蓄交易。
我试过使用tran.setText(“”);清除数据但旧的交易数据仍然存在。我还在代码的另一个版本上创建了一个按钮,并将按钮编码为tran.setText(“”);单击按钮时,它确实清除了数据,但是当我开始模拟其他帐户时,旧数据再次出现。
我想也许在计时器代码中可能需要添加或更改某些内容?
感谢人们可能有的任何想法。
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JOptionPane;
//import javax.swing.Timer;
import java.util.Timer;
import java.util.TimerTask;
/*
* 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.
*/
/**
*
* @author wafa
*/
public class Container extends javax.swing.JFrame {
/**
* Creates new form Container
*/
float total_balance = 0;
String transaction = "";
float thisMonth = 0;
float savingWithdrawChance = 2;
int depositBonus = 1;
int isSimulating = 0;
int iscreated = 0;
Timer timer ;//= new Timer();
public Container() {
initComponents();
isCreated.setVisible(false);
withdrawButton.setVisible(false);
depositButton.setVisible(false);
dispMonth.setVisible(false);
dispYear.setVisible(false);
chances.setVisible(false);
depositField.setVisible(false);
withdrawField.setVisible(false);
// total.setText(Float.toString(total_balance)+" £");
}
/**
* 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">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jComboBox1 = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
initial = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
create = new javax.swing.JButton();
start = new javax.swing.JButton();
stop = new javax.swing.JButton();
tran = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
total = new javax.swing.JLabel();
isCreated = new javax.swing.JLabel();
withdrawField = new javax.swing.JTextField();
depositField = new javax.swing.JTextField();
withdrawButton = new javax.swing.JButton();
depositButton = new javax.swing.JButton();
dispMonth = new javax.swing.JLabel();
dispYear = new javax.swing.JLabel();
chances = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Current", "Saving" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jLabel1.setText("AccountType");
initial.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
initialActionPerformed(evt);
}
});
jLabel2.setText("Initial Balance");
create.setText("Create Account");
create.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createActionPerformed(evt);
}
});
start.setText("Start Simulating");
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
stop.setText("Stop Simulating");
stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopActionPerformed(evt);
}
});
tran.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
tran.setText("Transactions");
tran.setVerticalAlignment(javax.swing.SwingConstants.TOP);
tran.setIconTextGap(20);
tran.setVerticalTextPosition(javax.swing.SwingConstants.TOP);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 344, Short.MAX_VALUE)
.addComponent(tran, javax.swing.GroupLayout.PREFERRED_SIZE, 355, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(22, 22, 22))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(jLabel1))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(initial, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(create))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(stop, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(60, 60, 60)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(initial)
.addComponent(create))
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(57, 57, 57)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(start, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(stop, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
.addComponent(tran, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jLabel3.setText("Total Balance");
total.setForeground(new java.awt.Color(51, 51, 255));
total.setText(" ");
isCreated.setText("Account Created");
withdrawButton.setText("Withdraw");
withdrawButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
withdrawButtonActionPerformed(evt);
}
});
depositButton.setText("Deposit");
depositButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
depositButtonActionPerformed(evt);
}
});
dispMonth.setText("Months");
dispYear.setText("Year");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(35, 35, 35)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(80, 80, 80)
.addComponent(total, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(11, 11, 11)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(dispYear)
.addComponent(dispMonth)
.addComponent(tran2, javax.swing.GroupLayout.PREFERRED_SIZE, 341, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(120, 120, 120)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(isCreated)
.addComponent(depositField, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
.addComponent(withdrawField))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(withdrawButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(depositButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(79, 79, 79))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(chances, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(97, 97, 97))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(isCreated)
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(total))
.addGap(34, 34, 34)
.addComponent(chances, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(withdrawField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(withdrawButton))
.addGap(25, 25, 25)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(depositField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(depositButton))
.addGap(39, 39, 39)
.addComponent(dispMonth)
.addGap(18, 18, 18)
.addComponent(dispYear)
.addGap(18, 18, 18)
.addComponent(tran2, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addContainerGap())
);
total.getAccessibleContext().setAccessibleName("total_bal");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 330, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void initialActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
float months = 0;
float year = 0;
Random ran = new Random();
int isWhat = 0;
int amount = 0;
int overDrawn = 0;
private void startActionPerformed(java.awt.event.ActionEvent evt) {
if(iscreated == 1)
{
timer = new Timer();
// withdrawButton.setVisible(true);
// depositButton.setVisible(true);
isSimulating = 1;
try{
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
dispMonth.setVisible(true);
dispYear.setVisible(true);
months++;
amount = ran.nextInt((int)total_balance+ran.nextInt(300));
isWhat = ran.nextInt(2);
System.out.println(amount+"-----"+isWhat);
if(isWhat == 1)
{
Object selectedItem = jComboBox1.getSelectedItem();
if(selectedItem.toString().matches("Current") && iscreated==1){
thisMonth += amount ;
if(thisMonth>=500 && depositBonus == 1)
{
if(iscreated==1){
depositBonus = 0;
total_balance = total_balance +10+ amount;
}
}
else
{
if(iscreated==1){
total_balance = total_balance + amount;
}
}
}
else
{
if(iscreated==1)
total_balance = total_balance + amount;
}
if(iscreated==1){
total.setText(Float.toString(total_balance)+" £");
transaction += "Deposit of " + amount+" £ ";
tran.setText("<html>" + ""+transaction+"" + "<br><br></html>");
}
}
else
{
Object selectedItem = jComboBox1.getSelectedItem();
if(selectedItem.toString().matches("Current") && iscreated==1){
if(true)
{
float entered = amount;
if( (total_balance+200)>=entered)
{
if(iscreated==1){
total_balance -= amount;
}
}
else
{ if(iscreated==1){
JOptionPane.showMessageDialog(create, "Overdraft exceeded");
overDrawn = 1;
}
}
if(iscreated==1){
total.setText(Float.toString(total_balance)+" £");
}
}
}
else
{
float entered = amount;
if( (total_balance-entered)>=100 && savingWithdrawChance>0)
{
if(iscreated==1){
total_balance -= amount;
savingWithdrawChance--;
chances.setText("Withdraw chances :"+Float.toString(savingWithdrawChance));
}
}
else
{
if(iscreated==1){
JOptionPane.showMessageDialog(create, "Withdraw could not occur, or you have exceed withdrawal chances");
overDrawn= 1;
}
}
if(iscreated==1){
total.setText(Float.toString(total_balance)+" £");
}
}
if(iscreated==1){
if(overDrawn==1)
{
overDrawn = 0;
}
else{
transaction += "Wtihdrawal of " + amount +" £ ";
tran.setText("<html>"+transaction+"<br><br></html>");
withdrawButton.setVisible(false);
depositButton.setVisible(false);
}
}
}
dispMonth.setText("Months "+Float.toString(months));
dispYear.setText("Years "+Float.toString(year));
//System.out.println(months);
if(months==12)
{
// JOptionPane.showMessageDialog(create, "Withdraw or deposit");
depositBonus = 1;
months=0;
thisMonth = 0;
year++;
savingWithdrawChance= 2;
if(total_balance>0)
{
Object selectedItem = jComboBox1.getSelectedItem();
if(selectedItem.toString().matches("Saving"))
total_balance = (float) (total_balance + (total_balance* 0.03));
transaction += "Annual profit of " + (total_balance* 0.03) +" £ Added";
tran.setText("<html>"+transaction+"<br><br></html>");
total.setText(Float.toString(total_balance)+" £");
}
}
}
}, 5*1000, 5*1000);
create.setVisible(false);
initial.setVisible(false);
}
catch(Exception e)
{
System.out.println("please restart applicaion");
}
}
else
{
JOptionPane.showMessageDialog(create, "Create Account First");
}
}
private void stopActionPerformed(java.awt.event.ActionEvent evt) {
isSimulating = 0;
create.setVisible(true);
initial.setVisible(true);
months = 0;
year = 0;
timer.cancel();
timer.purge();
withdrawButton.setVisible(false);
depositButton.setVisible(false);
}
private void createActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Object selectedItem = jComboBox1.getSelectedItem();
iscreated = 1;
if(selectedItem.toString().matches("Current"))
{
iscreated = 1;
//System.out.prfloatln("---");
if(!initial.getText().toString().matches(""))
{
float init = Float.parseFloat(initial.getText().toString());
if(init>=1)
{
total_balance = init;
isCreated.setText("Account created type : "+selectedItem.toString());
isCreated.setVisible(true);
total.setText(initial.getText().toString()+" £");
// withdrawButton.setVisible(true);
// depositButton.setVisible(true);
initial.setText("");
thisMonth = total_balance;
}
else
{
JOptionPane.showMessageDialog(this, "Initial Balance must be at least £1");
}
}
}
if(selectedItem.toString().matches("Saving"))
{
//System.out.prfloatln("---");
if(initial.getText().toString()!="")
{
float init = Float.parseFloat(initial.getText().toString());
if(init>=100)
{
total_balance = init;
isCreated.setText("Account created type : "+selectedItem.toString());
isCreated.setVisible(true);
total.setText(initial.getText().toString()+" £");
// withdrawButton.setVisible(true);
//depositButton.setVisible(true);
initial.setText("");
thisMonth = total_balance;
chances.setVisible(true);
chances.setText("Withdraw Chances :"+Float.toString(savingWithdrawChance));
}
else
{
JOptionPane.showMessageDialog(this, "Inital Balance must be at least £100");
}
}
}
}
private void depositButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
private void withdrawButtonActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @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 (floatroduced 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(Container.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Container.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Container().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel chances;
private javax.swing.JButton create;
private javax.swing.JButton depositButton;
private javax.swing.JTextField depositField;
private javax.swing.JLabel dispMonth;
private javax.swing.JLabel dispYear;
private javax.swing.JTextField initial;
private javax.swing.JLabel isCreated;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JButton start;
private javax.swing.JButton stop;
private javax.swing.JLabel total;
private javax.swing.JLabel tran;
private javax.swing.JLabel tran2;
private javax.swing.JButton withdrawButton;
private javax.swing.JTextField withdrawField;
// End of variables declaration
}
答案 0 :(得分:1)
您可以尝试使用:
tran.setText(null);
我认为这应该有用。