繁体   English   中英

语法错误,插入“ {”以完成EnumBody(在类末尾)

[英]Syntax error, insert “{” to complete EnumBody (at end of class)

package checkers;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JPanel;


enum Job{SPAWN, KING, NORM};
enum myColor{RED, BLACK};

int tileRow;
int tileCol;
Job job;
myColor side;
JButton button;
Checker piece;
Color color;

public class Tile 
{
    public Tile(int posRow, int posCol, JPanel panel, ActionListener listener)
    {
        int tileRow = posRow;
        int tileCol = posCol;
        if(tileRow > 9 || tileRow < 1)
            job = Job.KING;
        else if(tileRow < 4 || tileRow > 6)
            job = Job.SPAWN;
        else
            job = Job.NORM;

        button = new JButton();
        button.setPreferredSize(new Dimension(83, 83));
        if(tileRow%2==0)
        {
            if(tileCol%2==0)
            {
                color = Color.BLACK;
            }
            else
                color = Color.RED;
        }
        else
        {
            if(tileCol%2!=0)
            {
                color = Color.BLACK;
            }
            else
                color = Color.RED;
        }
        button.addActionListener((java.awt.event.ActionListener) listener);
    }

    public void Reset()
    {

    }

    public boolean isClicked(Object source)
    {
        if(source == button)
            return true;
        else 
            return false;
    }




}

编辑我在整个代码体内进行了编辑。 据Eclipse知道,myColor的右花括号被“假定”在classBody之后。

Eclipse希望我删除myColor的右括号,并用分号替换; 不管我是否放置分号,Eclipse都会告诉我不应该使用右括号,如果删除它,则将我的classBody右括号读取为EnumBody右括号。

我不知道这到底是怎么回事,但这肯定会导致类中发生奇怪的事情(为方格游戏创建Tile类+对象)。

奇怪的是,如果我希望Eclipse读取没有错误的Tile,则无法从另一个类中创建Tile对象的数组。

不完全确定您收到的错误消息。 下面的示例EnumIssue.java运行正常:

public class EnumIssue {

    enum Job
    {
        SPAWN, KING, NORM
    }
    enum myColor
    {
        RED, BLACK
    }

    public static void main(String[] args) {
        Job j = Job.SPAWN;
        myColor c = myColor.BLACK;
        System.out.println(j);
        System.out.println(c);
    }
}

输出:

SPAWN
BLACK

在提供完整代码后添加:

将变量声明移动到Tile类中。 更新的代码段如下:

:
:
import javax.swing.JPanel;

enum Job{SPAWN, KING, NORM};
enum myColor{RED, BLACK};

//This is where current variable declarations are. Move them inside class.

public class Tile
{
    //This is where variable declarations are moved to. 
    int tileRow;
    int tileCol;
    Job job;
    myColor side;
    JButton button;
    Checker piece;
    Color color;

    public Tile(int posRow, int posCol, JPanel panel, ActionListener listener)
    {
        int tileRow = posRow;
        :
        :

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM