【发布时间】:2013-06-20 19:45:47
【问题描述】:
基本上,每次我点击我的一个 JButton 时,它都会锁定。我环顾四周,发现可能是因为无限循环,但我在任何地方都看不到。
一双新鲜的眼睛会派上用场!
无论如何,这里是声明的 JButton:
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";
}
}
}
我认为它以某种方式连接到 JButton。
如果我需要显示更多代码,请告诉我。
无论如何,我们都非常感谢您的帮助!
感谢您的帮助和时间!
编辑:很抱歉,我忘了显示连接到 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);
}
【问题讨论】:
-
Game()是做什么的?看起来您在 UI 线程中进行处理,这使 UI 无法更新。 -
游戏运行几种不同的方法。我可以很快发布 Game() 方法。
-
我们还需要所有的 rollDice# 方法。
-
当然,我也会把它们放上来。
-
尽管他们所做的只是掷骰子
标签: java swing user-interface jbutton infinite-loop