java - 语法错误,插入“{”以完成 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 对象。
解决方案
不完全确定您收到的错误消息。下面的例子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;
:
:
推荐阅读
- powershell - 如何在powershell中[int]添加[string]regex替换?
- javascript - 从 .Net Core 在网页上运行 JS
- java - JPype 与 Java 10
- r - 使用 Rcpp 的 Lasso 解决方案:自学
- json - 将包含单引号和双引号的字符串变量传递给 ast.literal_eval
- wordpress - 如果条件为真,请勿发送 Woocommerce 新客户电子邮件
- python - 在 Python 中使用类定义调用实例方法
- firebase - FireBase Auth 与单独的 Recaptcha
- laravel - Laravel / Eloquent - 如何在 updateExistingPivot() 调用中添加额外的 wherePivot() 条件?
- node.js - 如何从 proto 文件生成 _grpc_pb.d.ts 以在 Node 应用程序中与 gRPC 一起使用?