huajia123456789 2013-12-24 16:37 采纳率: 50%
浏览 2064

五子棋游戏,运行结果出错,但是我总是检查不出来,希望得到帮助!!!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.awt.*;
public class Example2 extends JFrame implements

MouseListener,ActionListener
{ private ExamplePanel2[] panel=new ExamplePanel2[100];
private JButton jbStart=new JButton ("开始游戏");
private JButton jbBlack=new JButton ("黑方");
private JButton jbWhite=new JButton ("白方");
private int colorFlag=0;
Example2()
{ Container c=this.getContentPane();
c.setLayout(new BorderLayout());
JPanel pc=new JPanel();
JPanel ps=new JPanel();
c.add(pc,BorderLayout.CENTER);
c.add(pc,BorderLayout.SOUTH);
pc.setLayout(new GridLayout(10,10));
for(int i=0;i<panel.length;i++)
{ panel[i]=new ExamplePanel2();
panel[i].setNumber(i+1);
panel[i].addMouseListener(this);
pc.add(panel[i]);
}
ps.setLayout(new FlowLayout(FlowLayout.CENTER));
ps.add(jbStart);
ps.add(jbBlack);
ps.add(jbWhite);
jbStart.addActionListener(this);
jbBlack.addActionListener(this);
jbWhite.addActionListener(this);
this.setTitle("五子棋游戏");
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mouseClicked(MouseEvent arg0) { }
public void mouseEntered(MouseEvent arg0) { }
public void mouseExited(MouseEvent arg0) { }
public void mousePressed(MouseEvent arg0)
{ for(int i=0;i<panel.length;i++)
{ if(arg0.getSource()==panel[i])
{ if(colorFlag==0)
{ panel[i].setColor(0);
panel[i].repaint(); }
else
{ panel[i].setColor(1);
panel[i].repaint(); }
}
}
}
public void mouseReleased(MouseEvent arg0) { }
public void actionPerformed(ActionEvent arg0)

{ if(arg0.getSource()==jbBlack)
colorFlag=0;
else if(arg0.getSource()==jbWhite)
colorFlag=1;
else if(arg0.getSource()==jbStart)
for(int i=0;i<panel.length;i++)
panel[i].setStartFlag(1);
}
public static void main(String args[])
{ Example2 ex=new Example2(); }
}

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class ExamplePanel2 extends JPanel
{ private int color=0,number=0,startFlag=0,lock=0;
ExamplePanel2 setBackground()
{ this.setBorder(new LineBorder(Color.black,1));
this.setBorder(new Color(255,205,255)); }
public void paintComponent(Graphics g);
{ super.paintComponent(g);
if(startFlag==1)
{ if(lock==0)
{ if(color==0)
g.setColor(Color.black);
else
g.setColor(Color.white);
g.fillOval(10,10,30,30);
lock=1;
}
}
}
public int getColor()
{ return color; }
public void setColor(int color)
{ this.color=color; }
public int getNumber(int number)
{ return number; }
spublic void setNumber(int number)
{ this.number=number; }
public int getStartFlag()
{ return startFlag; }
spublic void setStartFlag(int startFlag)
{ this.startFlag=startFlag; }
}

  • 写回答

1条回答 默认 最新

  • 树熊 2013-12-25 01:37
    关注

    尝试一下格式化你的代码吧,问题提问框里有个代码的选项。

    另外,我跑了一下,没报错,但是界面什么都没有,要有什么启动参数吗?或者配置文件?

    评论

报告相同问题?

悬赏问题

  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'