首页 > 解决方案 > 语法错误,插入“{”以完成 EnumBody(在类结束时)

问题描述

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 对象。

标签: javasyntaxenums

解决方案


不完全确定您收到的错误消息。下面的例子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;
        :
        :

推荐阅读