class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable{
//玩家
JLabel play[] = new JLabel[32];
//棋盘
JLabel image;
//窗格
Container con;
//工具栏
JToolBar jmain;
//重新开始
JButton anew;
//悔棋
JButton repent;
//打开
JButton showOpen;
//保存
JButton showSave;
//退出
JButton exit;
//当前信息
JLabel text;
//保存当前操作
Vector FileVar;
Vector Var;
//规则类对象(使用于调用方法)
ChessRule rule;
/单击棋子******************************/
/chessManClick = true 闪烁棋子 并给线程响应/
/chessManClick = false 吃棋子 停止闪烁 并给线程响应/
boolean chessManClick;
/控制玩家走棋******************************/
/chessPlayClick = 1 黑棋走棋/
/chessPlayClick = 2 红旗走棋 默认红旗/
/chessPlayClick = 3 双方都不能走棋红旗走棋 默认红旗/
int chessPlayClick = 2;
//控制棋子闪烁的线程
Thread tmain;
//把第一次的单击给线程响应
static int Man,i;
ChessMainFrame(){
}
ChessMainFrame(String Title){
//获行刻格引用
con = this.getContentPane();
con.setLayout(null);
//实例化规则类
rule = new ChessRule();
FileVar = new Vector();
Var = new Vector();
//创建工具栏
jmain = new JToolBar();
text = new JLabel(“欢迎来到中国象棋”);
text.setToolTipText(“提示信息”);
anew = new JButton(“新游戏”);
anew.setToolTipText(“重新开始”);
exit = new JButton(“退出”);
exit.setToolTipText(“退出本程序”);
repent = new JButton(“悔棋”);
repent.setToolTipText(“返回到上次走棋的位置”);
showOpen = new JButton(“打开”);
showOpen.setToolTipText(“打开以前棋局”);
showSave = new JButton(“保存”);
showSave.setToolTipText(“保存当前棋局”);
//把组件添加到工具栏
jmain.setLayout(new GridLayout(0,6));
jmain.add(anew);
jmain.add(repent);
jmain.add(showOpen);
jmain.add(showSave);
jmain.add(exit);
jmain.add(text);
jmain.setBounds(0, 500, 450, 30);
con.add(jmain);
//添加棋子标签
drawChessMan();
/注册监听者/
anew.addActionListener(this);
repent.addActionListener(this);
exit.addActionListener(this);
showOpen.addActionListener(this);
showSave.addActionListener(this);
//注册棋子移动监听
for(int i=0;i<32;i++) {
drawChessMan();
con.add(play[i]);
play[i].addMouseListener(this);
}
//添加棋盘标签
con.add(image = new JLabel(new ImageIcon(“CChess.GIF”)));
image.setBounds(0, 0, 445, 498);
image.addMouseListener(this);
//注册窗体关闭监听
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
//窗体居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if(frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width)/2-200,(screenSize.height - frameSize.height)/2-290);
this.setIconImage(new ImageIcon(“相1.gif”).getImage());
this.setResizable(false);
this.setTitle(Title);
this.setSize(450, 555);
this.show();
}
/添加棋子方法************/
public void drawChessMan() {
//流程控制
int i,k;
//图标
Icon in;
//黑色棋子
//车
in = new ImageIcon(“车1.GIF”);
for(i=0,k=10;i<2;i++,k+=385);{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName(“车1”);
}
//马
in = new ImageIcon(“马1.GIF”);
for(i=4,k=60;i<6;i++,k+=285);{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName(“马1”);
}
//象
in = new ImageIcon(“象1.GIF”);
for(i=8,k=105;i<10;i++,k+=195);{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName(“象1”);
}
//士
in = new ImageIcon(“士1.GIF”);
for(i=12,k=155;i<14;i++,k+=95);{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName(“士1”);
}
//卒
in = new ImageIcon(“卒1.GIF”);
for(i=16,k=10;i<21;i++,k+=96.5);{
play[i] = new JLabel(in);
play[i].setBounds(k,160,40,40);
play[i].setName(“卒1”+i);
}
//炮
in = new ImageIcon(“炮1.GIF”);
for(i=26,k=60;i<28;i++,k+=289);{
play[i] = new JLabel(in);
play[i].setBounds(k,110,40,40);
play[i].setName(“炮1”+i);
}
//将
in = new ImageIcon(“将1.GIF”);
play[30] = new JLabel(in);
play[30].setBounds(205,10,40,40);
play[30].setName(“将1”);
//红色棋子
//车
in = new ImageIcon(“车2.GIF”);
for(i=2,k=10;i<4;i++,k+=385);{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName(“车2”);
}
//马
in = new ImageIcon(“马2.GIF”);
for(i=6,k=60;i<8;i++,k+=285);{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName(“马2”);
}
//相
in = new ImageIcon(“相2.GIF”);
for(i=10,k=105;i<12;i++,k+=195);{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName(“相2”);
}
//仕
in = new ImageIcon(“仕2.GIF”);
for(i=14,k=155;i<16;i++,k+=95);{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName(“仕2”);
}
//兵
in = new ImageIcon(“兵2.GIF”);
for(i=21,k=10;i<26;i++,k+=96.5);{
play[i] = new JLabel(in);
play[i].setBounds(k,300,40,40);
play[i].setName(“兵2”+i);
}
//炮
in = new ImageIcon(“炮2.GIF”);
for(i=28,k=60;i<30;i++,k+=289);{
play[i] = new JLabel(in);
play[i].setBounds(k,350,40,40);
play[i].setName(“炮2”+i);
}
//帅
in = new ImageIcon(“帅2.GIF”);
play[31] = new JLabel(in);
play[31].setBounds(205,450,40,40);
play[31].setName(“帅2”);
}
}