基本上,每次我点击我的一个 JButton 时,它都会锁定。我一直在环顾四周,发现可能是因为无限循环,但我在任何地方都看不到。
一双新鲜的眼睛会派上用场!
无论如何,这里是被声明的 JButtons:
public static JButton textYes = new JButton("Yes");
public static JButton textNo = new JButton("No");
这是我的 main() 方法:
public static void main(String[] args) throws IOException {
Greed gui = new Greed();
gui.launchFrame();
redirectSystemStreams();
Container contentPane = f.getContentPane();
contentPane.add(new Greed());
Scanner is = new Scanner(System.in);
System.out.println("Welcome to Greed...");
//do {
System.out.println("Would you like to play? (yes/no)");
area = "menu";
menu = is.next();
}
下面是 Start() 方法:
public static void start(String menu) {
switch (menu) {
case "yes":
jTextArea1.setText(null);
diceOne = 0;
diceTwo = 0;
diceThree = 0;
diceFour = 0;
diceFive = 0;
System.out.println("Rolling...");
Game();
break;
case "no":
System.out.println("Goodbye...");
System.exit(0);
break;
default:
invalidInput();
break;
}
}
这是带有 JButton 侦听器的 actionPerformed() 方法:
public void actionPerformed(ActionEvent e) {
//jTextArea1.setText(null);
if (box1.isSelected()) {
System.out.println("1 is selected");
willRerollDiceOne = true;
}
else {
//System.out.println("1 not selected");
willRerollDiceOne = false;
}
if (box2.isSelected()) {
System.out.println("2 is selected");
willRerollDiceTwo = true;
}
else {
//System.out.println("2 not selected");
willRerollDiceTwo = false;
}
if (box3.isSelected()) {
System.out.println("3 is selected");
willRerollDiceThree = true;
}
else {
//System.out.println("3 not selected");
willRerollDiceThree = false;
}
if (box4.isSelected()) {
System.out.println("4 is selected");
willRerollDiceFour = true;
}
else {
//System.out.println("4 not selected");
willRerollDiceFour = false;
}
if (box5.isSelected()) {
System.out.println("5 is selected");
willRerollDiceFive = true;
}
else {
//System.out.println("5 not selected");
willRerollDiceFive = false;
}
if ("menu".equals(area)) {
if(e.getSource() == textYes){
start("yes");
}
if(e.getSource() == textNo){
start("no");
}
}
if ("choiceReroll".equals(area)) {
if(e.getSource() == textYes){
choiceReroll = "yes";
}
if(e.getSource() == textNo){
choiceReroll = "no";
}
}
}
我认为它以某种方式连接到 JButtons。
让我知道是否需要显示更多代码。
无论如何,任何和所有的帮助表示赞赏!
感谢您的帮助和时间!
编辑:对不起,我忘了显示连接到 JBUttons 的听众:
textYes.addActionListener(this);
textNo.addActionListener(this);
编辑:另外,这里是 Game() 方法:
public static void Game() {
rollDiceOne();
rollDiceTwo();
rollDiceThree();
rollDiceFour();
rollDiceFive();
displayDiceValues();
f.validate();
f.repaint();
choiceRerollDice();
}
还有 rollDice# 方法:
public static void rollDiceOne() {
diceOne = 1 + (int)(Math.random()*6);
}
public static void rollDiceTwo() {
diceTwo = 1 + (int)(Math.random()*6);
}
public static void rollDiceThree() {
diceThree = 1 + (int)(Math.random()*6);
}
public static void rollDiceFour() {
diceFour = 1 + (int)(Math.random()*6);
}
public static void rollDiceFive() {
diceFive = 1 + (int)(Math.random()*6);
}