java:Swing(1)模拟随机抽签

package SwingBasicStudy;


import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/* 模拟随机抽签过程,
 * */
 //定义一个随机生成器接口;
interface Generator2<T>{
    T next();
}
//用于现实游戏用户头像的随机生成
class MemberImages implements Generator2<File> {

    @Override
    public File next() {
        //把文件中图片加到map集合中
        HashMap fileMap = 
            new HashMap();
        File[] files = new File("img").listFiles();
        ArrayList names = new ArrayList();
        for(File f:files){
            names.add(f.getName());
            fileMap.put(f.getName(),f);
        }

        File file = fileMap.get(names.get(
            new Random().nextInt(files.length)));
        return file;
    }

}
public class RandDrawLotsGameTwo {
    public static void ini(){
        JFrame frame = new JFrame("Random Draw Lots Game");
        JLabel label = new JLabel("Ready Go:");
        JLabel label2 = new JLabel("name");
        JButton button  = new JButton("随机抽签");

        //设置布局管理:
        GridBagLayout gl = new GridBagLayout();
        frame.setLayout(gl);

        frame.add(label);
        frame.add(label2);
        frame.add(button);

        //是用来控制添加进的组件的显示位置  
        GridBagConstraints s= new GridBagConstraints();//定义一个GridBagConstraints,  
        s.fill = GridBagConstraints.NONE;  
        //该方法是为了设置如果组件所在的区域比组件本身要大时的显示情况  

        s.gridwidth=0;//该方法是设置组件水平所占用的格子数,
        s.weightx = 1;//该方法设置组件水平的拉伸幅度,
        s.weighty=1;//该方法设置组件垂直的拉伸幅度,
        gl.setConstraints(label, s);//设置组件  

        s.gridwidth=0;
        s.weightx = 1;
        s.weighty=1;
        gl.setConstraints(label2, s); 

        s.gridwidth=0;  
        s.weightx = 0;  
        s.weighty=1;  
        gl.setConstraints(button, s);  
        //给按钮增加点击事件:
        class ButtonListener implements ActionListener{
            private int count=1;
            public void actionPerformed(ActionEvent e) {

                    File file = new Resourse().next();
                    ImageIcon icon = new ImageIcon(file.getAbsolutePath());
                    label.setText("");
                    label.setIcon(icon);

                    String fileName = file.getName();
                    String name = fileName.substring(
                        0, fileName.length()-4);
                    label2.setText("第 "+ count++ +
                        " 次抽签的幸运号是: "+name);

                    try {
                        TimeUnit.SECONDS.sleep((long)0.5);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
            }
        }           

        //在按钮上注册这个事件:
        button.addActionListener(new ButtonListener());


        //设置默认关闭动作
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗体大小
        frame.setBounds(300, 100, 100, 200);
        frame.setSize(600,500);
        frame.setVisible(true);
    }


    public static void main(String[] args) throws InterruptedException {
        ini();
    }   

}



运行结果展示:
java:Swing(1)模拟随机抽签_第1张图片
java:Swing(1)模拟随机抽签_第2张图片

你可能感兴趣的:(JAVA-SE)