2014-01-17 3 views
0

저는 작업중인 Simon 게임을 가지고 있으며, 끝내기 위해 잠시 디버깅을 해왔습니다. 지금까지 한 가지 오류 만 남았습니다. 컴파일러 (NetBeans IDE 오히려)보고 오류; 구체적으로 "기호 MenuPanel; Class : 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

수입이 가장 많습니다. 나는 나머지 코드를 게시 할 것이다. 1 초 ... – user3202949

+0

코드가 업데이트되었습니다. – user3202949

답변

0

게시 된 코드가있는 경우 누락 된 클래스에 대한 import 문이 필요할 수 있습니다.

import <path-to-borderpanel>.BorderPanel; 

그것은 (다른 사람과 함께) BorderPanel는 기본 패키지에있을 수 있다는 것을 코드 (즉, -가 정의되어있는 파일의 맨 위에있는 어떤 package 문)에서처럼 보인다. 이 경우, 패키지를 기본이 아닌 다른 패키지에 액세스 할 수 있도록 패키지에 넣어야합니다. 패키지를 simon 패키지에 배치하면 가져올 필요가 없습니다.

+0

프로그램 내에서 생성 된 클래스를 가져올 필요가 없습니다. 죄송합니다. 코드 하단을 잊어 버렸습니다. 1 초 하하하, 늦었 어. – 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 = 새 MenuPanel(); add (menuPanel); – user3202949

관련 문제