返回列表 發帖
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. public class Ch92 implements ActionListener{
  4.     private JFrame fm=new JFrame("任抽一張撲克牌");
  5.     private JButton btn=new JButton("下一張");
  6.     private JLabel lb;
  7.     private ImageIcon icon[]=new ImageIcon[54],icon0;
  8.     int x=(int)(Math.random()*54);
  9.     Ch92()
  10.     {
  11.             icon0=new ImageIcon(Ch92.class.getResource("pic/poker_icon.png"));
  12.             for(int i=0;i<54;i++)
  13.                     icon[i]=new ImageIcon(Ch92.class.getResource("pic/poker"+(i+1)+".png"));
  14.             lb=new JLabel(icon[x]);
  15.             lb.setBounds(80, 10, 234,351);
  16.             btn.setBounds(370, 310, 100, 30);
  17.             btn.addActionListener(this);
  18.            
  19.             fm.setIconImage(icon0.getImage());
  20.             fm.setBounds(100,100,500,400);
  21.             fm.setVisible(true);
  22.             fm.setResizable(false);
  23.             fm.setLayout(null);
  24.             fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         fm.add(btn);
  26.         fm.add(lb);
  27.     }
  28.     public void actionPerformed(ActionEvent e)
  29.     {
  30.             if(e.getSource()==btn)       
  31.         {
  32.                     int y=(int)(Math.random()*54);  //0~53
  33.                     lb.setIcon(icon[y]);
  34.         }
  35.     }
  36.         public static void main(String[] args) {
  37.         Ch92 app=new Ch92();
  38.         }

  39. }
複製代碼

TOP

返回列表