SpiderCardGame-java
java编写的蜘蛛纸牌游戏代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Spider extends JFrame{
//整型变量,表示难度等级为:简单
public static final int EASY = 1;
//整型变量,表示难度等级为:普通
public static final int NATURAL = 2;
//整型变量,表示难度等级为:难
public static final int HARD = 3;
//设定初始难度等级为简单
private int grade = Spider.EASY;
private Container pane = null;
//生成纸牌数组
private PKCard cards[] = new PKCard[104];
private JLabel clickLabel = null;
private int c = 0;//牌的数量
private int n = 0;
private int a = 0;
private int finish = 0;
Hashtable table = null;
private JLabel groundLabel[] = null;
public static void main(String[] args){
Spider spider = new Spider();
spider.setVisible(true);
}
/**
**构造函数
*/
public Spider(){
setTitle("蜘蛛牌");
//关闭窗口事件(使用 System exit(0)方法,直接关闭应用程序)setDefaultCloseOperation(3)
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
//设置框架的大小
setSize(1024, 742);
//生成SpiderMenuBar对象,并放置在框架之上
setJMenuBar(new SpiderMenuBar(this));
pane = this.getContentPane();//getContentPane()获得JFrame的内容面板
//设置背景颜色
pane.setBackground(new Color(0, 112, 26));
//将布局管理器设置成为null,布局管理器用于设置游戏界面的仓储式大小及时间监听程序的引入布局
pane.setLayout(null);
clickLabel = new JLabel();
//setBounds(),用来设置组件的大小和位置,首先要把上一阶的布局管理器设为null,两个是组件左上角在容器中的坐标,两个是宽度和高度
clickLabel.setBounds(883, 606, 121, 96);
pane.add(clickLabel);
//在布局管理器中设置对鼠标时间监听程序进行初始布局
clickLabel.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent me){
if (c < 60){
Spider.this.deal();
}
}
});
//添加纸牌
this.initCards();
this.randomCards();
this.setCardsLocation();
groundLabel = new JLabel[10];
int x = 20;
//对游戏的排放位置也要布局,纸牌需要摆放10列
for (int i = 0; i < 10; i++)
{
groundLabel[i] = new JLabel();
//setBorder()设置组件外围的边框,使用一个EmptyBorder对象能在组件周围留出空白
groundLabel[i].setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
//纸牌位置的大小也要用setBounds进行设置
groundLabel[i].setBounds(x, 25, 71, 96);
x += 101;
this.pane.add(groundLabel[i]);//添加到容器
}
this.setVisible(true);
this.deal();
this.addKeyListener(new KeyAdapter(){
class Show extends Thread{
public void run(){
Spider.this.showEnableOperator();
}
}
public void keyPressed(KeyEvent e){
if (finish != 8) if (e.getKeyCode() == KeyEvent.VK_D && c < 60){
Spider.this.deal();
}
else if (e.getKeyCode() == KeyEvent.VK_M){
new Show().start();
}
}
});
}
/**
**开始新游戏
*/
……………………
………………
……………………
^-^