2013-12-08 78 views
0

真的很感谢上一个问题的帮助,但仍然有一些错误。我正在制作一款寻宝游戏,用户点击一个gui来尝试揭示宝箱的位置。我正在使用一个动作监听器在按钮上显示一个藏宝箱的图像,如果这个位置被找到了,但是这是一个固定的位置,我想随机化这个。得到一些建议,使用按钮上的数组和随机数生成器,然后使用if/else来检查。有编译器的错误,我会评论下面的代码。一个好的编码器可能会在几秒钟内挑出我的新手错误!阵列和动作监听器java

import java.awt.*; 
    import javax.swing.*; 
    import java.util.Random; 
    import java.awt.event.ActionListener; 
    import java.awt.event.ActionEvent; 



    public class Test extends JFrame { 


    JLabel label1, label2, label3;  

    ImageIcon image1, image2, image3, image4, image5; 


JTextField textResult;  

    public static void main(String[] args) { 



    new Test(); 

    } 


    public Test(){ 

    this.setSize(700,700); 
    this.setLocationRelativeTo(null); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Treasure Hunt Game"); 

    JPanel thePanel = new JPanel(); 


    thePanel.setLayout(new GridLayout(0,3,0,0)); 

    image1 = new ImageIcon(getClass().getResource("Treasure.jpg")); 
    image2 = new ImageIcon(getClass().getResource("Pirate.jpg")); 
    image3 = new ImageIcon(getClass().getResource("sand2.jpg")); 
    image4 = new ImageIcon(getClass().getResource("emptyhole.jpg")); 
    image5 = new ImageIcon(getClass().getResource("map.jpg")); 

    label1 = new JLabel("Click the buttons to find the Treasure!"); 
    label2 = new JLabel(image5); 
    label3 = new JLabel(image2); 


    JButton [] buttons = new JButton[9]; 
    buttons[0] = new JButton(image3); 
    buttons[1] = new JButton(image3); 
    buttons[2] = new JButton(image3); 
    buttons[3] = new JButton(image3); 
    buttons[4] = new JButton(image3); 
    buttons[5] = new JButton(image3); 
    buttons[6] = new JButton(image3); 
    buttons[7] = new JButton(image3); 
    buttons[8] = new JButton(image3); 


    thePanel.add(buttons[0]); 
    thePanel.add(buttons[1]); 
    thePanel.add(buttons[2]); 
    thePanel.add(buttons[3]); 
    thePanel.add(buttons[4]); 
    thePanel.add(buttons[5]); 
    thePanel.add(buttons[6]); 
    thePanel.add(buttons[7]); 
    thePanel.add(buttons[8]); 
    thePanel.add(label1); 
    thePanel.add(label2); 
    thePanel.add(label3); 



    this.add(thePanel); 

    this.setVisible(true); 


    int treasureLocation = new Random().nextInt(buttons.length); 


    System.out.println(treasureLocation); 

在编译器,它给了我一个错误消息说,它不知道在下面的if else语句中的“按钮”或“treasureLocation”。

 } 
     public void actionPerformed(ActionEvent evt) { 
     if (evt.getSource() == buttons[treasureLocation]) { 
     } 

    else { 

    } 

    } 

} 
+0

是否打印语句? –

+0

请显示更多代码,至少'actionPerformed'的位置 –

回答

0
JLabel label1, label2, label3;  
ImageIcon image1, image2, image3, image4, image5; 
JTextField textResult; 
JButton [] buttons; // Move the declaration here 
int treasureLocation; // Move the declaration here 

和改变

JButton [] buttons = new JButton[9]; 

buttons = new JButton[9]; 

int treasureLocation = new Random().nextInt(buttons.length); 

treasureLocation = new Random().nextInt(buttons.length); 
+0

非常感谢Ajai的帮助。这似乎解决了这个问题。我遇到了一个你可能会提供帮助的新问题?我已经开始工作了,所以当点击if语句时,它会随机生成按钮上的胸部图片,这非常棒。然而,有什么想法,我可以改变其他按钮上的图片,所以当他们点击时,他们变成了一个空洞?我想过如果有其他说法,但不知道如何做到这一点。我会用它上面的代码提出一个新问题。 – Josh

0

实际上,你在你的构造,这实际上是not建议定义buttonstreasureLocation。因此,它们的生命周期和访问权仅限于此方法。在你的课堂上定义它们并初始化它们。

+0

非常感谢Ben的帮助。这似乎解决了这个问题。我遇到了一个你可能会提供帮助的新问题?我已经开始工作了,所以当点击if语句时,它会随机生成按钮上的胸部图片,这非常棒。然而,有什么想法,我可以改变其他按钮上的图片,所以当他们点击时,他们变成了一个空洞?我想过如果有其他说法,但不知道如何做到这一点。我会用它上面的代码提出一个新问题。 – Josh

+0

是的,为此创建一个新问题。如果我找到时间,我会查找它! – Ben

1

是否有可能你有一个不同的Listener类?

public class Test extends JFrame { 

    public Test(){ 
     Button[] buttons; 
     int treasureLocation; 
    } 

    private class ButtonListener implements ActionListener{ 
     public void actionPerformed(ActionEvent e) { 
      if (evt.getSource() == buttons[treasureLocation]) { 
     } 
    } 
} 

这将导致一个错误buttonstreasureLocation不在范围之内。如果不是这样,它对我来说似乎仍然是一个范围问题。尝试声明变量作为类成员

public class Test extends JFrame { 
    Button[] buttons; 
    int treasureLocation; 

    public Test(){ 

    } 
} 
+0

非常感谢您的帮助。这似乎解决了这个问题。我遇到了一个你可能会提供帮助的新问题?我已经开始工作了,所以当点击if语句时,它会随机生成按钮上的胸部图片,这非常棒。然而,有什么想法,我可以改变其他按钮上的图片,所以当他们点击时,他们变成了一个空洞?我想过如果有其他说法,但不知道如何做到这一点。我会用它上面的代码提出一个新问题。 – Josh

0

你需要有buttonstreasureLocation定义为属性的。所以不要在方法或构造函数定义它们,让他们在该行定义后

ImageIcon image1, image2, image3, image4, image5;