2013-12-08 77 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;