问题描述
- 运行结果正确,在连续5黑或5白可以自动出现一个对话框,显示赢了也可选继续或退出游戏
-
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(ps,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;
private int number=0;
private int startFlag=0;
private int lock=0;
ExamplePanel2 ()
{ this.setBorder(new LineBorder(Color.black,1));
this.setBackground(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()
{ return number; }
public void setNumber(int number)
{ this.number=number; }
public int getStartFlag()
{ return startFlag; }
public void setStartFlag(int startFlag)
{ this.startFlag=startFlag;
}
}import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class ExamplePanel2 extends JPanel
{ private int color=0;
private int number=0;
private int startFlag=0;
private int lock=0;
ExamplePanel2 ()
{ this.setBorder(new LineBorder(Color.black,1));
this.setBackground(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()
{ return number; }
public void setNumber(int number)
{ this.number=number; }
public int getStartFlag()
{ return startFlag; }
public void setStartFlag(int startFlag)
{ this.startFlag=startFlag;
}
}