浏览 2474 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-03-06  
OO
简单的纸牌游戏,需求依靠com.borland.jbcl.layout.*,可以从jbuilder下找到 :

package sample;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.lang.Thread;
public class MainFrame  extends  JFrame implements ActionListener/* implements ActionLisener*/
{
  XYLayout xYLayout1 = new XYLayout();
  JPanel jPanel1 = new JPanel();
  XYLayout xYLayout2 = new XYLayout();
  /*JTextField jTextField1 = new JTextField();
  JTextField jTextField2 = new JTextField();
  JTextField jTextField3 = new JTextField();
  JTextField jTextField4 = new JTextField();
  JTextField jTextField5 = new JTextField();
*/
  JTextField jTextField6 = new JTextField();
  JTextField jTextField7 = new JTextField();
/* JButton jButton1 = new Poker();
  JButton jButton2 = new Poker();
  JButton jButton3 = new Poker();
  JButton jButton4 = new Poker();
  JButton jButton5 = new Poker();*/

  JTextField[]   jTextField;
  JButton jButton6 = new JButton();
  Picture  pic[][];
  Poker [] jButton;
  ImageIcon back = new ImageIcon("./src/cards/b.gif");
  static  int money=100;
  static boolean  start=false;
  boolean  bool_1,bool_2,bool_3,bool_4,bool_5;
  int  a,b;
  static int row=4;
  static int column=13;
  String addr  ;
  boolean  key =false;
  JLabel jLabel1 = new JLabel();


