2014-01-17 116 views
0

我有一個西蒙比賽,我正在努力,我已經調試了一會兒,然後繼續完成。到目前爲止,這只是一個錯誤;編譯器(NetBeans IDE寧願)報告錯誤;特別是「無法找到符號MenuPanel;類:BorderPanel」。我知道這不屬於這個班級,但我不完全確定如何指向它。相關代碼如下:Java-無法找到符號(揮杆)

package simon; 

import java.util.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.Color.*; 


/** 
* Main Class 
* @author Chris Mailloux 
*/ 
public class Simon 
{ 
    // Variable declarations. 
    public static final String[] colors = {"Green", "Red", "Yellow", "Blue"}; 
    public static ArrayList<String> pattern = new ArrayList<String>(); 
    public static int currentScore = 0; 
    public static int iterator = 0; 
    public static boolean isPlayerTurn = false; 
    // PLACEHOLDER FOR HIGHSCORE RETRIEVAL FROM FILE 
    public static int highScore = 0; // TEMPORARY 

    public static void main (String[] args) 
    { 
     GameWindow window = new GameWindow(); 
     window.setVisible(true); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

/** 
* Class for main game window. 
* @author Chris 
*/ 
class GameWindow extends JFrame 
{ 
    GameWindow() 
    { 
     // Set basic properties of frame. 
     setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
     setTitle("Simon"); 
     setResizable(true); 
     setLocation(DEFAULT_HORIZONTAL_LOCATION, DEFAULT_VERTICAL_LOCATION); 
     // PLACEHOLDER FOR IMAGE ICON 

     // Adding Menu Panel 
     MenuPanel menuPanel = new MenuPanel(); 
     add(menuPanel); 

     // Adding buttons panel. 
     ButtonsPanel buttonsPanel = new ButtonsPanel(); 
     add(buttonsPanel); 

     // Adding Border layout helper panel. 
     BorderPanel borderPanel = new BorderPanel(); 
     add(borderPanel); 

     // Setting grid layout (with spaces) 
     buttonsPanel.setLayout(new GridLayout(2, 2, 20, 20)); 

     // Setting background color of main panel to black. 
     buttonsPanel.setBackground(Color.black); 
    } 
    // Declaring window positioning constants. 
    public static final int DEFAULT_WIDTH = 800; 
    public static final int DEFAULT_HEIGHT = 800; 

    // TEMPORARY PLACEHOLDER FOR SCREENSIZE CALCULATIONS 
    public static final int DEFAULT_HORIZONTAL_LOCATION = 0; 
    public static final int DEFAULT_VERTICAL_LOCATION = 0; 
} 

/** 
* Class to hold the buttonsPanel 
* @author Chris 
*/ 
class ButtonsPanel extends JPanel 
{ 
    public static JButton greenButton = new JButton(); 
    public static JButton redButton = new JButton(); 
    public static JButton yellowButton = new JButton(); 
    public static JButton blueButton = new JButton(); 

    ButtonsPanel() 
    { 
     // Setting background color of buttons. 
     greenButton.setBackground(Color.GREEN); // NEED COLOR CONSTANTS 
     redButton.setBackground(Color.RED); 
     yellowButton.setBackground(Color.YELLOW);  
     blueButton.setBackground(Color.BLUE); 

     // Add buttons to panel. (In order) 
     add(greenButton); 
      add(redButton); 
     add(yellowButton); 
      add(blueButton); 

     // Creating ActionListeners for 4 main buttons. 
     ActionListener greenAction = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       greenClicked(); 
      } 
     }; 

     ActionListener redAction = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       redClicked(); 
      } 
     }; 

     ActionListener yellowAction = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       yellowClicked(); 
      } 
     }; 

     ActionListener blueAction = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       blueClicked(); 
      } 
     }; 

      // Associate actions with buttons. 
     greenButton.addActionListener(greenAction); 
     redButton.addActionListener(redAction); 
     yellowButton.addActionListener(yellowAction); 
     blueButton.addActionListener(blueAction); 
    } 

     // Handling button activations. 
     public static void greenClicked() 
     { 
      // TODO 
     } 

     public static void redClicked() 
     { 
      // TODO 
     } 

     public static void yellowClicked() 
     { 
      // TODO 
     } 

     public static void blueClicked() 
     { 
      // TODO 
     } 
} 
/** 
* Menu buttons panel. 
* @author Chris Mailloux 
*/ 
class MenuPanel extends JPanel 
{ 
    public MenuPanel() 
    { 
     setBackground(Color.BLACK); 

     // Menu panel buttons. 
     JButton startButton = new JButton("Start"); 
     JButton scoreDisplay = new JButton(String.valueOf(Simon.currentScore)); 
     JButton highScoreDisplay = new JButton(String.valueOf(Simon.highScore)); 
     add(startButton); 
     add(scoreDisplay); 
     add(highScoreDisplay); 
     scoreDisplay.setBackground(Color.BLACK); 
     highScoreDisplay.setBackground(Color.BLACK); 
     startButton.setBackground(Color.BLUE); 
     scoreDisplay.setEnabled(false); 
     scoreDisplay.setEnabled(false); 
     // ActionListeners for menu buttons. 
     ActionListener startButtonAction = new ActionListener() 
     { 
      public void actionPerformed(ActionEvent event) 
      { 
       Game.startGame(); 
      } 
     }; 
     startButton.addActionListener(startButtonAction); 
    } 
} 

