返回列表 發帖
  1. package bbs.istak.org.tw;

  2. import javax.swing.JFrame;
  3. import javax.swing.JButton;
  4. import javax.swing.ImageIcon;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ActionEvent;

  9. public class Main {
  10.         
  11.         private JFrame fm;
  12.         private JButton b[]=new JButton[12];
  13.         private JButton b_r;
  14.         private ImageIcon mm[]=new ImageIcon[12];
  15.         private ImageIcon ic,mm_r,mm_b;
  16.         private int rc[]=new int[12];

  17.         Main()
  18.         {
  19.                 rc();
  20.                 ic=new ImageIcon(Main.class.getResource("star.png"));
  21.                 mm_b=new ImageIcon(Main.class.getResource("0.png"));
  22.                 mm_r=new ImageIcon(Main.class.getResource("return.png"));
  23.                
  24.                 for(int i=0; i<6; i++)
  25.                 {
  26.                         mm[i]=new ImageIcon(Main.class.getResource((i+1)+".png"));
  27.                     mm[i+6]=new ImageIcon(Main.class.getResource((i+1)+".png"));
  28.                 }
  29.                         
  30.                 for(int i=0; i<12; i++)
  31.                         b[i]=new JButton(mm_b);
  32.                 for(int i=0; i<4; i++)
  33.                     b[ i ].setBounds(i*130+12, 10, 128, 128);
  34.                 for(int i=0; i<4; i++)
  35.                     b[ i+4 ].setBounds(i*130+12, 140, 128, 128);
  36.                 for(int i=0; i<4; i++)
  37.                     b[ i+8 ].setBounds(i*130+12, 270, 128, 128);
  38.                 for(int i=0; i<12; i++)
  39.                         b[i].addMouseListener(ma);
  40.                
  41.                 b_r=new JButton(mm_r);
  42.                 b_r.setBounds(497, 407, 33, 33);
  43.                 b_r.addActionListener(al);
  44.                
  45.                 fm=new JFrame("翻牌遊戲");
  46.                 fm.setBounds(100, 100, 548, 480);
  47.                 fm.setIconImage(ic.getImage());
  48.                 fm.setVisible(true);
  49.                 fm.setResizable(false);
  50.                 fm.setLayout(null);
  51.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52.                 for(int i=0; i<12; i++)
  53.                         fm.add(b[i]);        
  54.                 fm.add(b_r);               
  55.         }
  56.         
  57.         void rc()
  58.         {
  59.                 for(int i=0; i<12; i++)
  60.                 {
  61.                         rc[i]=(int)(Math.random()*12);    //0~11
  62.                         for(int j=0; j<i; j++)
  63.                         {
  64.                                 if(rc[i]==rc[j])
  65.                                 {
  66.                                         i--;
  67.                                         break;
  68.                                 }
  69.                         }
  70.                 }
  71.         }
  72.         ImageIcon temp;
  73.         int locate = 0;
  74.         boolean isfirst = true;
  75.         MouseAdapter ma=new MouseAdapter(){
  76.                 public void mousePressed(MouseEvent e)
  77.                 {
  78.                          for(int i=0; i<12; i++)
  79.                          {
  80.                                  if(e.getSource()==b[i] && isfirst == true){
  81.                                          temp = mm[rc[i]];
  82.                                     
  83.                                          b[i].setIcon(mm[rc[i]]);
  84.                                              
  85.                                          locate = i;
  86.                                          
  87.                                      isfirst = false;
  88.                                          
  89.                                          break;
  90.                                     
  91.                                          
  92.                                  }else if(e.getSource()==b[i] && isfirst == false)
  93.                                  {
  94.                                          b[i].setIcon(mm[rc[i]]);
  95.                                          if(temp != b[i].getIcon()){
  96.                                                  b[i].setIcon(mm_b);
  97.                                                  b[locate].setIcon(mm_b);
  98.                                                  
  99.                                          }
  100.                                          
  101.                                          
  102.                                          isfirst = true;
  103.                                          break;
  104.                                  }
  105.                          }
  106.                 }
  107.         };

  108.         ActionListener al=new ActionListener() {
  109.                 public void actionPerformed(ActionEvent e) {
  110.                         if(e.getSource()==b_r)
  111.                         {
  112.                                 rc();
  113.                                 for(int i=0; i<12; i++){
  114.                                   b[i].setIcon(mm_b);

  115.                                  
  116.                                           
  117.                                  
  118.                                 }
  119.                         }
  120.                 }
  121.         };        

  122.         public static void main(String[] args) {
  123.                 new Main();        
  124.         }
  125. }
複製代碼

TOP

返回列表