【问题标题】:Java- Cannot find symbol (swing)Java-找不到符号(swing)
【发布时间】:2014-02-07 11:04:16
【问题描述】:

我正在开发一款西蒙游戏,在继续完成之前我已经调试了一段时间。到目前为止只剩下一个错误;编译器(而不是 NetBeans IDE)报错;特别是“找不到符号 MenuPanel ;类:BorderPanel”。我知道它不在这个类中,但我不完全确定如何将它指向哪里。相关代码如下:

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
    }
}

【问题讨论】:

  • 除了很多缺失的导入和至少一个缺失的......一些东西(menuPanel in BorderPanel)。
  • 我的导入在顶部。我将发布其余的代码。一秒……

标签: java swing compiler-errors symbols


【解决方案1】:

如果您发布的代码是完整的,那么您很可能需要缺失类的导入语句。

import <path-to-borderpanel>.BorderPanel;

从您的代码看来,BorderPanel(以及其他代码)可能位于默认包中(即 - 定义它们的文件顶部没有 package 语句)。在这种情况下,您需要将它们放在一个包中,以便其他非默认包可以访问它们。如果将它们放在simon 包中,则根本不需要导入它们。

【讨论】:

  • 我不需要导入在程序中创建的类。哦等等该死的对不起,忘记了代码的下半部分。一秒哈哈上帝已经晚了。
  • 抱歉,代码现在都在那里了,我的 copypasta 技能需要改进。
  • @user3202949 这就是一个大文件吗?
  • 是的。这是一个较小的程序,所以我认为没有必要拆分成不同的文件。
【解决方案2】:

在您的班级BorderPanel 中,您正在使用一个不存在的变量menuPanel

add(menuPanel, BorderLayout.NORTH);

这就是为什么它告诉你它是一个缺失的符号。

也许您打算创建一个MenuPanel 的新实例?

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

【讨论】:

  • 应该在哪里?不是,您收到的错误正是这会导致的。
  • gameWindow 构造函数中的这段代码应该创建该变量,不是吗?菜单面板 menuPanel = new MenuPanel();添加(菜单面板);
  • 这与您的BorderPanel 课程完全无关。
  • @RomanVottner 从技术上讲,这是完全合法的(你不会在实际项目中这样做);只定义了一个类public;这与我在回答中解释的他收到的错误无关。
  • 是的,正如@BrianRoach 所描述的,符号menuPanel(带有小写m)未在其使用范围内定义。
猜你喜欢
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 1970-01-01
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多