在程序中用了TIMER,但是运行时候发现没有实现定时的效果,目标是每PAUSE200重新交易一次,但是现在结果是停留在第一次,没有进行下去,DEBUG半天也看不出来问题,请大牛指点。import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;import javax.swing.JApplet;
import javax.swing.Timer;
public class Sim extends JApplet implements ActionListener {
private static final int POP = 10, SIZE = 500, WEALTH = 1000, PAUSE = 200;
private Person[] people;
private Random rng;
private Timer clock;
private Graphics graph;
private ActionListener taskPerformer;
public void init()
{
people = new Person[POP];
int share = WEALTH/POP;
for (int i = 0; i < POP; i++) {
people[i] = new Person(share);
} rng = new Random();
clock = new Timer(PAUSE, taskPerformer){
public void actionPerformed(ActionEvent evt) {
trade();
}
};
clock.start();
setSize(SIZE, SIZE);
}
public void actionPerformed(ActionEvent arg0) {
trade();
} public void trade() {
int one = rng.nextInt(POP);
int two = rng.nextInt(POP);
while (one == two) two = rng.nextInt(POP);
int maxAmount = Math.min(people[one].getWealth(), people[two].getWealth());
int amount = rng.nextInt(maxAmount);
people[one].take(amount);
people[two].give(amount);
paint(graph);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,SIZE,SIZE);
int x = 0;
int h;
int w = SIZE/POP;
g.setColor(Color.blue);
for (int i = 0; i < POP; i++) {
h = people[i].getWealth() * SIZE / WEALTH;
g.fillRect(x, SIZE-h, w, h);
x += w;
}
}
public Sim(){
init();
}
public static void main(String[] args) {
Sim testsim = new Sim();
}
}其中person class 是:public class Person {
private int wealth; // wealth in pennies (keep it discrete)
public Person(int wealth) {
this.wealth = wealth;
}
public int getWealth() {
return wealth;
}
public void setWealth(int newWealth) {
wealth = newWealth;
}
public void give(int amount) {
wealth -= amount;
}
public void take(int amount) {
wealth += amount;
}
}
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;import javax.swing.JApplet;
import javax.swing.Timer;
public class Sim extends JApplet implements ActionListener {
private static final int POP = 10, SIZE = 500, WEALTH = 1000, PAUSE = 200;
private Person[] people;
private Random rng;
private Timer clock;
private Graphics graph;
private ActionListener taskPerformer;
public void init()
{
people = new Person[POP];
int share = WEALTH/POP;
for (int i = 0; i < POP; i++) {
people[i] = new Person(share);
} rng = new Random();
clock = new Timer(PAUSE, taskPerformer){
public void actionPerformed(ActionEvent evt) {
trade();
}
};
clock.start();
setSize(SIZE, SIZE);
}
public void actionPerformed(ActionEvent arg0) {
trade();
} public void trade() {
int one = rng.nextInt(POP);
int two = rng.nextInt(POP);
while (one == two) two = rng.nextInt(POP);
int maxAmount = Math.min(people[one].getWealth(), people[two].getWealth());
int amount = rng.nextInt(maxAmount);
people[one].take(amount);
people[two].give(amount);
paint(graph);
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,SIZE,SIZE);
int x = 0;
int h;
int w = SIZE/POP;
g.setColor(Color.blue);
for (int i = 0; i < POP; i++) {
h = people[i].getWealth() * SIZE / WEALTH;
g.fillRect(x, SIZE-h, w, h);
x += w;
}
}
public Sim(){
init();
}
public static void main(String[] args) {
Sim testsim = new Sim();
}
}其中person class 是:public class Person {
private int wealth; // wealth in pennies (keep it discrete)
public Person(int wealth) {
this.wealth = wealth;
}
public int getWealth() {
return wealth;
}
public void setWealth(int newWealth) {
wealth = newWealth;
}
public void give(int amount) {
wealth -= amount;
}
public void take(int amount) {
wealth += amount;
}
}
的使用
使用 Timer 的软件包
javax.swing.plaf.basic 提供了根据基本外观构建的用户界面对象。
javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。
javax.swing.plaf.basic 中 Timer 的使用
声明为 Timer 的 javax.swing.plaf.basic 中的字段
protected Timer BasicComboPopup.autoscrollTimer
此受保护字段是特定于实现的。
protected Timer BasicSliderUI.scrollTimer
protected Timer BasicScrollBarUI.scrollTimer
protected Timer BasicTreeUI.ComponentHandler.timer
在滚动窗格中调整滚动条时使用的 Timer。
javax.swing.tree 中 Timer 的使用
声明为 Timer 的 javax.swing.tree 中的字段
protected Timer DefaultTreeCellEditor.timer
在启动编辑会话之前使用。
这个Timer是干吗用的?