4lqedz 发表于 2024-7-11 06:57:26

java五子棋程序的实现(人人对战)


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">一,界面的实现</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>还是来实现一个基本的界面,<span style="color: black;">由于</span><span style="color: black;">咱们</span><span style="color: black;">必须</span><span style="color: black;">一起</span>棋盘的区域还有<span style="color: black;">一起</span>功能区域,<span style="color: black;">因此</span><span style="color: black;">咱们</span>除了JFrame还<span style="color: black;">必须</span>用到JPanel和BorderLayout边框布局方式。代码如下:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">public</span> <span style="color: black;">class</span> <span style="color: black;">Gobong</span> <span style="color: black;">extends</span> <span style="color: black;">JPanel</span> <span style="color: black;">implements</span> <span style="color: black;">Gobonginte</span><span style="color: black;">{</span>

      <span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(</span><span style="color: black;">String</span><span style="color: black;">[]</span> <span style="color: black;">args</span><span style="color: black;">){</span>
      <span style="color: black;">Gobong</span> <span style="color: black;">gb</span><span style="color: black;">=</span><span style="color: black;">new</span> <span style="color: black;">Gobong</span><span style="color: black;">();</span>
      <span style="color: black;">gb</span><span style="color: black;">.</span><span style="color: black;">initUI</span><span style="color: black;">();</span>
      <span style="color: black;">}</span>
      <span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">initUI</span><span style="color: black;">(){</span>
      <span style="color: black;">JFrame</span> <span style="color: black;">frame</span><span style="color: black;">=</span><span style="color: black;">new</span> <span style="color: black;">JFrame</span><span style="color: black;">(</span><span style="color: black;">"五子棋"</span><span style="color: black;">);</span>
      <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">setSize</span><span style="color: black;">(</span><span style="color: black;">800</span><span style="color: black;">,</span> <span style="color: black;">680</span><span style="color: black;">);</span>
      <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">setDefaultCloseOperation</span><span style="color: black;">(</span><span style="color: black;">3</span><span style="color: black;">);</span>
      <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">setLocationRelativeTo</span><span style="color: black;">(</span><span style="color: black;">null</span><span style="color: black;">);</span>

      <span style="color: black;">JPanel</span> <span style="color: black;">jp</span><span style="color: black;">=</span><span style="color: black;">new</span> <span style="color: black;">JPanel</span><span style="color: black;">();</span>

      <span style="color: black;">BorderLayout</span> <span style="color: black;">bl</span><span style="color: black;">=</span><span style="color: black;">new</span> <span style="color: black;">BorderLayout</span><span style="color: black;">();</span><span style="color: black;">//设置JFrame的布局方式
      </span> <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">setLayout</span><span style="color: black;">(</span><span style="color: black;">bl</span><span style="color: black;">);</span>

      <span style="color: black;">GobongListener</span> <span style="color: black;">gl</span><span style="color: black;">=</span><span style="color: black;">new</span> <span style="color: black;">GobongListener</span><span style="color: black;">(</span><span style="color: black;">this</span><span style="color: black;">,</span><span style="color: black;">jb</span><span style="color: black;">);</span><span style="color: black;">//实例化事件处理类对象
      </span>
      <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">add</span><span style="color: black;">(</span><span style="color: black;">this</span><span style="color: black;">,</span><span style="color: black;">BorderLayout</span><span style="color: black;">.</span><span style="color: black;">CENTER</span><span style="color: black;">);</span><span style="color: black;">//添加中间面板
      </span>
      <span style="color: black;">jp</span><span style="color: black;">.</span><span style="color: black;">setBac<span style="color: black;">公斤</span>round</span><span style="color: black;">(</span><span style="color: black;">Color</span><span style="color: black;">.</span><span style="color: black;">DARK_GRAY</span><span style="color: black;">);</span>
      <span style="color: black;">jp</span><span style="color: black;">.</span><span style="color: black;">setPreferredSize</span><span style="color: black;">(</span><span style="color: black;">new</span> <span style="color: black;">Dimension</span><span style="color: black;">(</span><span style="color: black;">150</span><span style="color: black;">,</span><span style="color: black;">0</span><span style="color: black;">));</span>

      <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">add</span><span style="color: black;">(</span><span style="color: black;">jp</span><span style="color: black;">,</span><span style="color: black;">BorderLayout</span><span style="color: black;">.</span><span style="color: black;">EAST</span><span style="color: black;">);</span><span style="color: black;">//添加东边面板
      </span> <span style="color: black;">frame</span><span style="color: black;">.</span><span style="color: black;">setVisible</span><span style="color: black;">(</span><span style="color: black;">true</span><span style="color: black;">);</span>
      <span style="color: black;">gl</span><span style="color: black;">.</span><span style="color: black;">setG</span><span style="color: black;">(</span><span style="color: black;">getGraphics</span><span style="color: black;">());</span>

      <span style="color: black;">}</span>
    </div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>,<span style="color: black;">咱们</span><span style="color: black;">必须</span>画出一个棋盘,<span style="color: black;">由于</span>后面<span style="color: black;">咱们</span>其他类<span style="color: black;">亦</span><span style="color: black;">必须</span>,初始坐标,棋子<span style="color: black;">体积</span>等<span style="color: black;">有些</span>数据,为了便于修改和赋值,<span style="color: black;">咱们</span>定义一个接口:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">public interface Gobonginte {
      public static final int x0=30,y0=30,SIZE=30,row=20,column=20;
      public static final int[][] chesses=new int;

      }</div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了方便<span style="color: black;">咱们</span>后面进行重绘,<span style="color: black;">咱们</span><span style="color: black;">这儿</span>在继承JPanel类之后重写paint<span style="color: black;">办法</span>:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">public void paint(Graphics g){
      super.paint(g);
      this.draw(g);
      if(chesses!=null){
      this.drawqizi(g);
      }
      }</div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">画棋盘的<span style="color: black;">办法</span>:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">public void draw(Graphics g){
      //画棋盘
      for(int i=0;i&lt;row;i++){
      g.setColor(Color.black);
      g.drawLine(x0,y0+i*SIZE,x0+SIZE*(row-1),y0+i*SIZE);//画横线
      g.drawLine(x0+i*SIZE,y0 ,x0+i*SIZE,y0+SIZE*(column-1));//画横线
      }
      }</div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">二,棋盘落子</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">此刻</span>基本的界面有了,<span style="color: black;">此刻</span><span style="color: black;">必须</span>的是将在棋盘上画棋子,并且是画在交叉点上。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此时</span>为了得到点时的坐标点,<span style="color: black;">咱们</span><span style="color: black;">必须</span>添加监听器(MouseListener,ActionListener)<span style="color: black;">这儿</span>我为了方便,<span style="color: black;">选取</span>继承了MouseAdapter类:</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;">public class GobongListener extends MouseAdapter implements ActionListener,Gobonginte{

      public GobongListener(JPanel j,JComboBox&lt;String&gt; jb){
      this.jpanel=j;
      this.jb=jb;
      }


      public void mouseReleased(Mouse</div>




jm2020 发表于 2024-9-8 19:08:01

你的言辞如同繁星闪烁,点亮了我心中的夜空。

1fy07h 发表于 2024-9-29 15:42:28

你说得对,我们一起加油,未来可期。

j8typz 发表于 2024-10-29 09:30:08

楼主的文章深得我心,表示由衷的感谢!
页: [1]
查看完整版本: java五子棋程序的实现(人人对战)