//Java编程:五子棋游戏源代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.PrintStream;
import javax.swing.JComponent;
import javax.swing.JPanel;
/*
*main方法创建了ChessFrame类的一个实例对象(cf),
*并启动屏幕显示显示该实例对象。
**/
public class FiveChessAppletDemo {
public static void main(String args[]){
  ChessFrame cf = new ChessFrame();
  cf.show();
}
}
/*
*类ChessFrame主要功能是创建五子棋游戏主窗体和菜单
**/
class ChessFrame extends JFrame implements ActionListener {
private String[] strsize={"20x15","30x20","40x30"};
private String[] strmode={"人机对弈","人人对弈"};
public static boolean iscomputer=true,checkcomputer=true;
private int width,height;
private ChessModel cm;
private MainPanel mp;
//构造五子棋游戏的主窗体
public ChessFrame() {
  this.setTitle("五子棋游戏");
  cm=new ChessModel(1);
  mp=new MainPanel(cm);
  Container ContentPane();
  con.add(mp,"Center");
  this.setResizable(false);
  this.addWindowListener(new ChessWindowEvent());
  MapSize(20,15);
  JMenuBar mbar = new JMenuBar();
  this.setJMenuBar(mbar);
  JMenu gameMenu = new JMenu("游戏");
  mbar.add(makeMenu(gameMenu, new Object[] {
    "开局", "棋盘","模式", null, "退出"
    }, this));
  JMenu lookMenu =new JMenu("视图");
  mbar.add(makeMenu(lookMenu,new Object[] {
    "Metal","Motif","Windows"
    },this));
  JMenu helpMenu = new JMenu("帮助");
  mbar.add(makeMenu(helpMenu, new Object[] {
    "关于"
  }, this));
}
//构造五子棋游戏的主菜单
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 * 20+50 , h * 20+100 );
    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 >= 1){
      cm = new ChessModel(modeChess);
      Width(),cm.getHeight());
    }else{
      System.out.println("\u81EA\u5B9A\u4E49");
    }
简单的java游戏代码
    }
   
    public void actionPerformed(ActionEvent e){
    String ActionCommand();
    try{
      if (arg.equals("Windows"))
        UIManager.setLookAndFeel(
        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      else if(arg.equals("Motif"))
    UIManager.setLookAndFeel(
      "com.sun.java.if.MotifLookAndFeel");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。