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对象数组。
答案 0 :(得分:0)
不完全确定您收到的错误消息。以下示例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;
:
: