返回列表 發帖
  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import javax.swing.ImageIcon;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.ActionEvent;

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

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

  93.         public static void main(String[] args) {
  94.                 new main();        
  95.         }
  96. }
複製代碼

TOP

返回列表