/** 
* Border Panel support class. 
* @author Chris Mailloux 
*/ 
class BorderPanel extends JPanel 
{ 
    BorderPanel() 
    { 
     setLayout(new BorderLayout()); 
     add(menuPanel, BorderLayout.NORTH); 
    } 
} 
/** 
* Main game logic class. 
* @author Chris 
*/ 
class Game extends Simon // Extends to allow to inherit variables. 
{ 
// Resets variables for new game. 
public static void startGame() 
{ 
    isPlayerTurn = false; 
    pattern.clear(); 
    currentScore = 0; 
    iterator = 0; 
    gamePlay(); // Starts game 
} 

public static void gamePlay() 
{ 
    if (isPlayerTurn = false) 
    { 
     computerTurn(); 
    } 
    else 
    { 
     playerTurn(); 
    } 
} 

public static void computerTurn() 
{ 
    ButtonsPanel.greenButton.setEnabled(false); 
    ButtonsPanel.redButton.setEnabled(false); 
    ButtonsPanel.yellowButton.setEnabled(false); 
    ButtonsPanel.blueButton.setEnabled(false); 
    iterator = 0; // So CPU can use iterator to show pattern. 
    int randInt = (int) Math.random() * 4; 
    String randColor = colors[randInt]; 
    pattern.add(new String(randColor)); 
    showPattern(); 
} 

public static void playerTurn() 
{ 
    iterator = 0; 
    ButtonsPanel.greenButton.setEnabled(true); 
    ButtonsPanel.redButton.setEnabled(true); 
    ButtonsPanel.yellowButton.setEnabled(true); 
    ButtonsPanel.blueButton.setEnabled(true); 
} 

/** 
* Handles scoring and checks inputs for correctness. 
* @author Chris Mailloux 
*/ 
public static void check() 
{ 
    if(lastInput == pattern.get(iterator)) 
    { 
     iterator++; 
     if(iterator > currentScore) 
     { 
      currentScore++; 
      if(currentScore > highScore) 
      { 
       highScore++; 
       // PLACEHOLDER TO WRITE HIGHSCORE TO FILE. 
      } 
     } 
    } 
    else 
    { 
     gameOver(); 
    } 
} 

public static void showPattern() 
{ 
    int j = 0; 
    while (j < pattern.size()) 
    { 
     String showerHelper = pattern.get(j); // Helper variable. 
     switch (showerHelper) 
     { 
      case "Green" : ButtonsPanel.greenClicked(); 
      case "Red" : ButtonsPanel.redClicked(); 
      case "Yellow" : ButtonsPanel.yellowClicked(); 
      case "Blue" : ButtonsPanel.blueClicked(); 
      break; // Fallthrough handler. 
      default: System.out.println("Error: Invalid value for pattern" 
        + " ArrayList, or improper storage of variable in the" 
        + " showerHelper variable."); 
     } 
     j++; 
    } 
} 

    public static void gameOver() 
    { 
     // TODO 
    } 
} 
+1

除了很多缺少導入和至少缺少一個...東西('BorderPanel'中的'menuPanel')。 –

+0

我的進口位於頂部。我會發布其餘的代碼。一秒鐘... – user3202949

+0

代碼現在已更新。 – user3202949

回答

0

如果您的發佈代碼是完整的,那麼您很可能需要一個導入語句爲缺失的類。

import <path-to-borderpanel>.BorderPanel; 

它看起來像從你的代碼BorderPanel(與其他人一起)可能會在默認包(即 - 在定義它們的文件的頂部沒有package語句)。在這種情況下,您需要將它們放入一個包中,以便其他非默認包可以訪問它們。如果你將它們放在simon包中,那麼你根本不需要導入它們。

+0

我不需要導入在程序中創建的類。哦,對不起,忘了代碼的下半部分。一秒鐘,哈哈,上帝已經遲到了。 – user3202949

+0

對不起,代碼現在在那裏,我的copypasta技能需要工作。 – user3202949

+0

@ user3202949那麼這是一個大文件嗎? –

2

在您的課堂BorderPanel中,您使用的是......不存在的變量menuPanel

add(menuPanel, BorderLayout.NORTH); 

這就是爲什麼它告訴你它是一個缺失的符號。

也許你打算創建一個新的實例MenuPanel

add(new MenuPanel(), BorderLayout.NORTH); 
+0

只是忘了添加所有的代碼,應該在那裏。 – user3202949

+0

應該在哪裏?事實並非如此,而你收到的錯誤恰恰是會造成的。 –

+0

GameWindow構造函數中的這段代碼應該創建該變量,但是不應該這樣做? MenuPanel menuPanel = new MenuPanel(); add(menuPanel); – user3202949