我正在写简单的tictactoe游戏。一切顺利,直到我改变转弯。我创建了开关盒来改变X和O转。不幸的是,当我点击第一个单元格时,它会绘制X,但随后它会在下一个单元格上绘制Os。
这是主要课程:
public class Cell extends JPanel{
//dial of cell
protected static String dial="";
String cturn=getTurn();
public Cell(){
setBorder(new LineBorder(Color.black,1));
addMouseListener(new Click());
}
//get and set dials
public static String getDial(){
return dial;
}
public void setDial(String d){
dial=d;
repaint();
}
//defining shapes
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if (dial.equals("X")){
g.drawLine(10, 10, getWidth()-10, getHeight()-10);
g.drawLine(getWidth()-10, 10, 10, getHeight()-10);
}
if(dial.equals("O")){
g.drawOval(10, 10, getWidth()-20, getHeight()-20);
}
}
private class Click extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent e)
{
// if the cell is empty
if (dial.equals(""))
setDial(cturn);
else
{
switch (cturn) {
case "X":
cturn="O";
setDial(cturn);
break;
case "O":
cturn="X";
setDial(cturn);
break;
}
}
}
}
}
这是我的Cell类,其中Click类包含switch-case:
public class Game_frame extends JFrame{
private static String turn="X";
//cell grid
private Cell[][] cells = new Cell[3][3];
public Game_frame(){
JPanel panel = new JPanel (new GridLayout(3,3,0,0));
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
panel.add(cells[i][j] = new Cell());
panel.setBorder(new LineBorder (Color.red,1));
add(panel,BorderLayout.CENTER);
}
public static String getTurn(){
return turn;
}
}
还有game_frame类只保存单元格:
adb shell "ifconfig | awk '/inet addr/{print substr(\$2,6)}' | awk 'NR==2'"
答案 0 :(得分:3)
你做错了吗? 但我真的不知道,因为我试图测试你的代码,并没有工作作为一种方法似乎丢失:/ 我认为它应该是这样的:
switch (dial) {
case "X":
cturn="O";
setDial(cturn);
break;
case "O":
cturn="X";
setDial(cturn);
break;
}
答案 1 :(得分:1)
很抱歉,如果我误解了代码, 我觉得它应该拨入开关。
set AAA="C:\Windows\System32\notepad.exe"
%AAA% C:\a.txt