  public MainFrame() {
    try {
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      SwingUtilities.updateComponentTreeUI(this);
    }
    catch (UnsupportedLookAndFeelException ex) {
    }
    catch (IllegalAccessException ex) {
    }
    catch (InstantiationException ex) {
    }
    catch (ClassNotFoundException ex) {
    }





      pic = new Picture[row][column];
      createIcon();
  /*   for (int i = 0; i < 4; i++)
     for (int j = 0; j < 13; j++) {
       addr = "./src/cards/pic_" + i + "_" + j + ".gif";
       pic[i][j] = new Picture();
       pic[i][j].zx = i;
       pic[i][j].zy = j;
       pic[i][j].select = false;
     }*/


    jButton = new Poker[5];
    for(int i=0;i<5;i++)
    {
      jButton[i] = new Poker();
      jButton[i].n =i;
      //jButton[i].stat =true;
      jButton[i].addActionListener(this);
    }
    jTextField  = new JTextField[5];
    for(int i=0;i<5;i++)
    {
      jTextField[i] = new JTextField();
    }


    try {
      jbInit();
      this.setSize(800,400);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception {


    xYLayout1.setWidth(790);
    xYLayout1.setHeight(358);
    jTextField[4].setRequestFocusEnabled(true);
    jTextField[4].setEditable(false);
    jTextField[4].setText("");
    jTextField[3].setVerifyInputWhenFocusTarget(true);
    jTextField[3].setEditable(false);
    jTextField[3].setText("");
    jTextField[3].addActionListener(new MainFrame_jTextField3_actionAdapter(this));
    jTextField[1].setEditable(false);
    jTextField[1].setText("");
    jTextField[1].addActionListener(new MainFrame_jTextField1_actionAdapter(this));
    jPanel1.setLayout(xYLayout2);
    this.getContentPane().setLayout(xYLayout1);
    jTextField6.setVerifyInputWhenFocusTarget(true);
    jTextField6.setEditable(false);
    jTextField6.setText("你有现金 "+money+"元");
    jTextField6.addActionListener(new MainFrame_jTextField6_actionAdapter(this));
    jTextField7.setEditable(false);
    jTextField7.setText("每次 5元");
    jTextField7.addActionListener(new MainFrame_jTextField7_actionAdapter(this));
   /*
    jButton1.setText("jButton1");
    jButton2.setText("jButton1");
    jButton3.setText("jButton1");
    jButton4.setText("jButton1");
    jButton5.setText("jButton1");
      */
    jPanel1.setBorder(BorderFactory.createTitledBorder("you are playing"));
    jButton6.setText("发牌");
    jButton6.addActionListener(new MainFrame_jButton6_actionAdapter(this));
    jTextField[2].setEditable(false);
    jTextField[2].setText("");
    jTextField[0].setEditable(false);
    jTextField[0].setText("");

    jButton[0].setIcon(back);
    jButton[1].setIcon(back);
    jButton[2].setIcon(back);
    jButton[3].setIcon(back);
    jButton[4].setIcon(back);

    jLabel1.setAlignmentY((float) 0.8);
    jLabel1.setMaximumSize(new Dimension(18, 16));
    jLabel1.setOpaque(false);
    jLabel1.setPreferredSize(new Dimension(18, 16));
    jLabel1.setIconTextGap(4);
    jLabel1.setText("");
    jLabel1.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setVerticalTextPosition(javax.swing.SwingConstants.CENTER);
    jPanel1.add(jButton[0],  new XYConstraints(36, 87, 73, 97));
    jPanel1.add(jButton[1],  new XYConstraints(145, 87, 73, 97));
    jPanel1.add(jButton[2],   new XYConstraints(258, 87, 73, 97));
    jPanel1.add(jButton[3],   new XYConstraints(364, 87, 73, 97));
    jPanel1.add(jButton[4],  new XYConstraints(462, 87, 73, 97));
    jPanel1.add(jTextField6, new XYConstraints(565, 46, 128, 23));
    jPanel1.add(jTextField7, new XYConstraints(566, 120, 128, 23));
    this.getContentPane().add(jLabel1,            new XYConstraints(406, 294, 140, 40));
    jPanel1.add(jTextField[0],   new XYConstraints(45, 31, 55, 23));
    jPanel1.add(jTextField[1],    new XYConstraints(154, 31, 55, 23));

    jPanel1.add(jTextField[2],  new XYConstraints(266, 31, 55, 23));
    jPanel1.add(jTextField[3],  new XYConstraints(369, 31, 55, 23));
    jPanel1.add(jTextField[4],  new XYConstraints(470, 31, 55, 23));
    this.getContentPane().add(jButton6,          new XYConstraints(31, 285, 93, 31));
    this.getContentPane().add(jPanel1,        new XYConstraints(32, 22, 725, 256));


  }

  void jTextField3_actionPerformed(ActionEvent e) {

  }

  void jTextField1_actionPerformed(ActionEvent e) {

  }

  void jButton6_actionPerformed(ActionEvent e) {

    System.out.println("hello----------------------");
    if(start ==false)
   {
      newPoker();
     // createIcon();
      money -=5;
      jTextField6.setText("你有现金 "+money+"元");

      System.out.print("******************");

      for (int i = 0; i < 5; i++) {
        jTextField[i].setText(null);
      }
      jLabel1.setText(null);
       start = true;


    }else
    {
        System.out.println("execute---------------------------");
      //pic_back();
      execPoker();
      shuanfa();
      start=false;

    }

  }

  public void shuanfa()
  {
    int a[] =new int[5];
    int b[] =new int [5];
    int tmp_a;
    int tmp_b;
    for (int i = 0; i < 5; i++) {
      a[i] = jButton[i].x;
      b[i] = jButton[i].y;
    }

    for(int i=1;i<5;i++)
      for(int j=0;j<4;j++)
      {
        if(b[j]>b[j+1])
        {
            tmp_b=b[j];b[j]=b[j+1];b[j+1]=tmp_b;
            tmp_a=a[j];a[j]=a[j+1];a[j+1]=tmp_a;

        }

      }

    if((b[0]==b[1])&&(b[1]==b[2])&&(b[2]==b[3])||(b[1]==b[2])&&(b[2]==b[3])&&(b[3]==b[4]))
    {
      money +=150;
      jTextField6.setText("你有现金 "+money+"元");
      System.out.println("you win "+150);
      jLabel1.setText("you win "+150);
    }
    else if(((b[0]==b[1])&&(b[1]==b[2])&&(b[3]==b[4]))||((b[0]==b[1])&&(b[2]==b[3])&&(b[3]==b[4])))
    {
      money += 60;
      jTextField6.setText("你有现金 " + money + "元");
      System.out.println("you win " + 60);
      jLabel1.setText("you win "+60);
    }
    else if((((b[0]==b[1]-1)&&(b[0]==b[2]-2)&&(b[0]==b[3]-3)&&(b[0]==b[4]-4))
            &&((a[0]==a[1])&&(a[1]==a[2])&&(a[2]==a[3])&&(a[3]==a[4])))||(
      ((b[1]==9)&&(b[2]==10)&&(b[3]==11)&&(b[4]==12))&&
      (a[0]==a[1])&&(a[0]==a[2])&&(a[0]==a[3])&&(a[0]==a[4])&&(b[0]==0)
      ))
     {
       money += 200;
       jTextField6.setText("你有现金 " + money + "元");
       System.out.println("you win " + 200);
             jLabel1.setText("you win "+200);

     }
     else if((a[0]==a[1])&&(a[1]==a[2])&&(a[2]==a[3])&&(a[3]==a[4]))
     {
       money += 50;
       jTextField6.setText("你有现金 " + money + "元");
       System.out.println("you win " + 50);
             jLabel1.setText("you win "+50);
     }
     else if(((b[0]==b[1]-1)&&(b[0]==b[2]-2)&&(b[0]==b[3]-3)&&(b[0]==b[4]-4))
             ||((b[1]==9)&&(b[2]==10)&&(b[3]==11)&&(b[4]==12)&&(b[0]==0)))
     {
       money += 40;
       jTextField6.setText("你有现金 " + money + "元");
       System.out.println("you win " + 40);
             jLabel1.setText("you win "+40);
     }
     else if((b[0]==b[1])&&(b[1]==b[2])||(b[1]==b[2])&&(b[2]==b[3])
             ||(b[2]==b[3])&&(b[3]==b[4]))
     {
       money += 30;
       jTextField6.setText("你有现金 " + money + "元");
       System.out.println("you win " + 30);
             jLabel1.setText("you win "+30);

     }
     else if ((b[0]==b[1])&&(b[2]==b[3])||(b[0]==b[1])&&(b[3]==b[4])||
              (b[1]==b[2])&&(b[3]==b[4]))
     {
       money += 15;
       jTextField6.setText("你有现金 " + money + "元");
       System.out.println("you win " + 15);
             jLabel1.setText("you win "+15);
      }
      else if (((b[0]==b[1])&&b[0]>9)||((b[1]==b[2])&&b[1]>9)||
               ((b[2]==b[3])&&b[2]>9)||((b[3]==b[4])&&b[3]>9)||
               (b[0]==b[1])&&(b[0]==0))
      {
        money += 10;
        jTextField6.setText("你有现金 " + money + "元");
        System.out.println("you win " + 10);
              jLabel1.setText("you win "+10);


      }
      else
      {
          System.out.println("you lose-----------------");
                jLabel1.setText("you lose ");
      }



   /* for(int i=0;i<5;i++)
    {
    System.out.println("--------------a[i]------" + a[i]);
    System.out.println("--------------b[i]------" + b[i]);
    }*/
  }
  public void execPoker()
  {
    // pic_back();
    for (int i = 0; i < 5; ) {
      if(jButton[i].keep ==false)
      {
        int x = (int) (Math.random() * (4 - 1) + 0.5);
        int y = (int) (Math.random() * (13 - 1) + 0.5);

        if (pic[x][y].getselect()) {
          continue;
        }
        else
        {
           // jButton[i].setIcon(null);


         //jButton[i].setText("ddddd");

         System.out.println("sleep--------");
             /* try {
                Thread.sleep(1000);
              }
              catch (InterruptedException ex) {
              }*/


          pic[x][y].setselect();
          jButton[i].setIcon(pic[x][y]);

          //    jButton[i].setIcon(back);

          jButton[i].x = x;
          jButton[i].y = y;
          i++;


        }
      }else
      {
          i++;
      }

    }
    for(int i=0;i<5;i++)
    {
      jTextField[i].setText(null);
    }
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 13; j++) {
        pic[i][j].select = false;
      }
    }
    for(int i=0;i<5;i++)
    {
    jButton[i].stat = true;
    jButton[i].keep =false;
    }



  }

