- import javax.swing.*;
- import java.awt.event.*;
- public class Ch92 implements ActionListener{
- private JFrame fm=new JFrame("任抽一張撲克牌");
- private JButton btn=new JButton("下一張");
- private JLabel lb;
- private ImageIcon icon[]=new ImageIcon[54],icon0;
- int x=(int)(Math.random()*54);
- Ch92()
- {
- icon0=new ImageIcon(Ch92.class.getResource("pic/poker_icon.png"));
- for(int i=0;i<54;i++)
- icon[i]=new ImageIcon(Ch92.class.getResource("pic/poker"+(i+1)+".png"));
- lb=new JLabel(icon[x]);
- lb.setBounds(80, 10, 234,351);
- btn.setBounds(370, 310, 100, 30);
- btn.addActionListener(this);
-
- fm.setIconImage(icon0.getImage());
- fm.setBounds(100,100,500,400);
- fm.setVisible(true);
- fm.setResizable(false);
- fm.setLayout(null);
- fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- fm.add(btn);
- fm.add(lb);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==btn)
- {
- int y=(int)(Math.random()*54); //0~53
- lb.setIcon(icon[y]);
- }
- }
- public static void main(String[] args) {
- Ch92 app=new Ch92();
- }
- }
複製代碼 |