Java+Swing实现的五子棋游戏

Java+Swing实现的五子棋游戏

  • 一、系统介绍
  • 二、功能展示
    • 1.游戏展示
  • 三、系统实现
    • 1.ChessFrame .java
  • 四、其它
    • 1.其他系统实现
    • 2.获取源码

一、系统介绍

五子棋游戏实现人机对战、人人对战两个模式。

二、功能展示

1.游戏展示

Java+Swing实现的五子棋游戏_第1张图片

三、系统实现

1.ChessFrame .java

package five;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import view.UserFrame;


public class ChessFrame extends JFrame implements ActionListener {
    public static boolean iscomputer = true, checkcomputer = true;
    private String[] strsize = {"标准棋盘", "改进棋盘", "扩大棋盘"};
    private String[] strmode = {"人机对战", "人人对战"};
    private int width, height;
    private ChessModel cm;
    private MainPanel mp;

    public ChessFrame() {
        this.setTitle("五子棋游戏");
        cm = new ChessModel(1);
        mp = new MainPanel(cm);
        Container con = this.getContentPane();
        con.add(mp, "Center");
        this.setResizable(false);
        this.addWindowListener(new ChessWindowEvent());
        MapSize(14, 14);
        JMenuBar mbar = new JMenuBar();
        this.setJMenuBar(mbar);
        JMenu gameMenu = new JMenu("游戏");
        mbar.add(makeMenu(gameMenu, new Object[]{
                "开局", null, "棋盘", null, "模式", null, "退出"
        }, this));
        JMenu lookMenu = new JMenu("外观");
        mbar.add(makeMenu(lookMenu, new Object[]{
                "类型一", "类型二", "类型三"
        }, this));
        JMenu userMenu = new JMenu("用户管理");
        mbar.add(makeMenu(userMenu, new Object[]{
                "用户管理"
        }, this));
        JMenu helpMenu = new JMenu("版本");
        mbar.add(makeMenu(helpMenu, new Object[]{
                "关于"
        }, this));
        this.setLocationRelativeTo(null);
    }

    public JMenu makeMenu(Object parent, Object items[], Object target) {
        JMenu m = null;
        if (parent instanceof JMenu)
            m = (JMenu) parent;
        else if (parent instanceof String)
            m = new JMenu((String) parent);
        else
            return null;
        for (int i = 0; i < items.length; i++)
            if (items[i] == null)
                m.addSeparator();
            else if (items[i] == "棋盘") {
                JMenu jm = new JMenu("棋盘");
                ButtonGroup group = new ButtonGroup();
                JRadioButtonMenuItem rmenu;
                for (int j = 0; j < strsize.length; j++) {
                    rmenu = makeRadioButtonMenuItem(strsize[j], target);
                    if (j == 0)
                        rmenu.setSelected(true);
                    jm.add(rmenu);
                    group.add(rmenu);
                }
                m.add(jm);
            } else if (items[i] == "模式") {
                JMenu jm = new JMenu("模式");
                ButtonGroup group = new ButtonGroup();
                JRadioButtonMenuItem rmenu;
                for (int h = 0; h < strmode.length; h++) {
                    rmenu = makeRadioButtonMenuItem(strmode[h], target);
                    if (h == 0)
                        rmenu.setSelected(true);
                    jm.add(rmenu);
                    group.add(rmenu);
                }
                m.add(jm);
            } else
                m.add(makeMenuItem(items[i], target));
        return m;
    }

    public JMenuItem makeMenuItem(Object item, Object target) {
        JMenuItem r = null;
        if (item instanceof String)
            r = new JMenuItem((String) item);
        else if (item instanceof JMenuItem)
            r = (JMenuItem) item;
        else
            return null;
        if (target instanceof ActionListener)
            r.addActionListener((ActionListener) target);
        return r;
    }

    public JRadioButtonMenuItem makeRadioButtonMenuItem(
            Object item, Object target) {
        JRadioButtonMenuItem r = null;
        if (item instanceof String)
            r = new JRadioButtonMenuItem((String) item);
        else if (item instanceof JRadioButtonMenuItem)
            r = (JRadioButtonMenuItem) item;
        else
            return null;
        if (target instanceof ActionListener)
            r.addActionListener((ActionListener) target);
        return r;
    }

    public void MapSize(int w, int h) {
        setSize(w * 24, h * 27);
        if (this.checkcomputer)
            this.iscomputer = true;
        else
            this.iscomputer = false;
        mp.setModel(cm);
        mp.repaint();
    }

    public boolean getiscomputer() {
        return this.iscomputer;
    }

    public void restart() {
        int modeChess = cm.getModeChess();
        if (modeChess <= 3 && modeChess >= 0) {
            cm = new ChessModel(modeChess);
            MapSize(cm.getWidth(), cm.getHeight());
        }
    }


    public void actionPerformed(ActionEvent e) {
        String arg = e.getActionCommand();
        try {
            if (arg.equals("类型三"))
                UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            else if (arg.equals("类型二"))
                UIManager.setLookAndFeel(
                        "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
            else
                UIManager.setLookAndFeel(
                        "javax.swing.plaf.metal.MetalLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception ee) {
        }
        if (arg.equals("标准棋盘")) {
            this.width = 14;
            this.height = 14;
            cm = new ChessModel(1);
            MapSize(this.width, this.height);
            SwingUtilities.updateComponentTreeUI(this);
        }
        if (arg.equals("改进棋盘")) {
            this.width = 18;
            this.height = 18;
            cm = new ChessModel(2);
            MapSize(this.width, this.height);
            SwingUtilities.updateComponentTreeUI(this);
        }
        if (arg.equals("扩大棋盘")) {
            this.width = 22;
            this.height = 22;
            cm = new ChessModel(3);
            MapSize(this.width, this.height);
            SwingUtilities.updateComponentTreeUI(this);
        }
        if (arg.equals("人机对战")) {
            this.checkcomputer = true;
            this.iscomputer = true;
            cm = new ChessModel(cm.getModeChess());
            MapSize(cm.getWidth(), cm.getHeight());
            SwingUtilities.updateComponentTreeUI(this);
        }
        if (arg.equals("人人对战")) {
            this.checkcomputer = false;
            this.iscomputer = false;
            cm = new ChessModel(cm.getModeChess());
            MapSize(cm.getWidth(), cm.getHeight());
            SwingUtilities.updateComponentTreeUI(this);
        }
        if (arg.equals("开局")) {
            restart();
        }
        if (arg.equals("关于"))
            JOptionPane.showMessageDialog(null, " 第一版\nR数学192 董磊 1918180232", "版本", JOptionPane.PLAIN_MESSAGE);
        if (arg.equals("退出"))
            System.exit(0);
        if (arg.equals("用户管理")){
        	new UserFrame();
        }
           
    }
}























四、其它

1.其他系统实现

Java+Swing实现学生选课管理系统
Java+Swing图书管理系统

2.获取源码

请联系QQ:2080866767
或点击以下链接获取源码
Java+Swing实现的五子棋游戏

你可能感兴趣的:(JavaSwing系统系列实现,java,游戏,swing)