  public void pic_back()
  {
    for (int i = 0; i < 5;i++ ) {
      if (jButton[i].keep == false)
      {
        jButton[i].setIcon(back);
        jButton[i].repaint();
        System.out.println("@@@@@@@@   "+i);
      }
    }
  }


  void jTextField7_actionPerformed(ActionEvent e) {

  }

  public void actionPerformed(ActionEvent e)
  {
      Poker pok =(Poker)e.getSource();
      int i =pok.x;
      int j =pok.y;
      int nn=pok.n;
      System.out.println("#####################");

    if(start == true)
    {
      if(jButton[nn].stat==true)
      {
      jTextField[nn].setText("持牌");
      pic[i][j].select = true;
      System.out.println("select on-------------------"+i);
      System.out.println("select on-------------------"+j);
      jButton[nn].keep =true;

      System.out.println("true______________________");
      jButton[nn].stat =false;
      }
      else
      {
          jTextField[nn].setText(null);
          pic[i][j].select =false;
          jButton[nn].stat=true;

          System.out.println("select off-------------------"+i);
          System.out.println("select off-------------------"+j);
          System.out.println("false_______________________");
          jButton[nn].keep=false;

      }
    }
  }
  public void  createIcon(){
   // pic = new Picture[row][column];
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 13; j++) {
        addr = "./src/cards/pic_" + i + "_" + j + ".gif";
        pic[i][j] = new Picture(addr);
        pic[i][j].zx = i;
        pic[i][j].zy = j;
       // pic[i][j].select = false;

      }


  }



  public void newPoker()
{
//  zero();



  for(int i=0;i<5;)
  {

    int x = (int) (Math.random() * (4 - 1) + 0.5);
    int y = (int) (Math.random() * (13- 1) + 0.5);
    if(pic[x][y].getselect())
          continue;
    pic[x][y].setselect();
    jButton[i].setIcon(pic[x][y]);
    jButton[i].x=x;
    jButton[i].y=y;
    i++;
   }

/*  for (int i = 0; i < 4; i++) {
     for (int j = 0; j < 13; j++) {
       pic[i][j].select =false ;
     }
   }*/
   //start=true;
}

