我想知道如何让我的按钮切换到这个班级,我从另一个班级打电话。但它进入并且仅对按钮进行了一次更改,而且它就是它......
package code;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
public class Something implements ActionListener {
private Game G;
private int random;
private JButton a;
private JButton b;
private JButton c;
private Font i;
public Something(Game g, int rand, JButton d, JButton e, JButton f, Font h) {
G = g;
random = rand;
a = d;
b = e;
c = f;
i = h;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
G.increment();
if (random == 0) {
a.setText("A");
a.setEnabled(true);
b.setEnabled(false);
c.setEnabled(false);
SuperSize(a);
SmallerSize(b);
SmallerSize(c);
random = RandomNum();
;
} else if (random == 1) {
b.setText("B");
a.setEnabled(false);
b.setEnabled(true);
c.setEnabled(false);
SuperSize(b);
SmallerSize(a);
SmallerSize(c);
random = RandomNum();
} else if (random == 2) {
c.setText("C");
a.setEnabled(false);
b.setEnabled(false);
c.setEnabled(true);
SuperSize(c);
SmallerSize(a);
SmallerSize(b);
random = RandomNum();
}
}
public int RandomNum() {
Random r = new Random();
int rand = 0;
rand = r.nextInt(3);
return rand;
}
public void SuperSize(JButton a) {
Font myFont = i.deriveFont(Font.BOLD, i.getSize() * 4);
a.setFont(myFont);
}
public void SmallerSize(JButton a) {
a.setFont(i);
}
}
}
我不知道该怎么做,你们可以帮助我吗?
答案 0 :(得分:1)
每次调用Random
时都不要创建RandomNum
的新实例,而是在构造函数中创建一个实例并继续重复使用它。
public class Something implements ActionListener {
//...
private Random rnd;
public Something(Game g, int rand, JButton d, JButton e, JButton f, Font h) {
//...
rnd = new Random();
}
public int RandomNum() {
return rnd.nextInt(3);
}