我创建了一个GUI界面,用户将nimber输入文本字段,然后将该数字作为参数添加到CurrentAccount的对象中。然后将该数字加到或减去随机值。我希望每5秒发生一次,在方程完成后取值,然后加上或减去一个值,直到用户告诉它停止。到目前为止,我已创建此代码以确定是否应添加减去的随机数,生成随机数,将其添加或减去帐户余额,然后将帐户余额打印到文本字段。
//method generates a random number to determine if a value should be added or subtracted from the myBalance variable within theAccount classes and completes the equation
int month = 0;
private void simulate()
{
try
{
if(month<12)
{ //Creates instance of Random to decide if the random number should be added or subtracted to the balance
Random decider = new Random();
for (int i = 0; i < 1; i++)
{
int ans = decider.nextInt(2)+1;
//if the decider value == 1, subtract or "withdraw" random number from the balance
if (ans == 1)
{
//creates instance of Random to create random number
Random bal = new Random();
int withdrawAmount = bal.nextInt((500 - 100) + 1) + 100;
//sets account balance to the balance subtract the random number
theAccount.myBalance = theAccount.myBalance - withdrawAmount;
//increments the month timer
month++;
//prints the new balance to the transaction text field
jTextArea2.setText("The new balance is " + theAccount.myBalance );
} else
{
//if decider value == 2, add or "deposit" random number to the balance
//creates instance of Random to create random number
Random bal = new Random();
int depositAmount = bal.nextInt((500 - 100) +1) + 100;
//sets account balance to the balance plus the random number
theAccount.myBalance= theAccount.myBalance + depositAmount;
//increments the month timer
month++;
//prints the new balance to the transaction text field
jTextArea2.setText("The new balance is " + theAccount.myBalance );
}
//if the account has a balance of -£200, generate an error and reset the balance to the user inputted value
if (theAccount.myBalance < -200)
{
jTextArea1.setText("Overdraft of £200 only");
theAccount.myBalance = value;
}
//if the account has a balance of 0, generate an error as the account must always have at least £1 before entering the overdraft
if(theAccount.myBalance == 0)
{
jTextArea1.setText("An account must always have £1");
}
}
}
} catch (NullPointerException i)
{
jTextArea2.setText("Create an Account");
}
}
我尝试过使用thread.sleep(5000)方法,但必须将它放在错误的位置,因为它会在创建按钮上停留5秒并自行打印出余额。对此事的任何帮助都会很棒。此外,我有一个方法来检测用户输入,显然是按钮上的动作监听器来调用它和这个方法。
[修改评论] 我也尝试使用计时器来循环代码,但我似乎无法指出正确的代码重复,因为它除了填充内存之外什么都不做。
ActionListener timerListener = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
ControlPanel.this.simulate();
}
};
Timer theTimer = new Timer(5000,timerListener);
答案 0 :(得分:3)
运行定期任务的另一种方法可能是使用ScheduledExecutorService
然后你的代码看起来像这样:
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable yourRunnable = new Runnable() {
@Override
public void run() {
// Implement your Code here!
}
};
然后运行你的任务:
int initialDelay = 0;
int delay = 5;
scheduler.scheduleWithFixedDelay(yourRunnable, initialDelay, delay, TimeUnit.SECONDS);
您的任务可能会被调用方法shutdown()
scheduler.shutdown();
答案 1 :(得分:1)
以下是java.util.Timer
:
我们需要一个由Timer调用的任务:MyTimerTask
import java.util.Timer;
import java.util.TimerTask;
public class TimerTaskExample extends TimerTask {
@Override
public void run() {
// Implement your Code here!
}
}
然后我们必须安排一个Timer来执行这个任务:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, 10 * 1000);
第一个参数是要执行的任务。
第二个参数表示第一次执行前的延迟。
第三个参数是以毫秒为单位的句点。 (这里:每10秒执行一次!)
请注意,如果您想使用此机制在GUI中执行操作,则需要使用SwingUtilities.invokeLater()
编辑后的补充:
要使用javax.swing.Timer
,您必须致电start()
以使计时器运行。