public void zero() {
   for (int i = 0; i < 4; i++) {
     for (int j = 0; j < 13; j++) {
       pic[i][j].setselect();
     }
   }

}


  void jTextField6_actionPerformed(ActionEvent e) {

  }





}

class MainFrame_jTextField3_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jTextField3_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jTextField3_actionPerformed(e);
  }
}

class MainFrame_jTextField1_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jTextField1_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jTextField1_actionPerformed(e);
  }
}

class MainFrame_jButton6_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jButton6_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton6_actionPerformed(e);
  }
}

class MainFrame_jTextField7_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jTextField7_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jTextField7_actionPerformed(e);
  }


}

class Poker extends  JButton
{
  int n;//
  int x;
  int y;
  boolean  stat=true;
  boolean  keep=false;

  Poker(){

  }
  Poker(int x,int y){
    this.x=x;
    this.y=y;
    }
}
class Picture   extends ImageIcon
{
   int zx;
   int zy;
   boolean select =false;
   String a;
   Picture(){

   }

public  Picture(String a){
     super(a);
     //this.a =a;


   }

  public boolean  getselect(){
    return this.select;
    }
    public  void setselect(){
      this.select=true;
    }
}

class MainFrame_jTextField6_actionAdapter implements java.awt.event.ActionListener {
  MainFrame adaptee;

  MainFrame_jTextField6_actionAdapter(MainFrame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jTextField6_actionPerformed(e);
  }
}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics