Java 每x秒执行一次代码
我正在创建一个GUI界面,用户在其中向文本字段输入一个nimber,然后将该数字作为参数添加到CurrentAccount的对象中。然后将该数字与随机值相加或相减。我希望这每5秒发生一次,在方程完成后取一个值,加上或减去一个值,直到用户告诉它停止。到目前为止,我已经创建了这段代码来决定是否应该添加或减去一个随机数,生成随机数,将其添加或减去到帐户余额中,然后将帐户余额打印到文本字段中Java 每x秒执行一次代码,java,multithreading,user-interface,timer,Java,Multithreading,User Interface,Timer,我正在创建一个GUI界面,用户在其中向文本字段输入一个nimber,然后将该数字作为参数添加到CurrentAccount的对象中。然后将该数字与随机值相加或相减。我希望这每5秒发生一次,在方程完成后取一个值,加上或减去一个值,直到用户告诉它停止。到目前为止,我已经创建了这段代码来决定是否应该添加或减去一个随机数,生成随机数,将其添加或减去到帐户余额中,然后将帐户余额打印到文本字段中 //method generates a random number to determine if a va
//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");
}
}
下面是java.util.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 = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, 10 * 1000);
第一个参数是要执行的任务
第二个参数表示第一次执行之前的延迟
第三个参数是以毫秒为单位的周期。这里:每10秒执行一次
请注意,如果您想使用此机制在GUI中执行操作,则需要使用SwingUtilities.invokeLater
编辑后补充:
要使用javax.swing.Timer,必须调用start使计时器运行。这里是java.util.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 = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 0, 10 * 1000);
第一个参数是要执行的任务
第二个参数表示第一次执行之前的延迟
第三个参数是以毫秒为单位的周期。这里:每10秒执行一次
请注意,如果您想使用此机制在GUI中执行操作,则需要使用SwingUtilities.invokeLater
编辑后补充:
要使用javax.swing.Timer,您必须调用start使计时器运行。运行定期任务的另一种方法是使用 那么您的代码可能如下所示:
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可能会取消您的任务
运行定期任务的另一种方法是使用 那么您的代码可能如下所示:
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可能会取消您的任务
看看java.swing.util.Timer。它可以定期为GUI线程安排任务。谢谢,伙计,我尝试了一个计时器,我将修改我的注释以显示我为它准备的代码,我的问题是它没有被调用,所以我显然没有将它指向正确的代码查看java.swing.util.timer。它可以定期为GUI线程安排任务。谢谢,伙计,我试了一个计时器,我会修改我的评论来显示我的代码,我的问题是它没有被调用,所以我显然没有把它指向正确的代码。谢谢你,它工作得很好:这是我现在的一个负担:非常感谢你,它工作得很好:这是我现在的一个负担: